| Description:This code turns a standard textbox into a masked textbox which only accepts social security numbers and places dashes accordingly. Assumes:Create a textbox and name it txtSSN. Paste the code in the form. Private Sub txtssn_Change() Dim strTemp As String Dim intPos As Integer Dim intSel As Integer Dim intLen As Integer With txtssn intSel = .SelStart intLen = Len(.Text) strTemp = .Text intPos = InStr(1, strTemp, "-") While intPos <> 0 strTemp = Left(strTemp, intPos - 1) & Right(strTemp, Len(strTemp) - intPos) intPos = InStr(1, strTemp, "-") Wend If Len(strTemp) > 2 Then strTemp = Left(strTemp, 3) & "-" & Right(strTemp, Len(strTemp) - 3) If Len(strTemp) > 5 Then strTemp = Left(strTemp, 6) & "-" & Right(strTemp, Len(strTemp) - 6) End If .Text = strTemp intSel = intSel + (Len(.Text) - intLen) If intSel < 0 Then intSel = 0 .SelStart = intSel End With End Sub Private Sub txtssn_KeyPress(KeyAscii As Integer) Dim intSel As Integer With txtssn If KeyAscii >= 48 And KeyAscii <= 57 Then If Len(.Text) >= 11 Then KeyAscii = 0 ElseIf KeyAscii = 8 Then KeyAscii = 0 intSel = .SelStart If intSel = 4 Or intSel = 7 Then .Text = Left(.Text, intSel - 2) & Right(.Text, Len(.Text) - intSel) .SelStart = intSel - 2 ElseIf intSel > 0 Then .Text = Left(.Text, intSel - 1) & Right(.Text, Len(.Text) - intSel) .SelStart = intSel - 1 End If Else KeyAscii = 0 End If End With End Sub |
Create a Social Security Number Textbox |
Freelance ASP PHP web development | Web developer India Web development India| Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design