Private Sub textboxA_AfterUpdate()
..... blah blah codes here ....
textboxA.Value = "" textboxA.SetFocus'I notice the cursor show the focus to the object in the next tab index
End Sub
Private Sub textboxA_BeforeUpdate(Cancel As Integer)
If Trim(textboxA.Text) = "" Then
Cancel = True
MsgBox "Lot No is a compulsory field.", vbInformation, "Lot No"
End If
End Sub
If I understand your code correctly, you are trying to maintain the focus in the same control by using the AfterUpdate event.
To do this you will need to first set the focus to another control before setting the focus back on your initial control. Try something like;
Code:
Private Sub textboxA_AfterUpdate()
..... blah blah codes here ....
textboxA.Value = ""
[B][COLOR="Purple"]Me.AnyOtherControl.SetFocus[/COLOR][/B]
Me.textboxA.SetFocus
End Sub
If the user is tabbing through the controls on the form sequentially what you can do is use the GotFocus of the control that receives the focus after the one in question.
Then use this to check the contents of the previous control
Sub GotFocus
If IsNull(Me.LotNo) Then
Me.LotNo.SetFocus
End If
An alternative to set focus is DoCmd.GoToControl <<ControlName>>
John's approach is correct if you have to reset focus from AfterUpdate, but I have to tell you you're making a major blunder here.
You're running Validation code to insure that a control actually has data in it, but you're trying to run it off of the BeforeUpdate/AfterUpdate events of the control, which has two fatal mistakes!
If the user tabs/clicks into the textbox and nothing is entered, neither of these events will fire! So this type of code here is absolutely useless! The second problem is that the user may simply not move into the textbox at all! So the code is, once again, useless!
Code to insure that a field has data has to be done in the Form_BeforeUpdate event.
The BeforeUpdate event of a control should only be used to insure that if data is entered, it falls within certain parameters, when the data has to be, for instance, all numeric, or within a certain range, etc.
Also, you need to drop the Text off of the control name. Leave it TextboxA or TextboxA.Value, as John showed you.
Lastly, you need to check for Null value instead of simply a zero length string, with IsNull(Me.TextboxA) or something similar.
If I understand your code correctly, you are trying to maintain the focus in the same control by using the AfterUpdate event.
To do this you will need to first set the focus to another control before setting the focus back on your initial control. Try something like;
Code:
Private Sub textboxA_AfterUpdate()
..... blah blah codes here ....
textboxA.Value = ""
[B][COLOR=Purple]Me.AnyOtherControl.SetFocus[/COLOR][/B]
Me.textboxA.SetFocus
End Sub
John's approach is correct if you have to reset focus from AfterUpdate, but I have to tell you you're making a major blunder here.
You're running Validation code to insure that a control actually has data in it, but you're trying to run it off of the BeforeUpdate/AfterUpdate events of the control, which has two fatal mistakes!
If the user tabs/clicks into the textbox and nothing is entered, neither of these events will fire! So this type of code here is absolutely useless! The second problem is that the user may simply not move into the textbox at all! So the code is, once again, useless!
Code to insure that a field has data has to be done in the Form_BeforeUpdate event.
The BeforeUpdate event of a control should only be used to insure that if data is entered, it falls within certain parameters, when the data has to be, for instance, all numeric, or within a certain range, etc.
Also, you need to drop the Text off of the control name. Leave it TextboxA or TextboxA.Value, as John showed you.
Lastly, you need to check for Null value instead of simply a zero length string, with IsNull(Me.TextboxA) or something similar.
Are you saying adding extra event (as below) to the previous codes ?
Private Sub Form_BeforeUpdate(Cancel As Integer) If Trim(textboxA.Text) = "" Then
Cancel = True
MsgBox "Lot No is a compulsory field.", vbInformation, "Lot No"
End If
End Sub
If you look at my post again, you'll see that I said
"you need to check for Null value instead of simply a zero length string, with IsNull(Me.TextboxA) or something similar."
If Trim(textboxA.Text) = ""
will not detect a control where no data has been entered. If nothing has been entered into the field, or if the data there has been deleted, the control is Null, and this is what you have to check for.
And no, you don't add the code to the Form_BeforeUpdate event in addition to your code, you use it in place of your previous code.
1) I notice if the cursor is moved away from the textBoxA, the event textBoxA_AfterUpdate will be fired ONLY. It means the event textBoxA_BeforeUpdate is NOT fired although the textBoxA is null or empty. I have amended my code as following
Private Sub textBoxA_BeforeUpdate(Cancel As Integer)
If IsNull(textBoxA.Text) Then
Cancel = True
MsgBox "Lot No is a compulsory field.", vbInformation, "Lot No"
End If
End Sub
Anything else missing from the code or any other events I need to include in ?
2) If the textBoxA contains data, but if I move away my cursor to other controls to do other data entry first, the textBoxA_AfterUpdate is fired which is NOT my expectation.
In the VB or VB.Net, I will apply the textBoxA_KeyDown event and KeyCode=13 to resolve the problem. However, I find this doesn't work out in the VBA. Again, how can I resolve the problem textBoxA_AfterUpdate and case (1) at the same time ? Any possibility to use the textBoxA_Enter ?
Private Sub textBoxA_BeforeUpdate(Cancel As Integer)
If IsNull(textBoxA.Text) Then
Cancel = True
MsgBox "Lot No is a compulsory field.", vbInformation, "Lot No"
End If
End Sub
I'm sorry, I'm not trying to be rude, but is English not your first language? You repeatedly ignore what you've been told and go about your merry way, and then ask why your merry way doesn't work!
Code to insure that a field has data has to be done in the Form_BeforeUpdate event...Also, you need to drop the Text off of the control name. Leave it TextboxA or TextboxA.Value, as John showed you
Which part of this do you not understand? The code cannot be in the textBoxA_BeforeUpdate event! It has to be in the Form_BeforeUpdate event! You cannot use the Text property in this manner, in Access VBA, you have to use the Value property!
2) If the textBoxA contains data, but if I move away my cursor to other controls to do other data entry first, the textBoxA_AfterUpdate is fired which is NOT my expectation.
Once again, the textBoxA_BeforeUpdate and textBoxA_AfterUpdate events will be fired if, and only if, data in the textbox has been entered or edited, regardless of what your expectations are!
This is possibly the most important thing that will be said here, so listen closely!
You are not working in Visual Basic here, nor are you working in VB.Net! You need to basically forget anything you know of these languages while you're working in Access VBA! All of these languages have a common ancestor that they were derived from, QuickBasic 4.5, but they have all taken different paths from that time!
Things work differently in Access VBA. These languages share many functions and properties that are the same, but they also share many functions/properties that share the same names, but that work entirely differently! Your use of
textBoxA.Text
is a prime example! In VB this refers to the value held by a control. In Access VBA, this only refers to the data held by a control, while that control has the focus, which may or may not be the value held by the field that is bound to the control! In Access VBA, you have to use the Value property, instead of the Text property, to refer to the data held by the field bound to the control. And this is just one of many, many differences!
I'm not trying to be mean here, I'm really not, but if you want us to help, you have to read and pay attention to what we tell you! You simply cannot half way listen to our advice, continue doing what you were doing before you consulted us, and expect things to work out!
Thanks for the above comment, but I don't quite like to be commented "I'm sorry, I'm not trying to be rude, but is English not your first language?" . You need to understand my VB and VB.Net programming backgound as explained in the earlier post and nothing to do with the LANGUAGE !!!
However, I'd like to appreciate the spirit in assisting me, my comment to the MS Access is it require a super long stupid events driven. Consequently, I have done several trial and error and finally find my own programming way which is more simple and quite close to my existing programming way in the VB and VB.Net.
Program just requires one private boolean variable and 2 textbox events (which is BeforeUpdate and KeyDown) and everything work fine as expected.