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.