Need help with error code

MarcieFess

Registered User.
Local time
Today, 02:20
Joined
Oct 25, 2012
Messages
107
I've created a form, called frmReportsMenu. From this form, the users will be able to print the various forms required for client company stores.

On the form there is currently 1 field: a combo box with the label Store Name (combo box is named cboStoreKey).

The control source for this combo box is:

SELECT [Store Information].[Store Key], [Store Information].[Store Name] FROM [Store Information] ORDER BY [Store Name];

There is a button that says "Print HMIS Report"

Here's the VBA code in its entirety for this form:

Option Compare Database
'------------------------------------------------------------
' btnPrintHMISRpt_Click
'
'------------------------------------------------------------
Private Sub btnPrintHMISRpt_Click()
On Error GoTo btnPrintHMISRpt_Click_Err

Exit Sub


DoCmd.OpenReport "HmisReport", acViewPreview, , "[Store Key] = " & Me.cboStoreKey

DoCmd.RunCommand acCmdPrintPreview

btnPrintHMISRpt_Click_Exit:
Exit Sub
btnPrintHMISRpt_Click_Err:
MsgBox Error$
If Me.cboStoreKey = "" Then MsgBox ("You must select a store!")
End If
Resume btnPrintHMISRpt_Click_Exit
End Sub

When I click the button, I get a compile error: End If without block If

The line 'Private Sub btnPrintHIMSRpt_Click() has' a yellow arrow pointing to it, and the line is highlighted yellow.

The 'End If' (second line from the bottom) is selected.

I really don't understand this error. Can anyone help?
 
Well, the error is because of the End If line, which you can delete. You used the one-line format. Then you'll run into the next issue. ;)

By the way, I personally wouldn't rely on an error trap for that particular thing, I'd test for it at the beginning.
 
Actually the code isn't mine...I'm a beginner or maybe an advanced beginner. I found code that I could use, or I've gotten help from others here and elsewhere and that error trapping code was in their code so I used it. I can take it out.

It was that simple? Wow. I see an "If" statement and an "End If" statemenst and figure they match up.

Thank you!
 
The "normal" or block format would be:

Code:
If Me.cboStoreKey = "" Then 
  MsgBox ("You must select a store!")
End If

When you put it on one line, you don't use the End If (it knows the end of the line is the end of the statement). Personally I rarely use the one-line format, but it's perfectly valid.

And I have no problem with the error trap itself. I'd say there are two schools of thought on this type of thing. Preventing the error and letting the error trap handle it. Most of the time I prefer the former, so at the beginning of my code I'd have this type of thing:

Code:
If Me.cboStoreKey = "" Then 
  MsgBox ("You must select a store!")
  Exit Sub
End If

Where the Exit Sub would prevent code from continuing down to where the error would occur.
 

Users who are viewing this thread

Back
Top Bottom