Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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