我正在尝试运行附加查询以根据 sql 语句中参数的前 30 条记录更新表。所有数据都驻留在 Access 2010 数据库中,我想根据表单上的按钮运行查询。
我是 vba 的新手,并根据帖子组装了以下代码。
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
我收到以下错误消息。我试图弄清楚如何让循环在 sql 中填充我的日期引用。
运行时错误“3075”:
查询表达式 '(((tblWeather30DayMoving.NEW)- strTerritory) AND ((tblWeather30DayMoving.RptdDate) 在 #0/1/0# 和 #0/28/0#) 之间的日期语法错误。
知道如何将 i 和 j 传递给 sql 语句而不是当前显示的 0 吗?