Solved

Key Ascii

Posted on 2002-04-08
4
2,016 Views
Last Modified: 2008-03-10
Hi,

I have a password field where i display the password entered with a * and i append one more * to make it look like ** for every key stroke. I want to disable the space bar key from the password.. The code i have still gives one * for space bar..how do i supress it.


If KeyAscii = 32 Then
MsgBox "Space is not a valid key", vbOKOnly
txtPassword.SetFocus
Exit Sub

Else
txtPassword.Text = txtPassword.Text & "*"
txtPassword.SelStart = Len(txtPassword.Text)
End If

If KeyAscii = 8 Then
txtPassword.Text = Mid(txtPassword.Text, 1, Len(txtPassword.Text) - 2)
txtPassword.SelStart = Len(txtPassword.Text)
End If


Thanks
0
Comment
Question by:sramkris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 17

Expert Comment

by:inthedark
ID: 6927408
If KeyAscii = 32 Then
   MsgBox "Space is not a valid key", vbOKOnly
   txtPassword.SetFocus
    KeyAscii = 0
Exit Sub

0
 
LVL 17

Accepted Solution

by:
inthedark earned 100 total points
ID: 6927420
Also suggest that you set the PasswordChar property of the textbox to "*"

In this way you only need the following code (EXAMPLE FOR TEXT1):

Private Sub Text1_KeyPress(KeyAscii As Integer)

Static ValidKeys As String
Static Done As Boolean

If Not Done Then
    ' ADD YOU VALID KEYS HERE:
    ValidKeys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEGHIJLKMNOPQRSTUVWXYZ`¬!""£$%^&*(){}_+=[]/\?><,.;:'"
End If
If InStr(ValidKeys, Chr(KeyAscii)) = 0 Then
    KeyAscii = 0
    MsgBox "Invalid Password Key", vbExclamation, "Error"
    Text1.SetFocus
End If

End Sub

Hope this helps....inthedark
0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 6927421
Hi,
Try this -


Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 32 Then KeyAscii = 0
End Sub
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6927423
Woops

If Not Done Then
   Done = True ' I forgot this
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question