CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2007
    Posts
    162

    [RESOLVED] VB and MS Access

    This may be more of an Access question than a VB question but I am not overly familiar with either so I am trying to cover all bases.

    I need to convert local time to UTC (GMT) in a MS Access application.

    The application presently uses a simple VB routine to add either 3 or 4 hours to the local time depending on whether we are on DST or EST time. The problem is that we have to manually change this 3 to a 4 and vice versa in the spring and the fall.

    VB has a built in to handle this as shown below.

    Dim saveUtcNow As DateTime = DateTime.UtcNow

    This would give me the UTC time in saveUtcNow.

    When I try to get this VB code into MS Access it won't buy it. I am wondering if I need to include some other code ( a further declaration or an import ) into the MS Access VB window to get it to work.

    Thanks
    Zapper

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VB and MS Access

    You could convert it to a string. What version of Access are you using?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2007
    Posts
    234

    Re: VB and MS Access

    Wait... are you saying you're trying to use this code inside of Access?
    Dim saveUtcNow As DateTime = DateTime.UtcNow


    That looks like VB.NET.... while Access uses VBA which is a subset of the older VB6 set. So the above line isn't going to work in Access.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  4. #4
    Join Date
    Apr 2007
    Posts
    162

    Re: VB and MS Access

    MS Access 97

    The difficulty is VB in Access is not behaving the same as VB in MS studio.
    The following works in MS Studio VB

    Dim date1 As Date
    date1 = Date.UtcNow

    but it won't work in VB for Access. I get an error on the "Date.UtcNow"

    I am not sure if Access has its own version of VB or whether it can be directed to use the version that MS studio is using ??

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VB and MS Access

    Here's an Excel Macro that should work:

    Code:
    Option Explicit
    
    Public Declare Function SystemTimeToFileTime Lib _
      "kernel32" (lpSystemTime As SYSTEMTIME, _
      lpFileTime As FILETIME) As Long
    
    Public Declare Function LocalFileTimeToFileTime Lib _
      "kernel32" (lpLocalFileTime As FILETIME, _
      lpFileTime As FILETIME) As Long
    
    Public Declare Function FileTimeToSystemTime Lib _
      "kernel32" (lpFileTime As FILETIME, lpSystemTime _
      As SYSTEMTIME) As Long
    
    Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    
    Public Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
    End Type
    
    Public Function LocalTimeToUTC(dteTime As Date) As Date
        Dim dteLocalFileTime As FILETIME
        Dim dteFileTime As FILETIME
        Dim dteLocalSystemTime As SYSTEMTIME
        Dim dteSystemTime As SYSTEMTIME
    
        dteLocalSystemTime.wYear = CInt(Year(dteTime))
        dteLocalSystemTime.wMonth = CInt(Month(dteTime))
        dteLocalSystemTime.wDay = CInt(Day(dteTime))
        dteLocalSystemTime.wHour = CInt(Hour(dteTime))
        dteLocalSystemTime.wMinute = CInt(Minute(dteTime))
        dteLocalSystemTime.wSecond = CInt(Second(dteTime))
    
        Call SystemTimeToFileTime(dteLocalSystemTime, _
          dteLocalFileTime)
        Call LocalFileTimeToFileTime(dteLocalFileTime, _
          dteFileTime)
        Call FileTimeToSystemTime(dteFileTime, dteSystemTime)
    
        LocalTimeToUTC = CDate(dteSystemTime.wMonth & "/" & _
          dteSystemTime.wDay & "/" & _
          dteSystemTime.wYear & " " & _
          dteSystemTime.wHour & ":" & _
          dteSystemTime.wMinute & ":" & _
          dteSystemTime.wSecond)
    End Function
    This may look imposing, as is often the case when working with system calls, but it works wonderfully. There are three system routines referenced (SystemTimeToFileTime, LocalFileTimeToFileTime, and FileTimeToSystemTime). By setting up the calls and using them in order, the date and time are automatically adjusted to GMT. To use the function, in your worksheet you would enter this to convert the time in cell B7:
    
    =localtimetoutc(B7)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Apr 2007
    Posts
    162

    Re: [RESOLVED] VB and MS Access

    Thanks man, that should do what I need.

    Zapper

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured