MarkK
bit cruncher
- Local time
- Yesterday, 22:45
- Joined
- Mar 17, 2004
- Messages
- 8,435
My 2c is that running SQL in VBA is simpler, safer, more readable, and easier to maintain, if you use a temporary DAO.QueryDef object. Consider code like...
- String parameters are handled for you so your text data can contain apostrophes and double quotes. Names like O'Malley, and data like 8' - 2" x 4" are handled seamlessly, and exactly as entered by your user.
- Date parameters are handled for you so you don't have to worry about formats. Simply assign a valid date to a date parameter. Done.
Code:
Sub TempQDFSample()
Const SQL As String = _
"INSERT INTO MyTable " & _
"( SomeDate, StringField ) " & _
"SELECT Date1, prmComment " & _
"FROM OtherTable " & _
"WHERE StartDate = prmD1 AND EndDate prmD2 "
With CurrentDb.CreateQueryDef("", SQL)
.Parameters("prmComment") = Me.txtComment
.Parameters("prmD1") = Me.tbDateStart
.Parameters("prmD2") = Me.tbDateEnd
.Execute dbFailOnError
End With
End Sub
- Date parameters are handled for you so you don't have to worry about formats. Simply assign a valid date to a date parameter. Done.