Results 1 to 4 of 4
  1. #1
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185

    How to write SQL code in VBA

    Hi All



    If I have some SQL code like this:

    Code:
    INSERT INTO tbl_Changes ( HB, Finished )
    SELECT         tbl_Main.HB, tbl_Main.Finished
    FROM         tbl_Main RIGHT JOIN tbl_Import_TEMP ON tbl_Main.HB = tbl_Import_TEMP.HB
    WHERE         tbl_Main.Finished<>tbl_Import_TEMP.Finished
    How do I write this into VBA in a Access Module?

    Like I tried this but I think I have messed it up:

    Code:
    Sub test()
    
    Dim strSQL As String
    strSQL = "INSERT INTO ChangesTB ( HB, Status, ETA) SELECT Main.HB, Main.Status FROM Main RIGHT JOIN UpdateTB ON Main.HB = UpdateTB.HB WHERE Main.Status<>UpdateTB.Status"
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    Exit_Command2_Click:
    Exit Sub
    
    
    End Sub
    I've edited the code a bit to match actual tables and fields, though I might have messed that up also.

    Thanks

  2. #2
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185
    Ok sorry I think I have it by having this code instead:

    Code:
    Sub test()
    
    Dim strSQL As String
    strSQL = "INSERT INTO ChangesTB ( HB, Status) SELECT Main.HB, Main.Status FROM Main RIGHT JOIN UpdateTB ON Main.HB = UpdateTB.HB WHERE Main.Status<>UpdateTB.Status"
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    Exit_Command2_Click:
    Exit Sub
    
    
    End Sub
    I'm not entirely sure what "Main.Status FROM Main RIGHT JOIN UpdateTB ON Main.HB = UpdateTB.HB" does exactly?

    Basically I'm trying to record changes made to the Main table from the update table?

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    For a good set of tutorials and the mix of SQL and VBA see Martin Green's site.

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    The easiest way...let Access do it via visual object sql. No need to 'write' sql when you can drop tables & fields into the grid then click the SQL button.

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

Similar Threads

  1. Replies: 2
    Last Post: 09-04-2013, 11:01 PM
  2. Replies: 3
    Last Post: 10-02-2012, 02:07 AM
  3. i want to show description when i write the code
    By Mina Garas in forum Access
    Replies: 3
    Last Post: 02-23-2012, 11:00 AM
  4. How To Write Vb Code For This?
    By townguy in forum Queries
    Replies: 1
    Last Post: 09-15-2011, 12:00 AM
  5. Trying to write a simplet vba code
    By bopsgtir in forum Programming
    Replies: 6
    Last Post: 03-12-2011, 08:52 AM

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