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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: Set Required # Characters on form in Access 4

Status
Not open for further replies.

RDMRDM

Programmer
Apr 9, 2007
33
US
I have a text field on my form to enter SSN. The field is for Social Security Number. Since SSN's are 9 characters long, I would like to set this field to require the person to enter 9 characters and only 9 characters in length. How would I do this using a text box on a form? Thanks.
 
If this is linked to a field in a table, you can do it at the table level.

Open your table in design view.
Go to the "General" tab that should be showing in the bottom left of the window. Find "Input Mask", and click the build button (button with 3 dots on it) to the right of that field. In the next prompt, choose "Social Security Number", and it will force the 9 digit's you're talking about.

Or if you're wanting to do it in code, you can do that with the text box's BeforeUpdate event.

Code:
Private Sub MyTextBox_BeforeUpdate(Cancel as Integer)
  If Len(MyTextBox.Text) = 9 Then
    Cancel = False
  Else
    MsgBox "Please enter the correct number of digits"
    Cancel = True
    Exit Sub
  End If

  If IsNumeric(MyTextBox.Text) Then
    Cancel = False
  Else
    MsgBox "Please only enter numbers for SSNs!"
    Cancel = True
  End If
End Sub

Something like that..

--

"If to err is human, then I must be some kind of human!" -Me
 
. . . or perhaps:
Code:
[blue]Private Sub [purple][b][i]TextboxName[/i][/b][/purple]_BeforeUpdate(Cancel As Integer)
     
   If Not IsNumeric(Me.[purple][b][i]TextboxName[/i][/b][/purple].Text) Then
      MsgBox "Please only enter numbers for SSNs!"
      Cancel = True
   ElseIf Len(Me.[purple][b][i]TextboxName[/i][/b][/purple].Text) <> 9 Then
      MsgBox "Exactly 9 digits please!"
      Cancel = True
   End If
     
End Sub[/blue]

Calvin.gif
See Ya! . . . . . .
 
Yep, that'd be more concise.

I had read in some posts here, however, that using the Not keyword can slow down the code (a little), so that is why I coded it as above. It takes more lines of code, but if the whole "Not" theory/mention is correct, then the first code should run a little faster (not saying it would necessary be noticeable). [smile]

So, is it true about the "Not" theory, or am I just being a "knot on a log."

[LOL] Couldn't resist.

--

"If to err is human, then I must be some kind of human!" -Me
 
Howdy kjv1611! . . .
[blue]So, is it true about the "Not" theory . . .[/blue]
[blue]Poppy-Cock![/blue] [lol] . . . it must've been some fellows polishing their prowess over a few microseconds [blue]who love to hear themselves talk![/blue] [lol]

Technically Not is an [blue]processor instruction[/blue] and a part of every instruction set for every processor out their. Within every instruction set Not is one of the fastest executables!

Calvin.gif
See Ya! . . . . . .
 
Thanks for that bit of info, there, AceMan! [wink]
Pointy thingy on the way! [smile]

--

"If to err is human, then I must be some kind of human!" -Me
 
kjv1611 . . .

Actually the reason I used [blue]Not[/blue] was not for speed but for the fact [blue]the default value of Cancel is False![/blue]

So what happen to [blue]RDMRDM?[/blue] . . .


Calvin.gif
See Ya! . . . . . .
 
Got everything to work properly. Thanks so much for everyones help. RDMRDM
 
RDMRDM . . .

BTW: [blue]Welcome to Tek-Tips![/blue] To get great answers and know whats expected of you in the forums be sure to have a look at FAQ219-2884 [thumbsup2]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top