... and if it similar to your preset string of keys, then notice you "right typing", else "wrong typing"
I do searching and changing to have these codes, do you think about any problems with these codes (I hear that if another control belong to this form get focus then these codes are not ok ???)

' Create a lblMsg and btnLogin
Option Explicit
Dim strOfKeys As String
Dim keypresscount As Integer
Private Sub Form_Load()
   Me.Show
   'The form must handle the key events
   Me.KeyPreview = True
   strOfKeys = ""
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
' This is to restrict the times of key typing
    If keypresscount < 4 Then
    strOfKeys = strOfKeys + Chr(KeyAscii)
    'MsgBox "Form KeyPress event..." & Chr(KeyAscii) & " Key pressed", vbInformation, "Form Events"
    keypresscount = keypresscount + 1
    Else
    Exit Sub
End If
End Sub
Private Sub btnLogin_Click()
    ' Suppose that your keys is "abc"
    If (strOfKeys = "abc") Then
        lblMsg.Caption = "Right typing"
    Else
        lblMsg.Caption = "Wrong typing"
    End If
End Sub

Thanks !!!
:)

Hi,

Set this Property of Form:

KeyPreview =True.

By doing this ur Form will First Receive the KeyPress event before the Control.


Regards
Veena

do you mean Me.KeyPreview = True
I had it in codes ???

HI,

Give it in "PropertySheet" of "Forn" in Design mode

Regards
Veena

hi,

i had modified little in the coding.

Option Explicit
Dim strOfKeys As String
Dim keypresscount As Integer
Private Sub Form_Load()
Me.Show
'The form must handle the key events
Me.KeyPreview = True
strOfKeys = ""
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
' This is to restrict the times of key typing

strOfKeys = strOfKeys + Chr(KeyAscii)
'MsgBox "Form KeyPress event..." & Chr(KeyAscii) & " Key pressed", vbInformation, "Form Events"

End Sub
Private Sub btnLogin_Click()
Print strOfKeys
' Suppose that your keys is "abc"
If keypresscount < 4 Then
If (strOfKeys = "abc") Then
lblMsg.Caption = "Right typing"
strOfKeys = ""
Else
lblMsg.Caption = "Wrong typing"
End If
keypresscount = keypresscount + 1
Else
Exit Sub
End If
End Sub

ur coding is right but little changes

1. u checked keypresscount <4 in keypress event so wht happens means while u typing e.g("abc"), it increases keypresscount as 3. then u trying again by pressing one key it ncreases by one so the value of the keypresscount is 4. so it wont go inside the coding.

2. u didn't cleared the strOfKeys


al the best
shailu:)

HI,

Give it in "PropertySheet" of "Forn" in Design mode

Regards
Veena

Hi I did this but another control can do its effect, I add a button "command1"
Private Sub Command1_Click()
MsgBox ("hello")
End Sub
What I want is you must type string of keys first , if true then another control can be affected !!!
In this case, command 1 still give Message when I click it :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.