在编译器满足查询的代码中出现问题

Hiccup in code where compiler meets the query

这可能是一个远景,因为我认为我没有提供足够的信息,但无论如何我都会展示一下。我收到一个编译器错误,指出变量未根据查询定义。我已经尝试了一些东西,但我不确定我做错了什么..

VBA 还是有点新,所以任何信息将不胜感激!

这是我的代码:

Dim db As DAO.Database
    Dim rsStepCalendar As DAO.Recordset

        Set rsStepCalendar = db.OpenRecordset("Select * from tblStepCalendar " & _
                                                "Where (HeaderID = '" & Forms!frmContactsEdit!txtHeader.Value & "' ) " & _
                                                "AND (Cancel = False)" & _
                                                "AND (Active = True)", dbOpenDynaset)

    If rsStepCalendar.EOF Then

        MsgBox ("Add the record")

    Else
        Dim i As Integer

        If chkMonA <> rsStepCalendar("Monday").Value Then
            RstRecSet.Edit
                    rsStepCalendar("Monday").Value = chkMonA.Value
            RstRecSet.Update
        End If

        If chkTuesA <> rsStepCalendar("Tuesday").Value Then
            RstRecSet.Edit
                    rsStepCalendar("Tuesday").Value = chkTuesA.Value
            RstRecSet.Update
        End If

        If chkWedA <> rsStepCalendar("Wednesday").Value Then
            RstRecSet.Edit
                    rsStepCalendar("Wednesday").Value = chkWedA.Value
            RstRecSet.Update
        End If

        If chkThursA <> rsStepCalendar("Thursday").Value Then
            RstRecSet.Edit
                    rsStepCalendar("Thursday").Value = chkThursA.Value
            RstRecSet.Update
        End If

        If chkFriA <> rsStepCalendar("Friday").Value Then
            RstRecSet.Edit
                    rsStepCalendar("Friday").Value = chkFriA.Value
            RstRecSet.Update
        End If

    For i = 0 To 32

            If lstActive.Selected(i) <> rsStepCalendar(i).Value Then
           '     rsStepCalendar(i).Value
            End If
    Next
    End If

你没有说你在哪里有这段代码,但作为一般规则,你错过了例程的开始和结束

例如:

Sub MyTest
  ‘ your code goes here
End Sub

接下来,您的代码使用了 DB,但您从未设置过它,所以您需要这个:

Sub MyTest()

   Dim db              As DAO.Database
   Dim rsStepCalendar  As DAO.Recordset

   Set db = CurrentDb    <--- missing this

Set rsStepCalendar = db.OpenRecordset................