预期数组:当我尝试获取多选组合框的选定值时

时间:2015-02-06 17:17:53

标签: vba access-vba

所以我试图让我的代码尽可能灵活。所以我将我的按钮功能放在varibles中并调用另一个函数来添加逻辑。问题是,当我发现变化的那一天,我发现很难正确存储信息。我认为这是一个语法问题,我还没有理解。

If chkRetiree = True Then

    stepQuery = "Select * from tblStepCalendar " & _
                                            "Where (HeaderID = '" & header & "' ) " & _
                                            "AND (Cancel = False)" & _
                                            "AND (Retiree = True)"
    monday = chkMonR.Value
    tuesday = chkTuesR.Value
    wednesday = chkWedR.Value
    thursday = chkThursR.Value
    friday = chkFriR.Value
    day = "lstRetiree"

    Call stepUpdater
    'Call Initialize

End If

这是应该获取变量值的函数

   For i = 0 To 32
        If day(i).Selected Then 'This is where the error happens

            rsStepCalendar(i).Value = rsStepCalendar(i).Value

        End If
   Next

非常感谢先进。非常感谢任何帮助!

完整代码__________________________

Option Explicit
Option Compare Database
Dim monday As String
Dim tuesday As String
Dim wednesday As String
Dim thursday As String

Private Sub btnNewContact_Click()
    '        If lstDateWeek.Selected(i - 1) Then
    '
    '           RsStep("3" & i).Value = True

    Dim header As Integer

    header = Forms!frmContactsEdit!txtHeader.Value

    If chkActive = True Then
        stepQuery = "SELECT * FROM tblStepCalendar " & _
                    "WHERE (HeaderID = '" & header & "' ) " & _
                    "AND (Cancel = False)" & _
                    "AND (Active = True)"

        monday = chkMonA.Value
        tuesday = chkTuesA.Value
        wednesday = chkWedA.Value
        thursday = chkThursA.Value
        friday = chkFriA.Value
        day = "lstActive"

        Call stepUpdater
        'Call Initialize
    End If

    If chkRetiree = True Then
        stepQuery = "SELECT * FROM tblStepCalendar " & _
                    "WHERE (HeaderID = '" & header & "' ) " & _
                    "AND (Cancel = False)" & _
                    "AND (Retiree = True)"
        monday = chkMonR.Value
        tuesday = chkTuesR.Value
        wednesday = chkWedR.Value
        thursday = chkThursR.Value
        friday = chkFriR.Value
        day = "lstRetiree"
        Call stepUpdater
        'Call Initialize
    End If

    If chkCobra = True Then
        stepQuery = "SELECT * FROM tblStepCalendar " & _
                    "WHERE (HeaderID = '" & header & "' ) " & _
                    "AND (Cancel = False)" & _
                    "AND (Cobra = True)"
        monday = chkMonC.Value
        tuesday = chkTuesC.Value
        wednesday = chkWedC.Value
        thursday = chkThursC.Value
        friday = chkFriC.Value
        day = "lstCobra"

        Call stepUpdater
        'Call Initialize
    End If
End Sub

Dim friday As String
Dim day As String
Dim stepQuery As String
Dim i As Integer

Public Sub stepUpdater()
    'Call Initialize
    Dim db As DAO.Database
    Dim rsStepCalendar As DAO.Recordset
    Set db = CurrentDb
    Set rsStepCalendar = db.OpenRecordset((stepQuery), dbOpenDynaset)

    If rsStepCalendar.EOF Then
        rsStepCalendar.AddNew
        rsStepCalendar("Monday").Value = monday
        rsStepCalendar("Tuesday").Value = tuesday
        rsStepCalendar("Wednesday").Value = wednesday
        rsStepCalendar("Thursday").Value = thursday
        rsStepCalendar("Friday").Value = friday
        For i = 0 To 32
            If day(i).Selected Then
                rsStepCalendar(i).Value = rsStepCalendar(i).Value
            End If
        Next
        MsgBox ("Record Added")
        rsStepCalendar.Update
    Else
        If chkMonA <> rsStepCalendar("Monday").Value Then
            rsStepCalendar.Edit
            rsStepCalendar("Monday").Value = monday
            rsStepCalendar.Update
        End If

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

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

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

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

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

    Set rsStepCalendar = Nothing
End Sub

enter image description here

0 个答案:

没有答案