If you're talking about displaying the string after someone has entered it
normally, you can use Mid$ in statement mode to affect the desired change:
Private Sub Command1_Click()
Dim s As String
s = "1234567890"
Mid$(s, 1, 6) = "******"
Text1.Text = s
End Sub
If you're talking about modifying text entry on-the-fly as the user enters
the data, you'll need to manually handle the keypresses in the text box.
Start a new project, add two text boxes to the form, along with this code.
Text2 is where you enter the SSN; the entered value and the protected
strings are shown in Text1 and Text2, respectively:
Option Explicit
Dim realSSN As String
Dim protectedSSN As String
'for our purposes, to test that
'1234567890 converts to ******7890
Private Const maxSSNLength = 10
Private Sub Form_Load()
Text2.MaxLength = maxSSNLength
End Sub
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyDelete
'we'll cheat and assume a
'delete keypress means nuke everything.
'Good coding practice would only
'delete selected text, and adjust
'the real and protected strings
'accordingly.
Text2.Text = ""
realSSN = ""
protectedSSN = ""
End Select
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
Static bWorking As Boolean
Dim sLen As Long
'safety valve
If bWorking Then Exit Sub
bWorking = True
Select Case KeyAscii
'if key is 0 to 9
Case vbKey0 To vbKey9, vbKeyNumpad0 To vbKeyNumpad9
'if we've not reached the
'max length of the SSN string
If Len(realSSN) <= maxSSNLength Then
'the real SSN is built by adding
'the chr for the key pressed
realSSN = realSSN & Chr$(KeyAscii)
'the length of this string is saved
sLen = Len(realSSN)
'we want the first 6 to have the
'pw char
If sLen <= 6 Then
'first assign the realSSN to
'the protected string
protectedSSN = realSSN
'and in the protected string
'replace up to the first 6 chrs
'with a *
Mid$(protectedSSN, 1, sLen) = String(sLen, "*")
Else
'we're past the 6th chr,
'so show the actual chr
protectedSSN = protectedSSN & Chr$(KeyAscii)
End If
'show the two strings in two text boxes
Text1.Text = realSSN
Text2.Text = protectedSSN
'make sure the cursor is at the end
'of the entry text box
Text2.SelStart = Len(Text2.Text)
End If
'kill the keypress so the text box
'doesn't handle the keypress itself
KeyAscii = 0
Case vbKeyBack
'backspace pressed, so remove 1 from
'each string
If Len(realSSN) > 0 Then
realSSN = Left$(realSSN, Len(realSSN) - 1)
protectedSSN = Left$(protectedSSN, Len(realSSN) - 1)
'reflect the changes
Text1.Text = realSSN
Text2.Text = protectedSSN
Text2.SelStart = Len(Text2.Text)
'and kill the keypress
KeyAscii = 0
End If
Case Else
'anything other than
'backspace or 0 to 9,
'we kill
KeyAscii = 0
End Select
'reset to allow work
bWorking = False
End Sub
--
--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
Please reply to the newsgroups so all can participate.
"Bill" <NO*************@billsterworld.com> wrote in message
news:Es******************************@centurytel.n et...
: Hello,
: Could anyone post some simple code or advise me on how I can display the
SSN
: number like *****7890 in a text box, even thought the user entered
: 1234567890, and the value of the variable would remain the same.
: Only thing I can think of is, accept the user input load it into a
varible,
: and then just take the last 4 digits of the number and concatenate it to 5
: *'s and replace the text box variable with that.... I was hoping there was
a
: better way to do this though.
: Thanks for any help..
: Please email me
be*******@billsterworld.com
: Thanks.
:
: