愚弄变量和查询

Fooling with variables and querys

我正在尽最大努力将我的所有代码划分开来,以使其完全灵活。但是,当我尝试 运行 代码时,我 运行 遇到了一个问题。它是这样的 点击您输入的内容正在生成:无效的外部程序。

在我的程序的顶部,我有一些如下所示的变量:

Public Sub varHolder() 'this
    Dim monday As String
Dim tuesday As String
Dim wednesday As String
Dim thursday As String
Dim friday As String
Dim day As String
Dim stepQuery As String
Dim i As Integer

Dim db As DAO.Database
Dim rsStepCalendar As DAO.Recordset
Set db = CurrentDb
Set rsStepCalendar = stepQuery
end sub

我程序的下一部分开始用值填充这些变量。

Private Sub btnNewContact_Click()

call varHolder 'this
Dim header As Integer

header = Forms!frmContactsEdit!txtHeader.Value

If chkActive = True Then
    stepQuery = db.OpenRecordset("Select * from tblStepCalendar " & _
                                            "Where (HeaderID = '" & header & "' ) " & _
                                            "AND (Cancel = False)" & _
                                            "AND (Active = True)", dbOpenDynaset)
    monday = chkMonA.Value
    tuesday = chkTuesA.Value
    wednesday = chkWedA.Value
    thursday = chkThursA.Value
    friday = chkFriA.Value
    day = lstActive.Selected(i)

    Call stepUpdater

End If

If chkRetiree = True Then
    stepQuery = db.OpenRecordset("Select * from tblStepCalendar " & _
                                            "Where (HeaderID = '" & header & "' ) " & _
                                            "AND (Cancel = False)" & _
                                            "AND (Retiree = True)", dbOpenDynaset)
    monday = chkMonB.Value
    tuesday = chkTuesB.Value
    wednesday = chkWedB.Value
    thursday = chkThursB.Value
    friday = chkFriB.Value
    day = lstRetiree.Selected(i)

    Call stepUpdater

End If

If chkCobra = True Then
    stepQuery = db.OpenRecordset("Select * from tblStepCalendar " & _
                                            "Where (HeaderID = '" & header & "' ) " & _
                                            "AND (Cancel = False)" & _
                                            "AND (Cobra = True)", dbOpenDynaset)
    monday = chkMonC.Value
    tuesday = chkTuesC.Value
    wednesday = chkWedC.Value
    thursday = chkThursC.Value
    friday = chkFriC.Value
    day = lstCobra.Selected(i)

    Call stepUpdater

End If
End Sub

在我使用适当的变量执行代码之后。

Public Sub stepUpdater()

call varHolder 'this
If rsStepCalendar.EOF Then

    RstRecSet.Add
    rsStepCalendar("Monday").Value = monday
    rsStepCalendar("Tuesday").Value = tuesday
    rsStepCalendar("Wednesday").Value = wednesday
    rsStepCalendar("Thursday").Value = thursday
    rsStepCalendar("Friday").Value = friday
    RstRecSet.Update

    For i = 0 To 32
        If day <> rsStepCalendar(i).Value Then
        RstRecSet.Add
            rsStepCalendar(i).Value = rsStepCalendar(i).Value
        RstRecSet.Update
        End If
   Next

    MsgBox ("Record Added")

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

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

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

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

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

For i = 0 To 32

        If day <> rsStepCalendar(i).Value Then
            RstRecSet.Edit
                rsStepCalendar(i).Value = rsStepCalendar(i).Value
            RstRecSet.Update
        End If
Next
End If

End Sub

我的问题是我是否做了我不应该做的事情?错误是否源于我尝试使用所有这些功能?我得到的错误的问题是我可以调试所以我对我做错了什么一无所知。

编辑:

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

Sub Initialize()
    Set db = CurrentDb
    Set rsStepCalendar = stepQuery
End Sub

at the top of my program I have a few variables that look like this:

在这种情况下,您的错误是由于在 SubFunction 块之外分配了 变量

Dim db As DAO.Database
Dim rsStepCalendar As DAO.Recordset
' Can't do assignments outside of a Sub or Function.
--> Set db = CurrentDb
--> Set rsStepCalendar = stepQuery

全局作用域声明区只能赋Const个值。

要修复您的错误,请将 Set 行移动到 SubFunction 代码块中:

Dim monday As String
Dim tuesday As String
Dim wednesday As String
Dim thursday As String
Dim friday As String
Dim day As String
Dim stepQuery As String
Dim i As Integer

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

' Call this sub once to set the variable values.
Sub Initialize()
    Set db = CurrentDb
    ' This wouldn't work because stepQuery is a string.
    ' Only included here to show assignment should be outside global declaration area.
    Set rsStepCalendar = stepQuery
end sub