Results 1 to 10 of 10
  1. #1
    David618 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Posts
    55

    To click - or not to click...


    New question. On my Log In form, there's a command button (cmdLogin) that runs code on click, but I would also like that same code to run if the user also depresses "enter." I typically enter values and hit "enter" instead of reaching for the mouse and clicking on the command buttons, so I would like for users of my app to have the same convenience. Thoughts on how to do this?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    That would require code that checks if the Enter key was pressed - this is the Key Press event of the form or data controls.

    Alternatively, if you only want code to run if data is entered/edited, use the AfterUpdate event.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Beetle is offline Unrelatable
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    Camp Swampy (Denver, CO)
    Posts
    207
    I don't believe the KeyPress event recognizes the Enter key. I would use the KeyDown event. Example;

    Code:
    Private Sub YourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyReturn Then
            Call cmdYourButton_Click
        End If
    End Sub

  4. #4
    David618 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Posts
    55
    What would be the code that checks if the Enter key was pressed? I see the Key Press event on the property sheet for the command button (cmdLogin - labeled "Enter"). Would it be the same code for "Private Sub cmdLogin_KeyPress(KeyAscii As Integer)" as "Private Sub cmdLogin_Click()"?

  5. #5
    David618 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Posts
    55

    Is it this?

    I think I understand. It would be:

    Code:
    Private Sub cmdLogin_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyReturn Then
            Call cmdLogin_Click
        End If
    End Sub

  6. #6
    Beetle is offline Unrelatable
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    Camp Swampy (Denver, CO)
    Posts
    207
    No. You wouldn't trap it in the command button, you would trap it in the last text box where you enter a value just before you would normally click the command button.

  7. #7
    DepricatedZero's Avatar
    DepricatedZero is offline Cthulhu Fhtagn!
    Windows 8 Access 2007
    Join Date
    Apr 2013
    Location
    Cincinnati
    Posts
    65
    The Enter key is problematic, because Access has it reserved for switching between fields. At least in 2007. Not sure if there's a way around that, I never found one. My login form just has an AfterUpdate event fire on the password. Enter causes it to leave the field, which fires the AfterUpdate, or if they click the Login button it also fires - no one has ever noticed the difference, and I'll be making it proper once I convert it to VB.NET

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Just use the Default Property of the button: In Form Design View, with the cmdLogin selected, go to Properties - Other and set the Default Property to Yes. When <Enter> is pressed the cmdLogin code will fire.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Linq, I see that behavior even if the property is set to No. Of course, the button must have focus so I just tested with Default set as Yes and the button Click event fires regardless of where the focus is. Don't like that.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by June7 View Post

    ...the button Click event fires regardless of where the focus is. Don't like that...
    Be that as it may, it is the behavior the OP asked for! I wouldn't use it on most Forms, but on a login Form, with a user control, a password control and a submit button, it's not apt to cause problems.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Can't Click Any Buttons
    By netchie in forum Access
    Replies: 4
    Last Post: 05-08-2013, 10:38 AM
  2. SaveIt ( ) on Click
    By windwardmi in forum Access
    Replies: 4
    Last Post: 09-07-2011, 03:51 PM
  3. +1 on button click
    By 10 Gauge in forum Forms
    Replies: 4
    Last Post: 02-14-2011, 06:51 AM
  4. Can't Right Click Categories
    By netchie in forum Access
    Replies: 2
    Last Post: 10-26-2010, 10:48 AM
  5. Form on click
    By rexb in forum Forms
    Replies: 0
    Last Post: 11-16-2009, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums