Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    Opening same Report with criteria from two forms

    Hi all,


    Good morning

    I have some reports that open from two different forms/subforms.
    What I am looking to do is figure out how to get this code (Below) to determine if form is open and if then, open report to the criteria.

    Currently when on main menu, i open report with dates entered and it works great. if I dont enter dates the Me.BetwenTransDate.Visible = False Shows #Name?, its not hidden
    If I open report from Account with dates entered it does not show the Me.BetwenTransDate.Visible = True and The Me.BetwenTransDateMM.Visible = False shows #Name?, its not hidden
    Not real sure how to get this to open report per criteria when opened from different forms, Any Ideas please


    Code:
    
    Private Sub Report_Open(Cancel As Integer)
    On Error Resume Next
    
    
    If Forms!frmMainMenu.sfrmMainMenu.Form.grpFilterOptions = 1 Then
        Me.BetwenTransDateMM.Visible = True
        Me.BetwenTransDate.Visible = False
    
    
    ElseIf Forms!frmAccount.sfrmAccount.Form.grpFilterOptions = 1 Then
        Me.BetwenTransDate.Visible = True
        Me.BetwenTransDateMM.Visible = False
    
    
    Else
    
    
        Me.BetwenTransDate.Visible = False
        Me.BetwenTransDateMM.Visible = False
        End If
        
    End Sub

  2. #2
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I tried this also and does not work correctly either?
    Code:
    Private Sub Report_Open(Cancel As Integer)
    On Error Resume Next
    If IsLoaded("frmMainMenu.sfrmMainMenu") Then
    If Forms!frmMainMenu.sfrmMainMenu.Form.grpFilterOptions = 1 Then
        Me.BetwenTransDateMM.Visible = True
        Me.BetwenTransDate.Visible = False
    End If
    If IsLoaded("frmAccount.sfrmAccount") Then
    ElseIf Forms!frmAccount.sfrmAccount.Form.grpFilterOptions = 1 Then
        Me.BetwenTransDate.Visible = True
        Me.BetwenTransDateMM.Visible = False
    End If
    Else
        Me.BetwenTransDate.Visible = False
        Me.BetwenTransDateMM.Visible = False
        End If
        
    End Sub
    
    
    ' I have this as a function in Mod
    Public Function IsLoaded(FormName As String) As Boolean
    
    
        IsLoaded = CurrentProject.AllForms(FormName).IsLoaded
    
    
    End Function

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Check out the 'OpenArgs' parameter in the 'DoCmd Openform' ......

  4. #4
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi Davegri,
    Here is the code for the DoCmd,
    I could just duplicate the reports but I would like to use the one report and be able to open it from two different forms/subforms with the different criteria
    The other DoCmd code from Accounts is the same but has different form/subform language
    What I was attempting to do was on the report make one txtbox visible and one not visible or both not visible depenting on what form i opened it with and what criteria
    Code:
    Private Sub CmdReport_Click()
    
    
    
    
    'On Error GoTo Err_Handler
        'Purpose:  Open the report filtered to the items selected in the list box.
        'Author:   Allen J Browne, 2004.   http://allenbrowne.com
        Dim varItem As Variant      'Selected items
        Dim strWhere As String      'String to use as WhereCondition
        Dim strDescrip As String    'Description of WhereCondition
        Dim lngLen As Long          'Length of string
        Dim strDelim As String      'Delimiter for this field type.
        Dim strDoc As String        'Name of report to open.
        
        If IsNull(Me.LstAccountReport.Column(0)) Then
            MsgBox "You must select a Report from Reprot List!"
        Exit Sub
        End If
        
        
        'strDelim = """"            'Delimiter appropriate to field type. See note 1.
         With Me.LstAccountReport
            For Each varItem In .ItemsSelected
               strDoc = .Column(1, varItem) 'assumes list box has two columns, the first one holding the reportID is the bound one and hidden
           'now open the report
    
    
            'DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
             DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter, OpenArgs:=strDescrip
           
        Next
     End With
    
    
        'Loop through the ItemsSelected in the list box.
        With Me.LstAccountType
            For Each varItem In .ItemsSelected
                If Not IsNull(varItem) Then
                    'Build up the filter from the bound column (hidden).
                    strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
                    'Build up the description from the text in the visible column. See note 2.
                    strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
                End If
            Next
        End With
        
        'Remove trailing comma. Add field name, IN operator, and brackets.
        lngLen = Len(strWhere) - 1
        If lngLen > 0 Then
            strWhere = "[AccountTypeID] IN (" & Left$(strWhere, lngLen) & ")"
            lngLen = Len(strDescrip) - 2
            If lngLen > 0 Then
                strDescrip = "AccountType: " & Left$(strDescrip, lngLen)
            End If
        End If
        
        'Report will not filter if open, so close it. For Access 97, see note 3.
        If CurrentProject.AllReports(strDoc).IsLoaded Then
            DoCmd.Close acReport, strDoc
        End If
        
        'Omit the last argument for Access 2000 and earlier. See note 4.
        'DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
        
        If Me.grpFilterOptions = 2 Then
            DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
        Else
        DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere & " AND " & Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter, OpenArgs:=strDescrip
        End If
    
    
    
    
    Exit_Handler:
        Exit Sub
    
    
    Err_Handler:
        If Err.Number <> 2501 Then  'Ignore "Report cancelled" error.
            MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdPreview_Click"
        End If
        Resume Exit_Handler
     
     
    End Sub

  5. #5
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Would it be something like? This is just an example as would have to have several more opening args

    DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip & "AND" & Reports!CategoryDetailIncomeRpt!BetwenTransDateMM. Visible = False

    Forget this, that wouldnt work as be to many forms to open! Has to be something on the report it self
    Sorry
    Ignore post

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    The openargs would be much simpler. It would simply indicate to the report's report_open event from where the docmd openreport came from (such as openargs=1 or openargs = 2).
    Then the code in the report_open event could decide how to identify which form/subform source to use to construct the criteria based on the openargs.

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    that makes sense, I will try to get this
    I am assuming it would be like
    DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip & "AND" & =1

    Then in report
    If Forms!frmMainMenu.sfrmMainMenu.Form.OpenArgs = 1 Then
    Me.BetwenTransDateMM.Visible = True
    Me.BetwenTransDate.Visible = False

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Not real sure how to even write that as the code for OpenArgs: in my code above would be like?

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    You are passing values in OpenArgs to the report, not a form?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Since so much depends on the listboxes, perhaps OpenArgs is not the best solution, as additional considerations must be taken if openargs is assigned multiple fields.
    If another report expert responder here can jump in, perhaps your existing code should be in a different report event?

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    In report open event, perhaps use a select case block like
    Select Case True
    I find it to be a very flexible thus powerful adaptation of Select Case as it allows you to create logical tests that you ordinarily cannot. If you pass a string to OpenArgs (doesn't have to be separated) your logical tests are practically unlimited as long as they make sense. Example
    Code:
    Select Case True
       Me.OpenArgs = "dog,cat"
          do something
       Me.OpenArgs = "bird,dog"
          do something else
       and so on
    End Select
    EDIT - not sure that helps without mentioning that the fact that it uses Me means it won't care what form opened the report.
    Last edited by Micron; 04-21-2023 at 10:03 AM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    I have to ask, are fields/controls available on Report Open?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by Welshgasman View Post
    I have to ask, are fields/controls available on Report Open?
    From a form? Certainly. From the opening report? No.
    Last edited by Micron; 04-21-2023 at 10:48 AM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I have two text boxes on Report. Visible No

    If I open the report from the main menu subform, I want BetwenTransDate to be not visible, The BetwenTransDateMM to be visible
    If I open the report from the Account subform, then I want BetwenTransDateMM date to not be visible, The BetwenTransDate to be visible
    If I have dates and is =1, If =2 I dont want to show either of them from either form....
    Code:
    This is what is on the report as txt boxes~
    
    BetwenTransDateMM
    ="For Transactions Between: " & [Forms]![frmMainMenu].[sfrmMainMenu].[Form]![BeginningTransDate] & " and " & [Forms]![frmMainMenu].[sfrmMainMenu].[Form]![EndingTransDate]
    BetwenTransDate
    ="For Transactions Between: " & [Forms]![frmAccount].[sfrmAccount].[Form]![BeginningTransDate] & " and " & [Forms]![frmAccount].[sfrmAccount].[Form]![EndingTransDate]
    Code:
        This is on the forms as a DoOpen. Both forms are same just different criteria of what form
    
    If Me.grpFilterOptions = 2 Then
            DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
        Else
        DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere & " AND " & Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter, OpenArgs:=strDescrip
        End If

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Quote Originally Posted by d9pierce1 View Post
    I have two text boxes on Report. Visible No

    If I open the report from the main menu subform, I want BetwenTransDate to be not visible
    If I open the report from the Account subform, then I want BetwenTransDateMM date to not be visible
    If I have dates and is
    Code:
    This is what is on the report as txt boxes~
    
    BetwenTransDateMM
    ="For Transactions Between: " & [Forms]![frmMainMenu].[sfrmMainMenu].[Form]![BeginningTransDate] & " and " & [Forms]![frmMainMenu].[sfrmMainMenu].[Form]![EndingTransDate]
    BetwenTransDate
    ="For Transactions Between: " & [Forms]![frmAccount].[sfrmAccount].[Form]![BeginningTransDate] & " and " & [Forms]![frmAccount].[sfrmAccount].[Form]![EndingTransDate]
    Code:
        This is on the forms as a DoOpen. Both forms are same just different criteria of what form
    
    If Me.grpFilterOptions = 2 Then
            DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
        Else
        DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere & " AND " & Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter, OpenArgs:=strDescrip
        End If
    And what is strDescrip meant to be?
    Just add the form name into the OpenArgs, and test for that in the report?
    You are alreadu using OpenArgs, so I do not see an issue?
    You could also just set a TempVar and test for that in the Report.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help opening report from form using form criteria
    By templeowls in forum Programming
    Replies: 6
    Last Post: 03-10-2022, 03:00 PM
  2. Opening a report using criteria from a form
    By craig1988 in forum Forms
    Replies: 4
    Last Post: 08-22-2016, 08:23 AM
  3. Opening Forms Via Criteria
    By jlgray0127 in forum Forms
    Replies: 4
    Last Post: 03-31-2015, 07:26 PM
  4. Replies: 3
    Last Post: 10-23-2013, 08:11 AM
  5. Opening a report with search criteria through a combo box
    By very_much_a_beginner in forum Reports
    Replies: 1
    Last Post: 07-16-2012, 01:30 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