0

我正在尝试运行附加查询以根据 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 吗?

4

2 回答 2

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 字符串,然后在执行之前将它们替换为循环中的适当数字。不是最有效的,但我发现它简单易读。

于 2013-08-14T15:44:54.987 回答
0

您正在strSQL循环之外设置字符串。

此时i和的值为j0。

您需要将值分配给第二个循环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
于 2013-08-14T15:11:21.117 回答