I am trying to run an append query to update a table based on the first 30 records for parameters in an sql statement. All of the data resides in an Access 2010 database, and I would like to run the query based off of a button on a form.
I am new to vba and assembled the following code based off of posts.
Option Compare Database
Private Sub Command3_Click()
Dim sql As String
Dim i As Integer
Dim j As Integer
Dim rst As DAO.Recordset
Dim dbs As DAO.Database
Dim strTerritory As String
Set dbs = CurrentDb
strTerritory = "Alex Hernandez"
strSQL = "INSERT INTO tblWeather30DayMovingFinal ( NEW, RptdDate, [Clm Nbr], WeatherLimit ) SELECT TOP 30 tblWeather30DayMoving.[NEW], tblWeather30DayMoving.[RptdDate], tblWeather30DayMoving.[Clm Nbr], 1 AS WeatherLimit FROM tblWeather30DayMoving WHERE (((tblWeather30DayMoving.NEW)= strTerritory ) AND ((tblWeather30DayMoving.RptdDate) Between #" & i & "/1/" & j & "# And #" & i & "/28/" & j & "#)); "
Set rst = dbs.OpenRecordset("tblWeather30DayMoving", dbOpenTable)
With rst
For j = 2003 To 2013
For i = 1 To 12
If Not (rst.EOF And rst.BOF) Then
.MoveFirst
Do
CurrentDb.Execute strSQL
.MoveNext
Loop Until .EOF
End If
Next i
Next j
End With
Set rst = Nothing
End Sub
I receive the following error message. I am trying to figure out how to get the loop to fill my date references in the sql.
Run-time error '3075':
Syntax error in date in query expression '(((tblWeather30DayMoving.NEW)- strTerritory ) AND ((tblWeather30DayMoving.RptdDate) Between #0/1/0# And #0/28/0#)'.
Any idea how to pass i and j to the sql statement instead of the 0's that are currently showing?