我创建了一个输入框来获取输入的用户名但仍然使用取消按钮 Private Sub Form_Load()fsUserName = UCase(InputBox("Please Enter your name.", "User Name", _"dc"))If fsUserName = "" ThenMsgBox "No name Entered." Chr(13) Chr
Private Sub Form_Load() fsUserName = UCase(InputBox("Please Enter your name.", "User Name", _ "dc")) If fsUserName = "" Then MsgBox "No name Entered." & Chr(13) & Chr(13) & _ "You must enter a name.", vbExclamation, "ERROR" Form_Load ElseIf VarType(fsUserName) = 0 Then 'If cancel clicked cmdQuit_Click End If
还有一种方法,当单击表单上的X按钮时,它执行cmdQuit_Click,这样如果用户单击命令按钮Quit或X,则运行Quit脚本.在退出脚本中有消息框和清理.
您可以使用StrPtr()来检测是否单击了取消;Dim fsUserName As String fsUserName = InputBox("Please Enter your name.", "User Name", "dc") If (StrPtr(fsUserName) = 0&) Then MsgBox "Cancelled or X clicked" ElseIf fsUserName = "" Then MsgBox "No name Entered." & vbCr & "You must enter a name.", vbExclamation, "ERROR" Else fsUserName = UCase$(fsUserName) MsgBox fsUserName End If
如果要在表单卸载时执行某些操作,可以使用Form_Unload事件或更好的Form_QueryUnload事件,该事件在实际卸载之前触发.允许你取消它.
它还会告诉您表单卸载的原因(如果单击红色X,则UnloadMode将为0)
使用“卸载我”将引发两个事件.
编辑:在Form_Load中调用Form_Load会最终填满堆栈,最好使用循环来查找缺少的用户名.