Solved

MS-Access 2003 Cursor Control

Posted on 2009-04-05
9
487 Views
Last Modified: 2013-12-20
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
Comment
Question by:JoeWaynick
  • 5
  • 2
  • 2
9 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 24073725
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
 

Author Comment

by:JoeWaynick
ID: 24073831
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24073835
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
 

Author Comment

by:JoeWaynick
ID: 24073936
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:JoeWaynick
ID: 24073954
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
 
LVL 5

Assisted Solution

by:Mike77
Mike77 earned 250 total points
ID: 24076811
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
 

Author Comment

by:JoeWaynick
ID: 24079745
Hello Mike;

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

Joe
0
 
LVL 5

Assisted Solution

by:Mike77
Mike77 earned 250 total points
ID: 24080103
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
 

Author Comment

by:JoeWaynick
ID: 24083408
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now