L
lmcc007
I am a little confused!
On my old forms my command buttons were created using the wizard, which
created macros. Since I am trying to learn VBA, I want to create code for
them. Therefore, I saved the macros and then converted them to VBA. There
is some code on there I do not understand—for instance, “With
CodeContextObject.†Below is an example of one of the codes for saving a
record:
Option Compare Database
Option Explicit
'------------------------------------------------------------
' SaveAddress
'
'------------------------------------------------------------
Function SaveAddress()
On Error GoTo SaveAddress_Err
With CodeContextObject
On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
If (.MacroError <> 0) Then
Beep
MsgBox .MacroError.Description, vbOKOnly, ""
End If
End With
SaveAddress_Exit:
Exit Function
SaveAddress_Err:
MsgBox Error$
Resume SaveAddress_Exit
End Function
Since I would like to know what is going on I decided to create a code that
I can use over and over. Should I use a Procedure instead of a Function?
This is the code I wrote for my save record command button:
Private Sub cmdSaveAddress_Click()
SaveRecord
End Sub
Also, I created this first in a module called modUtility:
Public Function SaveRecord()
On Error GoTo HandleError
DoCmd.RunCommand (acCmdSaveRecord)
ExitHere:
Exit Function
HandleError:
MsgBox Err.Description
Resume ExitHere
End Function
Am I doing it correctly and on the right path?
Thanks!
On my old forms my command buttons were created using the wizard, which
created macros. Since I am trying to learn VBA, I want to create code for
them. Therefore, I saved the macros and then converted them to VBA. There
is some code on there I do not understand—for instance, “With
CodeContextObject.†Below is an example of one of the codes for saving a
record:
Option Compare Database
Option Explicit
'------------------------------------------------------------
' SaveAddress
'
'------------------------------------------------------------
Function SaveAddress()
On Error GoTo SaveAddress_Err
With CodeContextObject
On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
If (.MacroError <> 0) Then
Beep
MsgBox .MacroError.Description, vbOKOnly, ""
End If
End With
SaveAddress_Exit:
Exit Function
SaveAddress_Err:
MsgBox Error$
Resume SaveAddress_Exit
End Function
Since I would like to know what is going on I decided to create a code that
I can use over and over. Should I use a Procedure instead of a Function?
This is the code I wrote for my save record command button:
Private Sub cmdSaveAddress_Click()
SaveRecord
End Sub
Also, I created this first in a module called modUtility:
Public Function SaveRecord()
On Error GoTo HandleError
DoCmd.RunCommand (acCmdSaveRecord)
ExitHere:
Exit Function
HandleError:
MsgBox Err.Description
Resume ExitHere
End Function
Am I doing it correctly and on the right path?
Thanks!