Results 1 to 5 of 5
  1. #1
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262

    Using macro converted to VBA in other functions

    So I have a macro linked to a button, and I converted it to VBA to use in other functions. The function described in the following works on my other form, but the code was hand typed and not converted.



    Every time I hit this button, I want it to run its regular code, and then at the end, set the focus to a certain field. I also want to link this button to the enter key (KeyCode vbKeyReturn) in the scope of the form. Button code and proposed function code:

    Code:
    Private Sub butDAdd_Click()
    On Error GoTo butDAdd_Click_Err
    
    
        On Error Resume Next
        DoCmd.GoToRecord , "", acNewRec
        If (MacroError <> 0) Then
            Beep
            MsgBox MacroError.Description, vbOKOnly, ""
        End If
    
    
    
    
    butDAdd_Click_Exit:
        Exit Sub
    
    
    butDAdd_Click_Err:
        MsgBox Error$
        Resume butDAdd_Click_Exit
    
    
    End Sub
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    
    If (KeyCode = vbKeyReturn) Then
    butDAdd_Click
    Me.Drawing.SetFocus
    End If
    
    
    End Sub
    Right now, it does not do the function as described. It acts normally, Enter just toggles through the fields and buttons. This is probably built into access which is why it may not be working, but I'd like to try and override that. I used the KeyDown function because I don't know off the top of my head the ascii code for it in access, and so avoided the KeyPress function for now.

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    To make your command button respond to the Enter key, set its Default property to Yes. You don't need the Form_Keydown event code.

    John

  3. #3
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Ah That was painfully simple hehe.

    Now to trigger the SetFocus property after executing the button code?

  4. #4
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    UPDATE: I think I figured it out!

    Inserted code the red statement into the converted code:

    Code:
    Private Sub butDAdd_Click()
    On Error GoTo butDAdd_Click_Err
    
    
        On Error Resume Next
        DoCmd.GoToRecord , "", acNewRec
        Me.Drawing.SetFocus
        If (MacroError <> 0) Then
            Beep
            MsgBox MacroError.Description, vbOKOnly, ""
        End If
    
    
    
    
    butDAdd_Click_Exit:
        Exit Sub
    
    
    butDAdd_Click_Err:
        MsgBox Error$
        Resume butDAdd_Click_Exit
    
    
    End Sub

  5. #5
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    I tried the above with another form and it worked. Marking solved

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

Similar Threads

  1. Converted macro to VBA not working
    By gemadan96 in forum Forms
    Replies: 5
    Last Post: 06-04-2014, 10:36 AM
  2. Import Report Converted to Excel into Access
    By Dig in forum Import/Export Data
    Replies: 1
    Last Post: 03-07-2014, 11:26 AM
  3. Assign converted macro to navigation panel button
    By nhylan in forum Programming
    Replies: 4
    Last Post: 04-17-2013, 01:33 PM
  4. Replies: 2
    Last Post: 11-08-2012, 05:22 AM
  5. converted coding issues.
    By d4jones in forum Database Design
    Replies: 2
    Last Post: 07-11-2012, 09:48 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 - Senior Forums