0

我正在尝试使用 aSelect 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)

我试过设置CaseasCase "Entered"但它总是选择第一种情况,而不是正确的情况。如果它的格式如上所示,结果是Case Else

谁能提供一些关于我为什么或如何没有得到正确结果的见解?

4

1 回答 1

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它进入 case 表达式时的值。

于 2012-11-20T20:16:40.027 回答