Results 1 to 4 of 4
  1. #1
    louise is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    112

    Add an If statement behind a button on a form to determine which report to open

    Hello,
    I have an "Open Report" button on a form, but would like it to select which of several forms to open, depending on the value of a field.
    Presumably, this would be in a code behind "OnClick" but I haven't been able to figure out how to proceed.



    I would appreciate any suggestions!
    Thanks

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Where are you stuck? You can create an event handler by creating a Sub Routine. This is where you place your VBA. Use the VBA editor to build your code. You can create a Sub Routine from the properties sheet. Within the Event Tab, use the ellipses for the desired Event. Then, select "Code Builder".

    Your Code would look something like...

    Code:
    If Me.ControlName.Value = 1 then
    'Perform something here
    Else If Me.ControlName.Value = 2
    'Perform something else
    else
    'Perform the default
    End if

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    I have a form frmReports where
    user can pick from a list box , table tReports,
    to print.

    DoCmd.OpenReport lstRpt, acViewPreview

  4. #4
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I agree that a slick method is to use a listbox or combobox whose record source is tied to a table of reports. It easily allows for expansion of the reports list. Just be sure to prevent multi-selecting on the control, and pass the value chosen to a little routine that decides which report to open. In that routine, I use Case Select instead of a lot IFs because it is a neat way of compartmentalizing things. You can run blocks of code within each statement. Also, if your design would permit the report to be run again after changing a date range input for example, it is best to close the report before running code to open it. Otherwise, it will usually reopen with the same data constraints. I use If CurrentProject.Allreports("rptName").IsLoaded then" to test for this and prevent an error on trying to close something that is not open.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Open Report Form Button
    By zmbaker in forum Forms
    Replies: 9
    Last Post: 07-07-2014, 03:11 PM
  2. Replies: 3
    Last Post: 07-11-2013, 11:52 AM
  3. Replies: 2
    Last Post: 05-11-2012, 11:52 AM
  4. Open Current Report With Form Button
    By Rick5150 in forum Reports
    Replies: 8
    Last Post: 10-12-2011, 02:28 PM
  5. Replies: 2
    Last Post: 05-26-2011, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums