I created some code to handle masking the SSN, you should be able to adapt it for the DOB.
My form has a textbox called txtSSN and a button to toggle viewing masked or not.
Private Sub btnToggleView_Click()
' switch between masked and clear view of the SSN
txtSSN.SetFocus
If Left(txtSSN.Text, 1) = "*" Then
' currently displaying masked view, switch to clear view
btnToggleView.Caption = "Hide"
UpdateTextBox txtSSN, Len(txtSSN.Text), True
Else
' switch the clear view back to masked
btnToggleView.Caption = "View"
UpdateTextBox txtSSN, Len(txtSSN.Text)
End If
End Sub
Private Sub Form_Load()
' when the form is displayed, make sure things are cleared
txtSSN.SetFocus
txtSSN.Text = ""
txtSSN.Tag = ""
btnToggleView.Enabled = False
End Sub
Private Function MaskSSN() As String
' change all the numbers to *
Dim x As Integer
Dim maskedSSN As String
For x = 1 To Len(txtSSN.Tag)
If x = 4 Or x = 7 Then
' add the dash
maskedSSN = maskedSSN & "-"
Else
maskedSSN = maskedSSN & "*"
End If
Next x
MaskSSN = maskedSSN
End Function
Private Sub UpdateTextBox(ByRef txtBox As TextBox, PositionOfCursor As Integer, Optional unMask As Boolean)
' display the post masked data
txtBox.Text = ""
If unMask Then txtBox.Text = txtBox.Tag Else txtBox.Text = MaskSSN()
' position the cursor in the textbox
txtBox.SelStart = PositionOfCursor
txtBox.SelLength = 0
DoEvents
End Sub
Private Sub DeleteTheCharacterAt(ByRef txtBox As TextBox)
' remove the character at the current position of the textbox passed
txtBox.Tag = Left(txtBox.Tag, txtBox.SelStart) & Mid(txtBox.Tag, txtBox.SelStart + 2)
DoEvents
Exit Sub
End Sub
Private Sub txtSSN_KeyUp(KeyCode As Integer, Shift As Integer)
Dim KeyAscii As Integer
If KeyCode >= 96 And KeyCode <= 105 Then
' these are the number pad numbers
' this is needed because KeyCode is not the Ascii representation of the key
' but the number on the keyboard
KeyAscii = KeyCode - 48
Else
' some KeyCode and their ascii values are the same though
KeyAscii = KeyCode
End If
Select Case KeyAscii
Case 8, 46 ' backspace or delete key pressed
DeleteTheCharacterAt txtSSN
Case 48 To 57 ' a number 0 through 9 using the top keys
' this is the max length of a SSN, ignore the input
If Len(txtSSN.Tag) = 11 Then
' remove the 12th character by re-applying the completed SSN
UpdateTextBox txtSSN, 11
Exit Sub
End If
If txtSSN.SelStart >= Len(txtSSN.Tag) Then
' keep the actual numbers to save in the DB in the tag property
txtSSN.Tag = txtSSN.Tag & Chr(KeyAscii)
' automatically add the dashes
If Len(txtSSN.Tag) = 3 Or Len(txtSSN.Tag) = 6 Then
txtSSN.Tag = txtSSN.Tag & "-"
End If
UpdateTextBox txtSSN, Len(txtSSN.Tag)
Else
' inserting a number after a backspace or an arrow
' moved the cursor
txtSSN.Tag = Left(txtSSN.Tag, txtSSN.SelStart - 1) & Chr(KeyAscii) & Mid(txtSSN.Tag, txtSSN.SelStart)
UpdateTextBox txtSSN, txtSSN.SelStart
End If
Case Else ' do nothing
End Select
btnToggleView.Enabled = (Len(txtSSN.Text) > 0)
End Sub
Bookmarks