2015-02-05 67 views
-1

我正在盡我所能來劃分我的所有代碼,因此它是完全靈活的。然而,當我嘗試運行代碼時,我遇到了一個問題。它就像這樣進入正在生產:無效的外部程序。與變量和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 
+0

'請問錯誤stem' - **什麼錯誤** – cybermonkey 2015-02-05 17:42:27

回答

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 
+0

所以我會放在一個函數?並調用我需要它的功能? – SaladSnake 2015-02-05 18:46:13

+0

@SaladSnake - 設置值的函數只需要調用一次,因爲它們的範圍是全局的。一旦設定,它們將在任何地方都可用。 – 2015-02-05 18:48:15

+0

我有點困惑,我會更新代碼頂部。讓我知道我是否有正確的想法。 – SaladSnake 2015-02-05 18:50:22