Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Lexington Park, Maryland, USA
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Detect Enter key has been pressed (Access 2000)

    How do I detect if the key that has been pressed is the enter key when using the "On Key Press" Event?

    Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detect Enter key has been pressed (Access 2000)

    The On Key Press event has an argument KeyAscii (Integer), You can test if this equals vbKeyReturn:

    Private Sub txtSomething_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
    ' code goes here, e.g.
    Beep
    End If
    End Sub

    Warning: the default action of the Enter key in a text box on a form is to move to the next control. This is handled BEFORE the On Key Press event occurs. It's better to use the On Key Down event, in particular if you want to prevent the Enter key from moving to the next control:

    Private Sub txtSomething_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
    ' Ignore this key
    KeyCode = 0
    End If
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Lexington Park, Maryland, USA
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Enter key has been pressed (Access 2000)

    That worked perfectly. You are correct. I needed to use the key down event instead. For others information. I needed to add "me.refresh" after "If KeyCode = vbKeyReturn Then". The code looked like the following.


    Private Sub txtPassword_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
    Me.Refresh
    but_Enter_Click
    KeyCode = 0
    End If
    End Sub



    Thanks
    Mike

  4. #4
    5 Star Lounger
    Join Date
    Mar 2002
    Location
    Buenos Aires, Argentina
    Posts
    877
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Enter key has been pressed (Access 2000)

    Hi Hans,

    I'm using a textbox for users to type messages, which are to be stored in a tblMsgs table by pressing a cmdSend button. I'd like them to be able to use the chr(13) carriage return character. I noticed that by pressing CTRL+Enter within the textbox Access generates a carriage return, and these are effectively stored in the table along with the rest of the string (so it can be done). How would I go about Access generating a carriage return on Enter key press? I tried the following, but obviously to no avail:

    <code>Private Sub T_Cuerpo_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
    ' attempt to capture Enter key and return a carriage return character...
    KeyCode = 13
    End If
    End Sub</code>

    Any clues?
    <img src=/w3timages/blue3line.gif width=33% height=2>
    <img src=/S/flags/Argentina.gif border=0 alt=Argentina width=30 height=18> <big><font color=4682b4><font face="Comic Sans MS">Diegol</font face=comic></font color=4682b4> </big>

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detect Enter key has been pressed (Access 2000)

    You don't need VBA code for that. Set the "Enter key behavior" property of the text box to "New line in field" instead of Default.

  6. #6
    5 Star Lounger
    Join Date
    Mar 2002
    Location
    Buenos Aires, Argentina
    Posts
    877
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Enter key has been pressed (Access 2000)

    Alright Hans. Thank you <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>.
    <img src=/w3timages/blue3line.gif width=33% height=2>
    <img src=/S/flags/Argentina.gif border=0 alt=Argentina width=30 height=18> <big><font color=4682b4><font face="Comic Sans MS">Diegol</font face=comic></font color=4682b4> </big>

Posting Permissions

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