QueryClose event of a form triggering Exit event of a textbox on that form

N

noname

Hi,

I am stuck with a particular problem.
I have a form which has textboxes placed in a frame, which is placed
on 1st page of a multipage.

Whenever, i click the Red "X" on the form to unload the form, it
triggers the Exit event of the 1st Textbox on the form.

i have the following code in the Exit event of the Textbox. so it
triggers and runs that code, resulting in a message box. this
messagebox appears only after the form has unloaded. So its kind of
frustrating for me to track why its getting triggered by the
QueryClose Event of the form.

Form_QueryClose event code:
------------------------------------------
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)

If CloseMode = vbFormControlMenu Then

Cancel = False

Unload Me

frmCustomMsgBoxCross.Show

End If

End Sub



Textbox_Exit event code:
-----------------------------------

Private Sub txt_JobNo_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If txt_JobNo.Text = vbNullString Then

MsgBox "Please enter a Job No!", vbExclamation + vbOKOnly,
"Enter Job No"

Cancel = True

txt_JobNo.BackColor = RGB(255, 255, 0)

Else

txt_JobNo.BackColor = RGB(255, 255, 255)

End If

End Sub


Any ideas why its happening?
 
D

Dave Peterson

First, I would drop the msgbox and use a label instead. Then the label could be
updated with your warning message, but it would be gone before the user could
even see it.

If that doesn't work for you, then maybe you could add a module level boolean
variable and use that. Check that variable to see if the procedure should be
continued or just stopped.

Option Explicit
Dim BlkProc As Boolean
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
BlkProc = True
Cancel = False
Unload Me
frmCustomMsgBoxCross.Show
End If
End Sub
Private Sub txt_JobNo_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If BlkProc = True Then
Exit Sub
End If
If txt_JobNo.Text = vbNullString Then
MsgBox "Please enter a Job No!", _
vbExclamation + vbOKOnly, "Enter Job No"
Cancel = True
txt_JobNo.BackColor = RGB(255, 255, 0)
Else
txt_JobNo.BackColor = RGB(255, 255, 255)
End If
End Sub

As a user, I'd rather see the label than msgboxes.
 
N

noname

First, I would drop the msgbox and use a label instead.  Then the labelcould be
updated with your warning message, but it would be gone before the user could
even see it.

If that doesn't work for you, then maybe you could add a module level boolean
variable and use that.  Check that variable to see if the procedure should be
continued or just stopped.

Option Explicit
Dim BlkProc As Boolean
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
     If CloseMode = vbFormControlMenu Then
         BlkProc = True
         Cancel = False
         Unload Me
         frmCustomMsgBoxCross.Show
     End If
End Sub
Private Sub txt_JobNo_Exit(ByVal Cancel As MSForms.ReturnBoolean)
     If BlkProc = True Then
         Exit Sub
     End If
     If txt_JobNo.Text = vbNullString Then
         MsgBox "Please enter a Job No!", _
             vbExclamation + vbOKOnly, "Enter Job No"
         Cancel = True
         txt_JobNo.BackColor = RGB(255, 255, 0)
     Else
         txt_JobNo.BackColor = RGB(255, 255, 255)
     End If
End Sub

As a user, I'd rather see the label than msgboxes.

Thanks Dave,

I used a similar code last night... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top