Control Format

G

Grigby

Hi,

I have a form, with a serial number field with the
following code:

Private Sub SN_BeforeUpdate(Cancel As Integer)
Dim lngLen As Long
Dim strMsg As String

lngLen = Len(Nz(Me.SN, ""))
If lngLen < 6 Or lngLen > 25 Then
Cancel = True
strMsg = "SN must be 6 to 25 characters." &
vbCrLf & _
"Correct the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, "Invalid entry"
End If
End Sub

As part of this I also need to include a restriction on
the use of "-, /, \, +, * etc... In short the field can
only contain numbers and letters. I reviewed some
material similar to... If Not me!{SN} Like, but then
noted the number of potential variables. Can the code
above be modified to meet the restriction on the dash,
plus, slash, etc... Any guidance, code change, reading
material etc... would be appreciated ...

Thanks,
Greg
 
M

Marshall Barton

Grigby said:
I have a form, with a serial number field with the
following code:

Private Sub SN_BeforeUpdate(Cancel As Integer)
Dim lngLen As Long
Dim strMsg As String

lngLen = Len(Nz(Me.SN, ""))
If lngLen < 6 Or lngLen > 25 Then
Cancel = True
strMsg = "SN must be 6 to 25 characters." &
vbCrLf & _
"Correct the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, "Invalid entry"
End If
End Sub

As part of this I also need to include a restriction on
the use of "-, /, \, +, * etc... In short the field can
only contain numbers and letters. I reviewed some
material similar to... If Not me!{SN} Like, but then
noted the number of potential variables. Can the code
above be modified to meet the restriction on the dash,
plus, slash, etc...

Don't know what you mean by too many variables. This is the
way I would do it:

If lngLen<6 Or lngLe >25 Or Me.SN Like "*[!0-9a-z]*" Then

The bang in the square brackets tells Like to look for any
character not in the following list. See Wildcard
Characters in Help for details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top