Results 1 to 4 of 4
  1. #1
    Davidyam is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2012
    Posts
    14

    Converting SQL to VBA code

    Hi,

    I need help converting from SQL to VBA, can anyone help please. Thank you very much.

    TRANSFORM Sum([Overtime Records].Duration) AS SumOfDuration
    SELECT [Overtime Records].Officer, Sum([Overtime Records].Duration) AS [Total Of Duration]
    FROM [Overtime Records]
    GROUP BY [Overtime Records].Officer
    PIVOT [Overtime Records].Status;


    TRANSFORM Sum([Off-in-Lieu records].[Number of hours of off taken]) AS [SumOfNumber of hours of off taken]
    SELECT [Off-in-Lieu records].Officer, Sum([Off-in-Lieu records].[Number of hours of off taken]) AS [Total Of Number of hours of off taken]
    FROM [Off-in-Lieu records]
    GROUP BY [Off-in-Lieu records].Officer
    PIVOT [Off-in-Lieu records].Status;




    Thanks you.

    Best Regards.
    David Yam

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    What do you mean by 'convert SQL to VBA'? That isn't really possible. VBA can recognize and run SQL statements that are constructed in code but that is not a 'conversion'. If what you want to do is take an SQL statement built with Access query designer and write it into a code procedure, that is possible.

    However, I have never seen a pivot query SQL written into a VBA procedure.

    Perhaps if you explained more about why you want to do this and what you are really trying to accomplish, we can better advise.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Davidyam is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2012
    Posts
    14
    Hi June,

    Thanks for replying.
    Actually I wan to create an overtime database.
    Currently I have created a form whereby it calculates the hours base on my VBA code and is reflected in the table. I have also created a crosstab query but I think it is not useful thus I wan to change it to VBA code.

    I have 3 main tables: Overtime records, Off-in-lieu records and account overview
    Overtime records :Officer, duration of OT hours clocked and the status are my main concerns.
    Off-in-lieu records :Officer, numbers of hours of off taken and status
    Account overview :Balance

    I would like to sum-up the total amount of duration of OT hours by distinct officer with status approved from the overtime records table using VBA codes and minus away the total number of hours of off taken by the similar distinct officer with status approved from the Off-in-lieu records table and show it in the third table account overview where it shows the balance hours left by the officers.

    Thank you again

    Best regards,
    David Yam

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    First of all, the third table for storing aggregate data should not be needed. Queries should be able to perform and display the summary calcs you want.

    Try: Do two GROUP BY (totals) query on the Overtime and Off-in-lieu tables. Join those two queries to Officers table. Use the third query as RecordSource for a report to display records.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 01-05-2012, 11:55 AM
  2. Converting a Code to Text
    By rmcafee in forum Programming
    Replies: 7
    Last Post: 09-13-2011, 08:52 PM
  3. Converting mde to mdb
    By Jdejesus in forum Access
    Replies: 2
    Last Post: 04-08-2011, 11:52 AM
  4. Converting 2000 to 2007
    By Nettie in forum Access
    Replies: 8
    Last Post: 10-29-2010, 07:29 AM
  5. Converting HH:MM to decimal
    By katrinanyc926 in forum Queries
    Replies: 5
    Last Post: 08-13-2010, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums