Text box validation

coryt

Registered User.
Local time
Today, 12:57
Joined
Jun 13, 2003
Messages
82
I know this is probably simple, but I can seem to get it to work right.

The user scanns a barcode into a text box. I need a message box to appear if the number of characters in the text box is less then 15 characters (ie. 8888 instead of 888888888888888). Here is my code. I can't see what is wrong.

(my text box)_AfterUpdate

if me.mytextbox <> "***************" then
bytYesNo = msgbox ("BlaBlaBla", vbYesNo, "Bla Bla")
if bytYesNo = vbYes then
Do this
else
do this
end if

else
do this

end if
end sub


Thanks in advance
 
Works great!

Now I have another issue. When the message box appears it asks the user whether or not they want to continue. If they choose yes, the msgbox closes and the focus is set to the next field. If they choose no, I want the focus to move back to the field the scanned the barcode into, but it doesn't. It moved to the next field.

here is my code.


Private Sub txtMPShipCode_AfterUpdate()
If Len(Me.txtMPShipCode) < 15 And Len(Me.txtMPShipCode) <> 5 Then
bytYesNo = MsgBox("MP SHip Code is not correct." & vbCrLf & _
"Would you like to continue?", vbYesNo, "Incorrect MP Ship Code")

If bytYesNo = vbNo Then
Me.txtMPShipCode.SetFocus 'This is not working!!!!!!
Me.txtMPShipCode = ""

End If
End If
End Sub


txtMPShipCode is the field the user scanns the barcode into
 
Try this...
Code:
Private Sub txtMPShipCode_AfterUpdate()
    If Len(Me.txtMPShipCode) < 15 And Len(Me.txtMPShipCode) <> 5 Then
        If MsgBox("MP SHip Code is not correct." & vbCrLf & "Would you like to continue?", vbYesNo, "Incorrect MP Ship Code") = vbNo Then
            Me.txtMPShipCode.SetFocus
            Me.txtMPShipCode = ""
        Else
            MsgBox "User clicked No." 'for testing purposes
    End If
End Sub
HTH
 
I changed my code around and got it to work.

FYI, I place the code in the before update command. this is what it looks like now.

Private Sub txtMPShipCode_BeforeUpdate(Cancel As Integer)
If Len(Me.txtMPShipCode) < 15 And Len(Me.txtMPShipCode) <> 5 Then
bytYesNo = MsgBox("MP SHip Code is not correct." & vbCrLf & _
"Would you like to continue?", vbYesNo, "Incorrect MP Ship Code")

If bytYesNo = vbNo Then
Cancel = True
Me.txtMPShipCode.DefaultValue = ""

End If
End If
End Sub

I tried to put Me.txtMPShipCode = "", but I kept getting a message saying it couldnt save data because of the before update command bla bla bla.... So Me.txtMPShipCode.DefaultValue = "" is the only way I could get it so the user didn't have to delete the data before they rescanned the barcode.

Thanks for your help.
 

Users who are viewing this thread

Back
Top Bottom