Null statement

V

Very Basic User

I have a form that I select a report name from and click an event procedure
to open the selected report. The event procedure is written below. I need to
add a statement that if a user forgets to select a form name from the
options, it will open a report "ReportError"

Any help will be appreciated!

Private Sub Command50_Click()
Dim strRptName As String
strRptName = Me!Combo61
DoCmd.OpenReport strRptName, acViewReport
End Sub
 
S

Stefan Hoffmann

hi,
I have a form that I select a report name from and click an event procedure
to open the selected report. The event procedure is written below. I need to
add a statement that if a user forgets to select a form name from the
options, it will open a report "ReportError"
You should name your controls...
Any help will be appreciated!

Private Sub cmdOpenReport_Click()

Dim ReportName As String

ReportName = Trim(Nz(cboReportName.Value, ""))

If Len(ReportName) = 0 Then
MsgBox "Please select a report.", vbInformation + vbOkOnly
Else
DoCmd.OpenReport ReportName, acViewReport
End If

End Sub


mfG
--> stefan <--
 
R

RonaldoOneNil

Private Sub Command50_Click()
If IsNull(Me!Combo61) Then
DoCmd.OpenReport "ReportError", acViewReport
Else
DoCmd.OpenReport Me!Combo61, acViewReport
End If
End Sub
 
B

BruceM

You will be doing yourself a favor if you use meaningful names for your
command buttons, combo boxes, etc.

Assuming Combo61 is an unbound combo box from which the user selects a
report from a list, you could put the code into the After Update event for
Combo61. Maybe something like this:

Private Sub Combo61_AfterUpdate()

Dim strRptName As Variant

strRptName = Me!Combo61

DoCmd.OpenReport strRptName, acViewPreview


End Sub

Did you compile the code (Debug >> Compile)? If so, did it work? I ask
because AFAIK acViewReport is not a valid option. What exactly are you
trying to do with that option?

You should add the following to the top of all code modules, just below
Option Compare Database:
Option Explicit

In the VBA editor, click Tools >> Options. Click the Editor tab, and check
the box for Require Variable Declaration. It could be that Access is
allowing acViewReport as a variable, not caring that it is not defined as
anything. strRptName is a valid string variable (you are assigning a value
to a declared string variable (the Dim statement declares the variable).
However, without Option Explicit an undeclared variable will be assumed to
be a variable of variant type, even though it means nothing in context.

Back to the original question, if there is a reason using the command button
is the best option, you can use the Click event to test whether Combo61 is
null:

Private Sub Command50_Click()

Dim varRptName As Variant

varRptName = Me!Combo61

If IsNull varRptName Then
MsgBox "Please select a report."
Me.Combo61.SetFocus
Else
DoCmd.OpenReport varRptName, acViewPreview
End If

End Sub

Note that the variable is a variant, which can be null. A string cannot,
which would have been handled, but it would have complicated the code.
Also, I used acViewPreview, which lets you look at the report rather than
having it print immediately.
 
S

Stefan Hoffmann

hi,
Private Sub Command50_Click()
If IsNull(Me!Combo61) Then
DoCmd.OpenReport "ReportError", acViewReport
Else
DoCmd.OpenReport Me!Combo61, acViewReport
End If
End Sub
Haven't read the post carefully enough...

As a one-liner:

Private Sub Command50_Click()

DoCmd.OpenReport Nz(Combo61.Value, "ReportError"), acViewReport

End Sub

mfG
--> stefan <--
 
V

Very Basic User

RonaldoOneNil, your suggestion worked perfectly, Thank you!

The other comment about acViewReport not being a valid option. I've had that
feedback before, but someone said that it was a new option in the new acess
program. Sorry I can't better answer, but I don't know what some of the
questions are asking for. I have no formal training.

What I'm doing is... I have a table with the actual report name, report
description, and a status column. that I just use a 1, 2, 3 etc to help the
form select the appropriate report. So I have a drop down button that I
select a report from, click the event button, and the approprate report
opens.When the user left the selection empty, they got the error message
Debug or something like that. With Ronaldo's suggesstion It will now open a
report that states that they have to select a report first then they start
over.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top