愚弄变量和查询

时间:2015-02-05 17:40:29

标签: vba access-vba

我正在尽力划分我的所有代码,以便它完全灵活。但是,当我尝试运行代码时,我遇到了一个问题。它就是这样的On Click你输入的产生:无效的外部程序。

在我的程序的顶部,我有一些看起来像这样的变量:

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

1 个答案:

答案 0 :(得分:1)

  

在我的程序的顶部我有一些看起来像这样的变量:

在这种情况下,您的错误是由于在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