Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Letter vs Characters

Status
Not open for further replies.

MoulayKool

Programmer
Mar 9, 2006
19
0
0
US
Hi,
Can anyone help me Please
I need to make a text box allow the user to enter member id like this:

if the member id starts with a number then id lenght must equal 8. Example 16466851

If the id starts with a letter then id lenght must equal 11. Example JYW37125T01

Thanks in advance

Moulay

 
In the BeforeUpdate event procedure of the text box:
If Len(Me![text box]) <> IIf(Me![text box] Like "#*", 8, 11) Then
Cancel = True
Exit Sub
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

try something like (untested code)

Code:
if left(inputstring,1) <= "9" and len(inputstring) = 8 then
do something
else
msgbox "8 characters are required for your ID" & VBCRLF & "Please correct or re-enter your ID"
exit
end if

if left(inputstring,1) > "9" and len(inputstring) = 11 then
do something
else
msgbox "11 characters are required for your ID" & VBCRLF & "Please correct or re-enter your ID"
exit
end if

Not the most elegant of code but working on these line may get what you want.



Ian Mayor (UK)
Program Error
Programming is 10% coding and 90% error checking.
 
Just noticed PHV's coding, try that first.

Ian Mayor (UK)
Program Error
Programming is 10% coding and 90% error checking.
 
Thanks
It did not work for me. When I try to enter an id the text box allows me to enter as many as I want..
 
Any chance you could post your actual validation code from the Sub line to the End Sub ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
This is what I put:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me![Member ID#]) <> IIf(Me![Member ID#] Like "#*", 8, 11) Then
Cancel = True
Exit Sub
End If
End Sub
 
I said you to use the event procedure of the text box, not the form.


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I realized that I put the code in Form_BeforeUpdate, I changed to:

Private Sub MemberID_BeforeUpdate(Cancel As Integer)
If Len(Me![MemberID]) <> IIf(Me![MemberID] Like "#*", 8, 11) Then
Cancel = True
Exit Sub
End If
End Sub

But still not working, not even giving me an error message.
 
How are ya MoulayKool . . .

Here's a routine that not only [blue]validates & tunricates length proper[/blue], but [blue]only allows letter/number[/blue] data entry. Copy/paste the routine to the KeyPress event of MemberID (don't forget to disable the BeforeUpdate event for testing):
Code:
[blue]   Dim Txt As Property
   
   Set Txt = Me!Txt1.Properties("Text")
   
   If (Chr(KeyAscii) Like "[A-Za-z0-9]") Then
      If Len(Txt) = IIf(Txt Like "#*", 8, 11) Then
         KeyAscii = 0
      End If
   ElseIf (Chr(KeyAscii) Like "[ -/:-@[-~]") Then
         KeyAscii = 0
   End If
   
   Set Txt = Nothing[/blue]

Calvin.gif
See Ya! . . . . . .
 
MoulayKool . . . Woops
Code:
[blue]Set Txt = Me!Txt1.Properties("Text")
   Should be:
Set Txt = Me![purple][b][YourTextboxName][/b][/purple].Properties("Text")[/blue]

Calvin.gif
See Ya! . . . . . .
 
thanks so much for your idea but it's sitll not working. This is what I put:

Private Sub Text53_BeforeUpdate(Cancel As Integer)

Dim Txt As Property

Set Txt = Me![Text53].Properties("Text")

If (Chr(KeyAscii) Like "[A-Za-z0-9]") Then
If Len(Txt) = IIf(Txt Like "#*", 8, 11) Then
KeyAscii = 0
End If
ElseIf (Chr(KeyAscii) Like "[ -/:-@[-~]") Then
KeyAscii = 0
Ectnd If

End Sub

I hope my question was clear the first time I posted it.
what I am trying to do is make sure the user doesn't enter extra numbers or letters in the input box. there are 2 different id's that needs to be entered.

let say for example the user is trying to enter this id in the text box 12345678
Once the user enter 1 he should only be allowed to enter no less or more 8 characters because the first characther is a number.

Now let say the user is trying to enter this id P1234567890
Once the user enter P the he should only be allowed to enter no less or more 11 characters because the first characther is a letter.

In other words if the first character the user enters is a number then the text box should only allow 8 characters to be entered (No less and no more)
if the first character the user enters is a letter then the text box should only allow 11 characters to be entered (No less and no more)

Thanks sooo much again



 
Roger That MoulayKool . . .

The code given by me only turnicates the proper length. [blue]PHV's[/blue] code in he [blue]BeforeUpdate[/blue] event with the added message below, should fullfill yur needs.
Code:
[blue]   Dim Msg As String, Style As Integer, Title As String
   Dim txtLen As Integer, DL As String
   
   txtLen = IIf(Me![[purple][b]YourTextboxName[/b][/purple]] Like "#*", 8, 11)
   DL = vbNewLine & vbNewLine
   
   If Me!![[purple][b]YourTextboxName[/b][/purple]] <> txtLen Then
      Msg = "Data Input must be " & _
            txtLen & " Characters in Length! " & DL & _
           "Go back and make it so!"
      Style = vbInformation + vbOKOnly
      Title = "Character Length Error!"
      MsgBox Msg, Style, Title
     Cancel = True
   End If[/blue]
If this doesn't work . . . then there's something you han'nt told us! . . .

Calvin.gif
See Ya! . . . . . .
 
Hi All,
Yes, all the above codes work. The reason I was having trouble is: I am looking for the text box to only allow 8 or 11 character. in other words once the user reach 8 then the text box should not allow him to enter anymore. And the same goes for 11 characters.
Your code allows the user to enter as many character as he desires but gets a message to fix it if he tries to do anything else.
I am so sorry that it took this long to solve. I really appreciate your help and patient.

MoulayKool
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top