Originally Posted by
davidvorob
I don't necessarily want to save out the INSERT query as a standalone query as it will 1) dirty up the list of queries i have (already have too many) and 2) provide ability for someone to alter data by double clicking on it (would like to control it in a better way).
Just hard code all of it?
Code:
Public Sub example()
On Error GoTo ErrHandler
Dim db As DAO.Database
Dim sql As String
Dim startDate As Date
Dim endDate As Date
'search criterea
startDate = #1/1/2021#
endDate = #12/31/2021#
'build the sql code
sql = ""
sql = sql & "INSERT INTO targetTable " & vbCrLf
sql = sql & "SELECT field1, " & vbCrLf
sql = sql & " field2, " & vbCrLf
sql = sql & " field3 " & vbCrLf
sql = sql & "FROM sourceTable " & vbCrLf
sql = sql & "WHERE dateField >= #" & startDate & "#" & vbCrLf
sql = sql & " AND datefield <= #" & endDate & "#;"
'print the sql string in the immediate window (Ctrl+G) make sure it looks correct
Debug.Print sql
'execute the sql code
Set db = CurrentDb
db.Execute sql, dbFailOnError
ExitHandler:
'clean up objects
Set db = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, , "Error #" & Err.Number
Resume ExitHandler
End Sub