Validating data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have an Access 97 db. I'm trying to validate that data entered into a
bound field does not contain any spaces. The field in the underlying table
has Required set to Yes, Zero Length set to No and the field is indexed with
duplicates allowed.
Here's the code, but it keeps giving me a 2115 error. Can anyone see what
the problem is?

Private Sub TxtBarCode_BeforeUpdate(Cancel As Integer)

'^^^Check for spaces in TxtBarCode
If InStr([TxtBarCode], " ") <> 0 Then
MsgBox "The Bar Code can't contain spaces." & vbCrLf & "Please enter it
without spaces.", vbExclamation, "Spaces found"
Cancel = True
Me!TxtBarCode.Undo
Exit Sub
End If
End Sub
 
To block spaces, try setting the Validation Rule property of the text box
to:
Not Like "* *"

Alternatively, you could discard spaces as the user types them by adding
this to the KeyPress event of the text box:
If KeyAscii = vbKeySpace Then KeyAscii = 0
That would not prevent pasting spaces in, so you might use both.
 
Back
Top