I am trying something that has been done hundreds of times - except my code does not compile. Using this as a reference How to do INSERT into a table records extracted from another table I came up with the following sub, that is supposed to clear a table and refill it from another linked table (The idea is to liberate the linked table so it doesn't have a .laccdg file in its name for long)
Private Sub Form_Open(Cancel As Integer)
Dim rsDocs As Recordset
Dim sqlQuery As String
DoCmd.RunSQL "DELETE * FROM [Docs]"
sqlQuery = "INSERT INTO [Docs] (Numero, Description, [ID Symix], Groupe, [ID Sami])" & _
" SELECT [Unité] & "" "" & [Numéro Document] AS Numero, Description, [ID Symix], [Groupe Source], [ID Doc Sami]" & _
" FROM [Documents]"
Debug.Print sqlQuery
Set rsDocs = CurrentDb.OpenRecordset(sqlQuery)
CurrentDb.Execute sqlQuery
rsDocs.Close
Set rsDocs = Nothing
End Sub
I get an error on the execute line. The sql statement is wrong. Can someone see where it falters? Is the use of "" "" to join two fields together acceptable in this situation?
Also, clearing the table prompts a message asking if i am sure i want to do this. Will setting DisplayAlerts to False have negative consequences? Or should it be harmless if I put it back to True right after?
No need for extra set of double quotes
A general hint for debugging queries you design in VBA:
Usually you will get a more specific error message from the query editor than what you get from VBA.
DisplayAlerts in Access is: DoCmd.SetWarnings True/False
But it is better to avoid it by using DB.Execute instead of DoCmd.RunSQL
Instead of 2 sets of double quotes, which didn't work, or 1 set of double quotes, which don't produce a valid string, use 1 set of single quotes: