[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

MS-Access 2003 Cursor Control

I'm programming a telephone edit in a MS-Access 2003 form field using VBA.  My operating system is XP/Pro.  Whenever invalid data is detected I display a MsgBox from the AfterUpdate event explaining the nature of the error.  However, when I click "OK" to the MsgBox the cursor advances to the next text box.

Can someone explain how I make the cursor remain in the text box with the invalid data until valid data is entered and the content successfully passes my edit check?  Here's my code:
Private Sub txtWorkPhone_AfterUpdate()
    txtWorkPhone = Trim(txtWorkPhone)
    Call Phone_Validation(txtWorkPhone)
End Sub
 
Private Sub Phone_Validation(txtPhoneNumber)
    If Left(txtPhoneNumber, 1) < 2 Then
        MsgBox ("Area Code Cannot Begin With Zero Or One")
    Else
        If Mid(txtPhoneNumber, 4, 1) < 2 Then
            MsgBox ("Telephone Prefix Cannot Begin With Zero Or One")
        End If
    End If
End Sub

Open in new window

0
JoeWaynick
Asked:
JoeWaynick
  • 5
  • 2
  • 2
3 Solutions
 
Patrick MatthewsCommented:
Hello JoeWaynick,

Instead of using the AfterUpdate event, use the BeforeUpdate event, and if your validation tests come up with a
failure, then set the Cancel argument to True.  That will cancel the update, and should keep the focus on the
control.  To be doubly sure, include a line like this:

Me.txtWorkPhone.SetFocus

Regards,

Patrick
0
 
JoeWaynickAuthor Commented:
Hello matthewspatrick;

Thanks for responding.  

What do you mean by "set the Cancel argument to True"?  I'm sorry for asking such a basic question.

Joe
0
 
Patrick MatthewsCommented:
The BeforeUpdate event sub has an argument, Cancel.  In effect, to cancel the update, you include a line
in your code like this:

      Cancel = True
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JoeWaynickAuthor Commented:
Patrick;

I tried the BeforeUpdate event and got an error on "txtWorkPhone = Trim(txtWorkPhone)" prior to the function call.  I commented out the offending statement and the called procedure doesn't recognize invalid data.  What could I be doing wrong?

Joe
0
 
JoeWaynickAuthor Commented:
Patrick;

I found my typo. The BeforeUpdate event is working as you said.  Thank you!.  Now the cursor remains in the original textbox, but at the end of the field.  Is there a way I can make it go to the beginning of the field?
0
 
Mike77Commented:
the SelStart property is for that :

Me.txtWorkPhone.SelStart=0
Or if using a variable that contains the name of your control :
Me.Controls(StrControlName).SelStart = 0
Which gives :
Me.Controls("txtWorkPhone").SelStart = 0
0
 
JoeWaynickAuthor Commented:
Hello Mike;

Thank you for your input.  Can I also clear the textbox at the same time?

Joe
0
 
Mike77Commented:
If you want to clear the textbox, you'll have to move your code back to the afterupdate event, as clearing it will fire the beforeupdate event and generate an error as you cannot fire the beforeupdate event from there. You could select all the text like this though :
        Me.txtWorkPhone.SelStart = 0
        Me.txtWorkPhone.SelLength = Len(Me.txtWorkPhone)
0
 
JoeWaynickAuthor Commented:
Mike;

Your're awesome.  I'm going to increase the points to 500 and give half to you and half to Patrick.  Thank you for sticking with me.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now