如果僅僅是要移到密碼的測試,創建一個Function
返回一個Boolean
:
Function PasswordOK(strPwd As String) As Boolean
PasswordOK = strPwd = Format(Now, "Hh") * 2
End Function
,然後你可以使用它作爲:
If PasswordOK(strPasswd) Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
'Exit Sub '<-- this isn't needed, because the next
' statement after this one is also Exit Sub
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
或者,如果合適,您可以通過傳遞更多參數將更多代碼移動到常規例程中:
Sub ChangeForm(oldForm As String, newForm As String)
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
DoCmd.Close acForm, oldForm, acSaveYes
DoCmd.OpenForm newForm, acNormal, "", "", acEdit, acNormal
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
End Sub
,並用它作爲
Private Sub btnStatistics_click()
ChangeForm "frmCustomer", "frmStatistics"
End Sub
或許兩者之間的某個地方,把密碼的只是輸入,它的測試,進入普通程序:
Function PasswordOK() As Boolean
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
PasswordOK = True
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
PasswordOK = False
End If
End Function
並用它作爲
Private Sub btnStatistics_click()
On Error GoTo Err_Handler
If PasswordOK() Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
End If
Exit_This_Sub:
Exit Sub
Err_Handler:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_This_Sub
End Sub
Th謝謝你。我認爲第一個是確定的。因爲每次運行不同的命令。 – YvetteLee
對不起。我測試了它,它工作正常。我是否必須離開'Dim strPasswd As String'和'strPasswd = InputBox(「請輸入密碼」,「需要密碼」)? – YvetteLee
@YvetteLee如果您使用的是第一種方法(只有在函數中完成密碼測試),那麼您仍然需要在主代碼中輸入密碼。我會更新我的答案,添加第三種方法來完成輸入和測試,但不是其他方法。 – YowE3K