Command Button Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I use the wizard to create a command button (cmdNewReport)I get
something like this:
Private Sub cmdNewReport_Click()
On Error GoTo Err_cmdNewReport_Click

Dim stDocName As String

stDocName = "rptMyReport"
DoCmd.OpenReport stDocName, acPreview

Exit_cmdNewReport_Click:
Exit Sub

Err_cmdNewReport_Click:
MsgBox Err.Description
Resume Exit_cmdNewReport_Click

End Sub

On my own I would use the VBA editor to generate something like this:
DoCmd.OpenReport "rptMyReport", acPreviewView

The acPreviewView part is what shows up automatically in the VBA editor. It
seems to function similarly to acPreview, which the wizard generates.
My questions are: Do I lose or gain anything by entering the report name
directly into the DoCmd line rather than by defining it as the wizard does?
Do acPreview and acPreviewView do the same thing? What does this code
accomplish, and do I need it (or is it advisable to include it)?:
Err_cmdNewReport_Click:
MsgBox Err.Description
Resume Exit_cmdNewReport_Click
 
Bruce,

Apart from the fact that it's acViewPreview and not acPreviewView :-)
as far as I know there would be no significant difference in performance
between these two approaches. The Wizards in Access are well-known for
the awkwardness of the code they produce. I personally would always do
the same as you, i.e.
DoCmd.OpenReport "NameOfReport", acViewPreview
.... otherwise you are just cluttering up your code with unnecessary fluff.
It would be different if you needed to refer to the name of the report
multiple times within the procedure, in which case there would be some
advantage in assigning it to a variable.

As for the error handling code, the answer is Yes, generally every
procedure should have an error handler written into it. The example you
showed from the wizard is about the most basic error handling you can get.
 
When I use the wizard to create a command button (cmdNewReport)I get
something like this:
Private Sub cmdNewReport_Click()
On Error GoTo Err_cmdNewReport_Click

Dim stDocName As String

stDocName = "rptMyReport"
DoCmd.OpenReport stDocName, acPreview

Exit_cmdNewReport_Click:
Exit Sub

Err_cmdNewReport_Click:
MsgBox Err.Description
Resume Exit_cmdNewReport_Click

End Sub

On my own I would use the VBA editor to generate something like this:
DoCmd.OpenReport "rptMyReport", acPreviewView

The acPreviewView part is what shows up automatically in the VBA editor. It
seems to function similarly to acPreview, which the wizard generates.
My questions are: Do I lose or gain anything by entering the report name
directly into the DoCmd line rather than by defining it as the wizard does?
Do acPreview and acPreviewView do the same thing? What does this code
accomplish, and do I need it (or is it advisable to include it)?:
Err_cmdNewReport_Click:
MsgBox Err.Description
Resume Exit_cmdNewReport_Click


In addition to what Steve said, I would always add the error number to
the error message, as without the number you don't know what error to
trap if you needed to:

Err_cmdNewReport_Click:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_cmdNewReport_Click
 
Personnally, I agree with Fred, but would go one step further and display
where segment of code was that through the error.

Err_cmdNewReport_Click:
MsgBox "Form: enter your form name here" & vbcrlf _
& "Procedure: cmdNewReport_Click" & vbcrlf _
& "Error #: " & Err.Number & " " & Err.Description
Resume Exit_cmdNewReport_Click
 
Thanks for the replies. I asked the question at work on Friday, and didn't
look at it again until today, which accounts for the long delay in
acknowledging the help. I ended up using the following code:

Private Sub cmdNewReport_Click()
On Error GoTo Err_cmdNewReport_Click

DoCmd.OpenReport "rptMyReport", acPreview

Exit_cmdNewReport_Click:
Exit Sub

Err_cmdNewReport_Click:
MsgBox "Form #: " & Err.Number & " " & Err.Description
Resume Exit_cmdNewReport_Click

End Sub

I still don't know just what would trigger an error, or what exactly will
show up in the message box if one occurs. Clearly I need to learn more about
this aspect of Access, but for now I appreciate the help in getting past the
latest question.


Dale Fye said:
Personnally, I agree with Fred, but would go one step further and display
where segment of code was that through the error.

Err_cmdNewReport_Click:
MsgBox "Form: enter your form name here" & vbcrlf _
& "Procedure: cmdNewReport_Click" & vbcrlf _
& "Error #: " & Err.Number & " " & Err.Description
Resume Exit_cmdNewReport_Click
 
Back
Top