Solved

MS-Access 2003 Cursor Control

Posted on 2009-04-05
9
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

739 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