Select Case中的TempVars

时间:2012-11-20 20:12:25

标签: vba ms-access ms-access-2007 access-vba

我正在尝试使用Select Case来评估TempVar字符串值,但案例并未给出正确的结果。

到目前为止我的VBA代码:

Private Sub Form_Load()

    Select Case TempVars!CurrentStatus

        Case Entered
            'code to disable some controls

        Case In Progress
            'code to disable some controls

        Case Ready to Approve
            'code to disable some controls

        Case Approved
            'code to disable some controls

        Case Else
            Call MsgBox("Case statement didn't catch", vbInformation, "Tempvar test for case statements")

    End Select
End Sub

TempVar以另一种形式设置如下:

Dim StrUserInput1 As String

TempVars("CurrentStatus").Value = DLookup("Status", "Control", "Status = '" & StrUserInput1 & "'")

'This is set by comparing the user input vs allowed status (for reports which is yet to be implemented)

我已尝试将Case设置为Case "Entered",但它始终选择第一个案例,而不是正确的案例。如果格式如上所示,则结果为Case Else

任何人都可以提供一些有关我为何或如何得不到正确结果的见解?

1 个答案:

答案 0 :(得分:4)

我很惊讶代码甚至运行并且不会抛出错误。你需要在引号中包含字符串文字,如下所示:

Select Case TempVars!CurrentStatus

    Case "Entered"
        code to disable some controls

    Case "In Progress"
        code to disable some controls

    Case "Ready to Approve"
        'code to disable some controls

    Case "Approved"
        'code to disable some controls

    Case Else

        Call MsgBox("Case statement didn't catch", vbInformation, "Tempvar test for case statements")

End Select

如果这不起作用,请在Select Case TempVars!CurrentStatus上放置一个断点,以查看TempVars!CurrentStatus在输入案例表达式时的值。