2012-11-20 56 views
0

我試圖用一個Select Case來評估TempVar字符串值,但這種情況是不給我正確的結果。TempVars的在選擇案例

我的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) 

我已經嘗試設置CaseCase "Entered",但它始終選擇第一種情況,而不是正確的。如果上面格式爲秀,結果是Case Else

任何人都可以提供關於爲什麼或如何我沒有得到正確的結果一些見解?

回答

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值是什麼,當它進入的情況下表達。

+3

將沒有錯誤編譯沒有'選項Explicit' – SeanC

+1

@SeanCheshire - 是啊,我一直以爲'選項正在使用Explicit',你看到這讓我。 – LittleBobbyTables

+0

感謝您的幫助,需要更頻繁地使用「Option Explicit」:S – Deafdan