Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Reports (VBA) 1

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
0
0
GB
I have a report that has an on open event as follows:

Private Sub Report_Open(Cancel As Integer)


Dim strCriteria As String
strCriteria = InputBox("Please enter the full month for data you wish to see, e.g. type " & Chr(34) & "May." & Chr(34), "Enter month to view data")

If strCriteria = "January" Then
ElseIf strCriteria = "February" Then
ElseIf strCriteria = "March" Then
ElseIf strCriteria = "April" Then
ElseIf strCriteria = "May" Then
ElseIf strCriteria = "June" Then
ElseIf strCriteria = "July" Then
ElseIf strCriteria = "August" Then
ElseIf strCriteria = "September" Then
ElseIf strCriteria = "October" Then
ElseIf strCriteria = "November" Then
ElseIf strCriteria = "December" Then
Else
MsgBox "Incorrect month format has been entered", , "Error!"
DoCmd.Close
End If
End Sub

Basically an input box is requesting that a month is typed. If the month is not typed correctly, I want to close the report (I could put this code on a form rather than the report on open event, but I need to learn how to do it this way!)

I cannot use docmd.close as I have in my code above, as an error is generated: "This action cannot be carried out while processing a form or report".

Any resolutions gratefully accepted!

TIA
 
If you put the line "End" in the else part of your if statement. Your everything will come to a screeching halt; the report won't even finish opening.
 
Just a remark wich is not realy about your problem but wich can simplify the reading of your code.
Try using this code

Select Case strCriteria
Case "January"
' code
Case "February"
' code
...
Case Else
MsgBox "Incorrect month format has been entered", , "Error!"
End
End Select

It does the same but is easier to read and to change if necessary.

B.
 
Just a note about the "End" statement. My situation is one in which the report is opened by a database which is a black box - I can't see it. It generates an error message ("Subscript out of range"). I have no idea what this is all about. When I open it directly from the database window, it works fine. I have a hunch that there may be some kind of "On Error" statement in the code which calls for the opening of the report and is triggered when the report fails to open. If you are in total control of the call to the report, you should have no problem.
 
Use a Combo box on the selection screen. Set the property Limit to List = Yes. The Row Source Type is 'Value List'. The row source = "January";"Feb..."; etc. Now your user can only pick the correct month and there are no typos.

Mary Bussio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top