Report not opening

ubeauty

New member
Local time
Today, 18:27
Joined
Apr 10, 2005
Messages
5
Having a problem with the following code:
Private Sub cmdPrintPreview_Click()
If Forms!frmWantedReports!cboGenres = "Chart" Then
DoCmd.OpenReport "rptWantedChart", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Dancehall/Reggae" Then
DoCmd.OpenReport "rptWantedDancehall", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Electro/Rap" Then
DoCmd.OpenReport "rptWantedElectro", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Funk/Soul" Then
DoCmd.OpenReport "rptWantedFunk", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Hardcore/Rave" Then
DoCmd.OpenReport "rptWantedHardcore", acPreview
Else
If Forms!frmWantedReports!cboGenres = "House" Then
DoCmd.OpenReport "rptWantedHouse", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Indie" Then
DoCmd.OpenReport "rptWantedIndie", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Jungle/Drum&Bass" Then
DoCmd.OpenReport "rptWantedJungle", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Techno/Trance" Then
DoCmd.OpenReport "rptWantedTechno", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Turntablism" Then
DoCmd.OpenReport "rptWantedTurntablism", acPreview
Else
If Forms!frmWantedReports!cboGenres = "TV/Films" Then
DoCmd.OpenReport "rptWantedTV", acPreview
Else
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

DoCmd.Close
End Sub

When the cmd button is pressed the report opens for a milli-second then disappers?!?
Any help with this would be greatly appreciated.

Thanks in advance
 
You should use a Case statement rather than all those if's statements.

Search this forum, for case statement, you will find several examples.
 
However, you shouldn't have all those reports ... you only need one report, where you can pass you Genre through a parameter query.
 
Private Sub cmdPrintPreview_Click()
If Forms!frmWantedReports!cboGenres = "Chart" Then
DoCmd.OpenReport "rptWantedChart", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Dancehall/Reggae" Then
DoCmd.OpenReport "rptWantedDancehall", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Electro/Rap" Then
DoCmd.OpenReport "rptWantedElectro", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Funk/Soul" Then
DoCmd.OpenReport "rptWantedFunk", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Hardcore/Rave" Then
DoCmd.OpenReport "rptWantedHardcore", acPreview
Else
If Forms!frmWantedReports!cboGenres = "House" Then
DoCmd.OpenReport "rptWantedHouse", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Indie" Then
DoCmd.OpenReport "rptWantedIndie", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Jungle/Drum&Bass" Then
DoCmd.OpenReport "rptWantedJungle", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Techno/Trance" Then
DoCmd.OpenReport "rptWantedTechno", acPreview
Else
If Forms!frmWantedReports!cboGenres = "Turntablism" Then
DoCmd.OpenReport "rptWantedTurntablism", acPreview
Else
If Forms!frmWantedReports!cboGenres = "TV/Films" Then
DoCmd.OpenReport "rptWantedTV", acPreview
Else<---------Whats This For??? :D End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

DoCmd.Close<---Heres your problem, delete that and its all smooth
End Sub
 
Last edited:
Thanks guys, that runs nice and smooth. I got a bit carried away with the Else commands :eek:

I'm just experimenting with Access to build a database for all my records, i'll try the Case statement. Are there any pro and cons for using a Case instead of If/Else?

Thanks again
 
Would love to have only one report but haven't quite figured that bit out yet :confused:
 
Would love to have only one report but haven't quite figured that bit out yet

Are your reports bound to queries, where in each query you have typed the criteria yourself like "Chart", then other query with criteria like "House" etc

If that the case, simply delete all of your queries and leave just one, and in the criteria put this [Enter Your Genre] ... eventually delete all of your reports and leave just one report bounded to this query.

Then at runtime, you will be prompted to enter your genre preference, and the report will simply run according to your input.

There are better approaches as well, such as passing your criteria preference through an unbound textform from a form, but the above description will serve you as a start.
 
Thanks for the tip on using just the one report, after a bit of reading up i found the option to use filters, hopefully this looks a bit better:

Private Sub cmdPrintPreview_Click()
If IsNull(cboGenres) Then
DoCmd.OpenReport "rptWanted", acViewPreview
End If
If Forms!frmWantedReports!cboGenres = "Chart" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = Chart" & Genre
Else
If Forms!frmWantedReports!cboGenres = "Dancehall/Reggae" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = Dancehall/Reggae" & Dancehall
Else
If Forms!frmWantedReports!cboGenres = "Electro/Rap" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = Electro/Rap" & Electro
Else
If Forms!frmWantedReports!cboGenres = "Funk/Soul" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = Funk/Soul" & Funk
Else
If Forms!frmWantedReports!cboGenres = "Hardcore/Rave" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = Hardcore/Rave" & Hardcore
Else
If Forms!frmWantedReports!cboGenres = "House" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = House" & House
Else
If Forms!frmWantedReports!cboGenres = "Indie" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = Indie" & Indie
Else
If Forms!frmWantedReports!cboGenres = "Jungle/Drum&Bass" Then
DoCmd.OpenReport "rptWanted", acPreview, "Genre = Jungle/Drum&Bass" & Jungle
Else
If Forms!frmWantedReports!cboGenres = "Techno/Trance" Then
DoCmd.OpenReport "rptWantedTechno", acPreview, "Genre = Techno/Trance" & Techno
Else
If Forms!frmWantedReports!cboGenres = "Turntablism" Then
DoCmd.OpenReport "rptWantedTurntablism", acPreview, "Genre = Turntablism" & Turntablism
Else
If Forms!frmWantedReports!cboGenres = "TV/Films" Then
DoCmd.OpenReport "rptWantedTV", acPreview, "Genre = TV/Films" & TV
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

End Sub

My next task is to put it all in a case statement, is there less code with a case statement?
 
if you follow my suggestion and have just 1 parameter query and 1 report, you would not even need neither your ifs nor the case statement.
 
Done as you suggested max and it works like a treat.

Thanks for all the tips :)
 

Users who are viewing this thread

Back
Top Bottom