John1967 said:
I have a combo box that contains 3 items; I want the user to select one item
and for that selection to initiate a macro to open a report in preview mode.
I have created 3 reports and 3 macros. Sounds simple in concept but I just
can't seem to get the right code to do what I need. Would I use If, Then,
Else logic here?
Combo Box:
Main Issue
Contributor
Status
Macros:
OpenReport Main Issue
OpenReport Contributor
OpenReport Status
Thanks for any advice to this Access newbie.
John----
What I'd do:
Create a table to contain the list of report names, but with a numeric
(integer) ID field as the first field. Make the ID the primary key -
you'll need to enter unique values.
Create my combo box with the wizard turned ON, so that it asks you how
it should be set up. Choose the table of report names as the control
source, and store the numeric identifier but choose to hide it ("key
column") in the combo box display when asked. This'll make sense when
you run it if it doesn't now - Access wizards are brilliant.
In the combo-box properties page, click the selector at the end of the
After Update event - choose "Code Builder". I always use code modules -
allows you to include error code. See:
http://allenbrowne.com/ser-23a.html
(just the 9 numbered lines will do).
In the body of your event handler, use a SELECT ... CASE statement based
on the numeric value of the (updated) combo box to launch the report you
want (or a msgbox alert in the CASE ELSE branch).
Launch the report using:
DoCmd.OpenReport <reportname>, (see Help for other arguments).
Yup, that's essentially a macro, but this way it can be wrapped in error
handling code.
I still shudder at the recollection of the day I decided not to test for
errors in a particularly important function I was writing because
"that'll only generate an error if I've made a programming mistake". O,
the arrogance of youth... Later I'd wrapped this code in another
function, passing in a critical argument and introducing a (common)
programming mistake. Took us weeks to find it. Always use error
handling code.
HTH
Phil, London