how can i Enable the F2 or f3 in my keyboard when i press the F1 in my keyboard??

btw this is my code

i want to enable the F2 when i press F1......I've got an error when i press the F2 first before the F1....sorry for my stupidity it is because i'm a newbie in vb6

If KeyCode = vbKeyF1 Then
search_click
If KeyCode = vbKeyF2 Then
delete_click
End If

Dani AI

Generated

The runtime error triggered when F2 is pressed first is almost always caused by the delete routine running without the state that the search routine creates (selection, record pointer, nonempty variables, etc.). was right to suggest a boolean flag; 's suggestion to call delete immediately after F1 defeats the purpose, and correctly notes that F1 is commonly the Help key so it can be intercepted by the system.

Use the form KeyDown event with the form's KeyPreview set to True, keep a module-level flag to record that F1 was pressed, and move the actual work into reusable procedures instead of directly calling button_Click handlers. Example pattern:

' module-level flag
Private F1Pressed As Boolean

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyF1
            F1Pressed = True
            DoSearchAction   ' shared routine (extract logic out of button_Click)
            KeyCode = 0      ' consume F1 (prevents default Help)
        Case vbKeyF2
            If Not F1Pressed Then
                MsgBox "Search must run before Delete."
            Else
                DoDeleteAction
                F1Pressed = False
            End If
    End Select
End Sub

Notes and troubleshooting:

  • Prefer named routines like DoSearchAction and DoDeleteAction so both keys and UI buttons call the same safe logic.
  • Validate preconditions inside the delete routine (nonempty selection, valid record index) to avoid crashes even if the flag is wrong.
  • If keys still don't reach the form, ensure focus settings and that controls (textboxes) are not consuming keys; KeyPreview = True solves most focus problems.
  • Consider a timeout or toggle for the F1 flag if accidental presses must expire.

Recommended Answers

All 3 Replies

just make the code looks like this :-

If KeyCode = vbKeyF1 Then
search_click
delete_click
If KeyCode = vbKeyF2 Then
delete_click
End If

You can provide a variable(maybe a boolean) to store if you've pressed F1 in your keyboard and then check it when your pressing F2..

You need to check if F1 has already been pressed which pressing F2 key. It is always recommended to avoid using F1 and F10 key.

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.