W
WLMPilot
I posted a question last month for a problem I was having with Setfocus. The
answer I got was something I had not seen before. I am teaching myself VBA
as I go along. I would appreciate it if someone could explain what the code
is doing so I can better understand it.
The problem I was having is when a user would enter an incorrect value in a
textbox, I would display a msg for incorrect entry. I would then execute the
following:
Textbox1 = ""
Textbox1.Setfocus 'to allow user to reenter correct value.
Problem is that the next textbox on userform received the setfocus. If you
need to see my code, let me know. Below is the answer I received.
***THIS IS RESPONSE TO MY QUESTION****
Enter Event of any Control Object occurs before a control actually receives
the focus from a control on the same form. Exit occurs immediately before a
control loses the focus to another control on the same form.
So a possible solution to your problem might be something like this:
Place all code behind your form with this line “Dim MyCancel As Boolean†at
the top outside any procedure.
Dim MyCancel As Boolean
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = MyCancel
MyCancel = False
End Sub
Replace the code Textbox1="" and Textbox1.Setfocus with:
MyCancel = True
TextBox1 = vbNullString
My questions concerning the new code I was given is this:
1) I did not know you could place code outside a subroutine. In this case,
Dim MyCancel As Boolean is by itself. What does this do?
2) What is the macro for Cancel = Mycancel doing?
3) What is being triggered with MyCancel = True?
4) Isn't Textbox1 = "" the same as Textbox1 = vbNullString?
I appreciate any help in explaning this code and what is going on.
Thanks,
Les
answer I got was something I had not seen before. I am teaching myself VBA
as I go along. I would appreciate it if someone could explain what the code
is doing so I can better understand it.
The problem I was having is when a user would enter an incorrect value in a
textbox, I would display a msg for incorrect entry. I would then execute the
following:
Textbox1 = ""
Textbox1.Setfocus 'to allow user to reenter correct value.
Problem is that the next textbox on userform received the setfocus. If you
need to see my code, let me know. Below is the answer I received.
***THIS IS RESPONSE TO MY QUESTION****
Enter Event of any Control Object occurs before a control actually receives
the focus from a control on the same form. Exit occurs immediately before a
control loses the focus to another control on the same form.
So a possible solution to your problem might be something like this:
Place all code behind your form with this line “Dim MyCancel As Boolean†at
the top outside any procedure.
Dim MyCancel As Boolean
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = MyCancel
MyCancel = False
End Sub
Replace the code Textbox1="" and Textbox1.Setfocus with:
MyCancel = True
TextBox1 = vbNullString
My questions concerning the new code I was given is this:
1) I did not know you could place code outside a subroutine. In this case,
Dim MyCancel As Boolean is by itself. What does this do?
2) What is the macro for Cancel = Mycancel doing?
3) What is being triggered with MyCancel = True?
4) Isn't Textbox1 = "" the same as Textbox1 = vbNullString?
I appreciate any help in explaning this code and what is going on.
Thanks,
Les