Results 1 to 9 of 9
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    KeyDown Event (A2000 SR1)

    The following code is supposed to check if a telephone number exists before the form closes.
    If one doesn't exist, the code puts the user back to the [Tel] text box to enter one.
    If the user presses VbKeyF10 again, the code see's the [Tel] HAS now been filled in and is ok to close the form.
    It's not working correctly.
    I just get kicked out of the form.
    Can any-one see the reason for this and offer some help.
    Regards
    Dave

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF10 Then
    If IsNull([Tel]) Then MsgBox "You Must Enter A Telephone Contact Number For This Engineer"
    Me.Tel.SetFocus
    Else
    DoCmd.Close acForm, Me.Name
    End If
    End Sub

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: KeyDown Event (A2000 SR1)

    Dave,
    You may need to check if Tel = "" as well as if it's Null.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: KeyDown Event (A2000 SR1)

    Cheers Rory

    I was hoping for some help on the syntax really.
    Can you help further

    Regards

    Dave

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: KeyDown Event (A2000 SR1)

    For Form KeyDown event to work properly, you have to set form's KeyPreview property to Yes. As noted in Help file for KeyDown event:
    <hr>For both events, the object with the focus receives all keystrokes. A form can have the focus only if it has no controls or all its visible controls are disabled.

    A form will also receive all keyboard events, even those that occur for controls, if you set the KeyPreview property of the form to Yes. With this property setting, all keyboard events occur first for the form, and then for the control that has the focus. You can respond to specific keys pressed in the form, regardless of which control has the focus. For example, you may want the key combination CTRL+X to always perform the same action on a form.<hr>

    Otherwise syntax for event looks OK.

    HTH

  5. #5
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: KeyDown Event (A2000 SR1)

    Sorry, Dave, I didn't really think about what your code was doing there! Currently your code is trapping all keystrokes anywhere on the form (I'm assuming you have the form's KeyPreview property set to yes?) and if the user presses anything except f10 at any time then the Else condition is triggered and it closes the form. What you need is to detect if they press f10 and then, if so, check if Tel is empty, if not close the form. Something like:
    <pre>Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyF10
    If IsNull(Me!Tel) Or Me!Tel = "" Then
    MsgBox "You Must Enter A Telephone Contact Number For This Engineer"
    Me.Tel.SetFocus
    Else
    DoCmd.Close acForm, Me.Name
    End If
    Case Else
    End Select

    End Sub
    </pre>

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  6. #6
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: KeyDown Event (A2000 SR1)

    PS: On second thought I'd modify syntax slightly to this:

    <pre>If KeyCode = vbKeyF10 Then
    If IsNull([TEL]) Or Len([TEL]) = 0 Then
    MsgBox "You Must Enter A Telephone Number"
    Me.TEL.SetFocus
    Else
    DoCmd.Close acForm, Me.Name
    End If
    End If</pre>

    The nestled IF statements are easier to read this way which helps avoid logic errors. The LEN function tests for zero-length string in addition to Null.

    HTH

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: KeyDown Event (A2000 SR1)

    Thanks Rory & Mark.

    I can manipulate the code from here.

    Thanks Again

    Dave

  8. #8
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: KeyDown Event (A2000 SR1)

    Hi Mark

    The LEN function tests for zero-length string in addition to Null.

    Does that mean this would work?

    If KeyCode = vbKeyF10 Then
    'If IsNull([TEL]) Or Len([TEL]) = 0 Then
    If Len([TEL]) = 0 Then
    MsgBox "You Must Enter A Telephone Number"
    Me.TEL.SetFocus
    Else
    DoCmd.Close acForm, Me.Name
    End If
    End If

    Thanks, John

  9. #9
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: KeyDown Event (A2000 SR1)

    Hi John,
    No that wouldn't work because len(Null) returns Null not 0. You could use:
    <pre>If Len([Tel] & "") = 0 Then
    </pre>

    instead though.
    HTH.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •