Solved

MS-Access 2003 Cursor Control

Posted on 2009-04-05
9
492 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

830 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