[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vba code to go to a clear the content of a text box and move cursor to that text box

Posted on 2009-12-22
7
Medium Priority
?
439 Views
Last Modified: 2013-11-28
After the user clicks the ok button, I want the contents of the of the text box named "Part" to clear and the cursor to move into that text box on my for.  Can someone please help me develop the vba code for this.....Thanks.
If MsgBox("Special Character " & Mid(Me.Part, j, 1) & " Exists.  PLEASE RE-TYPE PART NUMBER CORRECTLY!!!", , "Part Number Authentication") = vbOK Then

Open in new window

0
Comment
Question by:sxxgupta
7 Comments
 
LVL 31

Expert Comment

by:hnasr
ID: 26107064
Me!Part.SetFocus
Me!Part.Text=""
0
 

Author Comment

by:sxxgupta
ID: 26107168
This is what I have:
Right now the Part field with special characters prompts the messagebox.  The user clicks Ok and the value is cleared, but the focus moves to the next field and does not remain in the Part field.................

Private Sub Part_AfterUpdate()
Dim j As Integer
For j = 1 To 7
   If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", Mid(Me.Part, j, 1)) = 0 Then
      MsgBox "Special Character " & Mid(Me.Part, j, 1) & " Exists.  PLEASE RE-TYPE PART NUMBER CORRECTLY!!!", , "Part Number Authentication"
        Me.Part.Value = ""
        Me.Part.SetFocus
      End If
      
   End If
Next
End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 26107386
Do it in the BeforeUpdate event and Cancel that event if needed:

Private Sub Part_BeforeUpdate(CAncel as Integer)
Dim j As Integer
For j = 1 To 7
   If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", Mid(Me.Part, j, 1)) = 0 Then
      MsgBox "Special Character " & Mid(Me.Part, j, 1) & " Exists.  PLEASE RE-TYPE PART NUMBER CORRECTLY!!!", , "Part Number Authentication"
        Cancel = True
        Me.Part.Value = ""
      End If
     
   End If
Next

End Sub
0
Independent Software Vendors: 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!

 
LVL 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 26107393
Sorry, you should also use

Me.Part.Undo

instead of

Me.Part.Value = ""

Setting the Value could trigger the BeforeUpdate event again, forcing you into an endless loop.
0
 
LVL 5

Expert Comment

by:fhillyer1
ID: 26107506
following your logic you run a loop on the 7 characters for the part number
but when you find a non valid character you are still on the loop, you should exit
the loop as soon as the non valid character is found try this and let me know
also notice that you have an extra END IF there
Private Sub Part_AfterUpdate()
Dim j As Integer
For j = 1 To 7
   If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", Mid(Me.Part, j, 1)) = 0 Then
        goto Nonvalid
   End If
Next
NonValid:
      MsgBox "Special Character " & Mid(Me.Part, j, 1) & " Exists.  PLEASE RE-TYPE PART NUMBER CORRECTLY!!!", , "Part Number Authentication"
        Me.Part.Value = ""
        Me.Part.SetFocus

End Sub

Open in new window

0
 
LVL 44

Expert Comment

by:GRayL
ID: 26107956
It looks like your textbox has the same name as the field Part.  That's a sure recipe for getting lost quickly.  Try changing the name of your form controls by prefixing the field name with an abbreviation representing the control type - in your case, txtPart.  Other examples are lblPart, lbxPart, cboPart, etc.
0
 

Author Closing Comment

by:sxxgupta
ID: 31669188
Thank you very much LSMC1!.
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.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

867 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