我正在尝试运行追加查询,以根据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吗?
答案 0 :(得分:0)
您正在循环外设置strSQL
字符串。
此时,i
和j
的值为0。
您需要为第二个循环中的strSQL
分配值:
For j = 2003 To 2013
For i = 1 To 12
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 & "#)); "
If Not (rst.EOF And rst.BOF) Then
.MoveFirst
Do
CurrentDb.Execute strSQL
.MoveNext
Loop Until .EOF
End If
Next i
Next j
答案 1 :(得分:0)
我是在记事本中做过的,没有经过测试,但这里有一个想法:
Option Compare Database
option explicit
Private Sub Command3_Click()
Dim sql As String, sql2 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"
sql = "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 #mm/01/yyyy# And #mm/28/yyyy#)); "
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
sql2 = replace(1, sql,"yyyy", cstr(j)) 'replace "jjjj" by year
sql2 = replace(1,sql2,"mm", format(i,"00")) 'replace "mm" by month
debug.print sql2
CurrentDb.Execute sql2 'this can be REM'd once it is all working
.MoveNext
Loop Until .EOF
End If
Next i
Next j
End With
Set rst = Nothing
End Sub
另请注意,您没有设置Option Explicit
,而是在strSql和Sql之间混合变量名。
我使用愚蠢的日期创建了sql字符串,然后在执行之前用循环中的相应figues替换它们。不是最有效的,但我发现它简单易读。