Results 1 to 6 of 6
  1. #1
    roaftech is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2012
    Location
    Romania
    Posts
    65

    Dynamic References (form and report names held in string variables)

    I work with an aid charity and am developing a database which keeps details of aid donations and where the principal output from the database is various standard reports. The common feature of most of these reports is that they require specific date ranges, so I prepared a parameter form which has buttons preset for the various date combinations. When a report is opened from the switchboard it opens the parameter form (if not already open) and switches focus to the form. The form has another button "Apply Filter" which closes the report and re-opens it using the date options as a filter. The form also has a "Close Report" button for ending the session tidily. So far, so good. Now I would like to use this form or a similar form with most of my reports.



    In this instance the form fS1DateSelector assembles the filter 'strFilter' and passes it to the report as part of a OpenReport instruction The report_open code includes the following line
    Code:
    Me.Filter = Forms!fS1DateSelector.strFilter
    Everything works fine, indicating that all the necessary declarations, etc, have been dealt with elsewhere in the code.
    I would like to replace the line above with something like this, where the form name is dynamic
    Code:
    Me.Filter = "[forms]![" & strFrmName & "].[strFilter]"
    However, this does not work as required. The code appears to resolve to the correct call but then it opens a dialog box with the prompt 'Enter [forms]![fS1DateSelector].[strFilter]' .
    I have a similar problem with the following:
    Code:
        With Reports![rStockList]
           .Filter = strFilter
           .FilterOn = True
    where I want to replace '[rStockList]' with a dynamic reference but all my permutations have resulted in syntax errors.

    What is the correct syntax for dynamic references in these contexts?
    Many thanks.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Why does the form name need to be dynamic? Is report opened from more than one form?

    strFilter is a variable or a textbox on form?

    Use the WHERE CONDITION argument of OpenReport and the Me.Filter code is not needed.

    DoCmd.OpenReport "reportname", , , strFilter
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    roaftech is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2012
    Location
    Romania
    Posts
    65
    Many thanks for your reply. I was trying to find a balance between providing enough info for the context and overloading the first post with confusing detail.

    Operating sequence
    1: The report is opened from a switchboard menu item, and then it opens the form (if not already open);
    2: The form controls set up the filter and then reopen the report;
    3: Repeat 2 as often as necessary;
    4: The form has a button which closes the report (if not closed by its own close button) and then closes the form.

    Quote Originally Posted by June7 View Post
    Why does the form name need to be dynamic? Is report opened from more than one form?
    I can anticipate a situation where the report could be called from another form, eg when the beneficiary is the criteria instead of the date. However, I haven't thought through the details of this yet and it may be that we go down a separate route.

    Quote Originally Posted by June7 View Post
    strFilter is a variable or a textbox on form?
    strFilter is a variable which is declared and populated in the form, in this sequence
    Code:
    Option Explicit
    Option Compare Database
    
    Private Const strFrmName As String = "fS1DateSelector"  'parameter form
    Private Const strQryName As String = "qAvizDataAll"     'data source query
    Public strRptName As String   'report name
    Public intFirstDate As Date   'first date
    Public intFinalDate As Date   'final date
    Public strFilter As String    'assembled filter
    ------------------------------------------    
    Private Sub cmdApplyFilter_Click()
    ' assemble the filter using the date ranges
        strFilter = "[data] between [forms].[" & strFrmName & "].[firstdate] and [forms].[" & strFrmName & "].[finaldate]"
        
    ' If the report is open then close it. Reopen the report with new filter.
        If SysCmd(acSysCmdGetObjectState, acReport, strRptName) = acObjStateOpen Then
            DoCmd.Close acReport, strRptName
        End If
            DoCmd.OpenReport strRptName, acViewPreview, strFilter
    End Sub

    Quote Originally Posted by June7 View Post
    Use the WHERE CONDITION argument of OpenReport and the Me.Filter code is not needed.
    DoCmd.OpenReport "reportname", , , strFilter
    Comparing your code with my version above, it seems that I have confused FilterName and WhereCondition, and I need another comma.
    Do I still need the 'FilterOn=True' statement when using that process?

    I'll come back to the Reports! context described in the first post in a separate post in order to avoid prolonging this one.
    Many thanks for your assistance, not just with this but also with your responses to other questions, which I have read 'on the quiet'.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Do not need the FilterOn=True code when using WHERE CONDITION.

    Date values need # delimiter.

    You don't want the form/control names in the strWHERE construct, just the date values that are in the controls for the form.

    Is [data] the actual name of Date/Time field in the report RecordSource? Try:

    strFilter = "[data] BETWEEN #" & Forms(strFrmName).[firstdate] & "# AND #" & Forms(strFrmName).[finaldate] & "#"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    roaftech is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2012
    Location
    Romania
    Posts
    65
    Many thanks for your suggestions - I'll give them a go.

    The forms and report are presented in Romanian whilst the coding is in English. I tend to use Romanian names for those fields which our staff will use and English abreviations for others. It does not help that 'data' is Romanian for 'date' and 'date' is Romanian for 'data'.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Then don't use 'data' and 'date' for field names. Reserved words should not be used as field names anyway.

    Better would be something like DateEnter (or the Romanian equivalent).
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 3
    Last Post: 07-26-2012, 03:09 PM
  2. Dynamic field names per record
    By snofrandy in forum Queries
    Replies: 1
    Last Post: 05-30-2012, 02:50 PM
  3. Replies: 3
    Last Post: 04-23-2012, 12:06 AM
  4. Emailing report to address held in field
    By ham355 in forum Reports
    Replies: 4
    Last Post: 02-21-2012, 01:07 PM
  5. Create dynamic connection string to Access
    By janwane in forum Access
    Replies: 2
    Last Post: 10-07-2011, 06:56 AM

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