Results 1 to 6 of 6
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Run Append Query Through VBA


    I know you can use a delete query in vba by
    Code:
    DoCmd.RunSQL "DELETE * FROM table1"
    Can you do the same with an Append query? Everything I see says to create an actual query and call the queryname from your vba.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Can you do the same with an Append query?
    Yes, I do it all of the time.

    Except I use
    Code:
    Currentdb.Execute "DELETE * FROM table1", dbFailOnError
    Another example:
    Code:
     sSQL = "INSERT INTO RecordSources (FormName, ctlType, frmRecSource) VALUES ('" & obj.Name & "', 'Form', '" & tmp & "');"
      '                Debug.Print sSQL
      db.Execute sSQL, dbFailOnError

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    So it would be insert into as opposed of append?

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try creating a new append query, then switch to SQL View to see what the SQL looks like.

    (But yes, the SQL of an append query begins with "INSERT INTO..........")

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by ssanfu View Post
    Yes, I do it all of the time.

    Except I use
    Code:
    Currentdb.Execute "DELETE * FROM table1", dbFailOnError
    Another example:
    Code:
     sSQL = "INSERT INTO RecordSources (FormName, ctlType, frmRecSource) VALUES ('" & obj.Name & "', 'Form', '" & tmp & "');"
      '                Debug.Print sSQL
      db.Execute sSQL, dbFailOnError
    This is my full procedure, it seems to execute fine but access will crash EVERY time I run it and the append/insert into is not actually executed. If I break it down piece by piece it is the append/insert statement that causes the bug as the importing the Excel Spreadsheet goes rocket fast.

    Do you see any issues with my syntax below? This is Access 2013
    Code:
    Public Sub ImportSpreadsheet()
        Dim strXlsx As String, monthname As String
    
    
        DoCmd.SetWarnings False
    
    
        monthname = Format(CStr(Now()), "mmmm")
        strXlsx = "c:\tEST.xls"
        DoCmd.TransferSpreadsheet acImport, , monthname, strXlsx, True, monthname & "!"
    
    
        Do.Cmd.RunSQL "INSERT INTO [ALL_Data] ( [1], [2], [3], 4, [5] )" _
                                + "SELECT [1], [2], [3], 4, [5]" _
                                + "FROM monthname;"
        DoCmd.SetWarnings True
    
    
    End Sub

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    Do.Cmd.RunSQL "INSERT INTO [ALL_Data] ( [1], [2], [3], 4, [5] ) SELECT [1], [2], [3], 4, [5] FROM monthname;"
    Two things:
    1) You REALLY have fields named [1], [2], [3], 4 and [5]???? The Access gnomes can get REALLY peeved when field names begin with a number

    2) The command is "DoCmd.RunSQL", not "Do.Cmd.RunSQL" (with a period between Do and cmd).

    3) If you Set Warnings OFF, you MUST turn them back on at the end of the sub!
    Code:
    DoCmd.SetWarnings TRUE
    This is why I use the "CurrentdB.Execute........" command. The set warnings commands is not needed.


    As to the code executing properly, I don't know (because of the field names). But I don't see any other errors.


    Good luck

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

Similar Threads

  1. Replies: 1
    Last Post: 09-02-2015, 08:44 AM
  2. Access can't append all the records in the append query
    By fluffyvampirekitten in forum Access
    Replies: 2
    Last Post: 08-27-2015, 01:53 AM
  3. Replies: 1
    Last Post: 10-06-2011, 08:37 AM
  4. Replies: 7
    Last Post: 07-21-2011, 01:01 PM
  5. Append query won't append
    By yelkenli in forum Queries
    Replies: 5
    Last Post: 02-12-2010, 11:19 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