Using the same variable in several queries

I have a report (rpt_EventsLog) that has 2 sub reports (subrpt_on duty today_jr) and (subrpt_on duty today_sr).

I want to enter the date one time, and use it for the queries on each of those reports, but right now it asks the question ([Date?]) three times?
TIgerVAsked:
Who is Participating?
 
Dale FyeCommented:
Another option, which I use frequently is to Create a TempVar for your new variable.  So in the AfterUpdate event of your form control, you would include:
Private Sub txt_DateControlName_AfterUpdate

    Tempvars!ReportDate = me.txt_DateControlName.Value 

End Sub

Open in new window

Then, in your queries, you could use:

SELECT * FROM yourTable where [DateField] = [Tempvars]![ReportDate]

and would not need to declare that in your query.
0
 
PatHartmanCommented:
Add the criteria fields to a form and run the report from that form.  Change the query to reference the form fields rather than to prompt.

Where SomeField = Forms!MyForm!SomeField
0
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
and define Forms!MyForm!SomeField as a Parameter, otherwise you may have problems down the line when queries call on that query (especially Crossqueries).
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"but right now it asks the question ([Date?]) three times?"

You need to have the *exact* same Prompt in each of the three (?) RecordSources involved ... the main report, and both sub reports, in which case it should ... only ask once.

mx
0
 
TIgerVAuthor Commented:
It is the same (jr. and sr are the same query, just copied and one option changed)  Sub reports don't pass the answer
0
 
PatHartmanCommented:
So have you tried the form or tempvar solution yet?
0
 
TIgerVAuthor Commented:
I have not, I will be back at it first thing in the morning.   After reviewing it, I'm sure the Tempvar solution will be the working one.
0
 
PatHartmanCommented:
They BOTH work.  The form field reference doesn't require any code but the tempvar solution does.  That is the difference.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Well ... and the solution I posted doesn't require anything additional beyond what the OP currently has. Just change the prompts to match each other :-)
I posted that for an immediate quick fix.

The ultimate solution is of course either of the other two :-)

mx
0
 
PatHartmanCommented:
Joe,
Access prompts multiple times for arguments for reports.  So, even if the query of the subreport uses the same argument as the query of the main report, two prompts happen because Access runs two queries.  The only time Access only prompts once is if the queries are nested or the argument is used multiple times in the same query.

In most cases, the subreport should be relying in the master/child links rather than arguments though to avoid this issue.   That's what I do and probably what you do.  But in any event, if you open a report and enter a value at the prompt and then decide to print the report, you get prompted again.

All in all , it is much less annoying to use forms to provide values.  That also allows you to validate them before using them.
0
 
TIgerVAuthor Commented:
Thank you!
0
 
Helen FeddemaCommented:
You could also save the Date value to a custom database property, and then retrieve it in a query or elsewhere using the GetProperty function.  Here is some for creating and retrieving custom db properties:

[sample code fragment using the procedure]

   Dim dbs As DAO.Database
   Dim lngCount As Long
   Dim lngID As Long
   Dim rpt As Access.Report
   Dim rst As DAO.Recordset
   Dim strPrompt As String
   Dim strQuery As String
   Dim strRecordSource As String
   Dim strReport As String
   Dim strSQL As String
   Dim strTitle As String
   
   strRecordSource = "tblInventoryItemsComponents"
   strQuery = "qryTemp"
   Set dbs = CurrentDb

   'Numeric filter
   lngID = Nz(Me![ID])
   If lngID <> 0 Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[ID] = " & lngID & ";"
   End If

   'String filter
   strInventoryCode = Nz(Me![InventoryCode])
   If strInventoryCode <> "" Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[InventoryCode] = " & Chr$(39) & strInventoryCode & Chr$(39) & ";"
   End If

   'Date range filter from custom database properties
   dteFromDate = CDate(GetProperty("FromDate", ""))
   dteToDate = CDate(GetProperty("ToDate", ""))
   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

   'Date range filter from controls
   If IsDate(Me![txtFromDate].Value) = True Then
      dteFromDate = CDate(Me![txtFromDate].Value)
   End If

   If IsDate(Me![txtToDate].Value) = True Then
      dteToDate = CDate(Me![txtToDate].Value)
   End If

   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

   Debug.Print "SQL for " & strQuery & ": " & strSQL
   lngCount = CreateAndTestQuery(strQuery, strSQL)
   Debug.Print "No. of items found: " & lngCount
   If lngCount = 0 Then
      strPrompt = "No records found; canceling"
      strTitle = "Canceling"
      MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
      GoTo ErrorHandlerExit
   Else
      'Use this line if you need a recordset
      Set rst = CurrentDb.OpenRecordset(strQuery)
   End If

   'Use SQL string as the record source of a form
   strFormName = "fpriLoadSoldPackingSlip"
   DoCmd.OpenForm FormName:=strFormName, _
      view:=acDesign
   Set frm = Forms(strFormName)
   frm.RecordSource = strSQL
   DoCmd.OpenForm FormName:=strFormName, _
      view:=acNormal
   
   'Use SQL string as the record source of a report
   strReport = "rptLoadSold"
   DoCmd.OpenReport ReportName:=strReport, _
      view:=acViewDesign, _
      windowmode:=acHidden
   Set rpt = Reports(strReport)
   rpt.RecordSource = strSQL
   DoCmd.OpenReport ReportName:=strReport, _
      view:=acViewNormal, _
      windowmode:=acWindowNormal

   'The report has the filtered query as its record source

'Create a sorted rather than filtered query
Private Sub fraChoice_AfterUpdate()

   Dim intChoice As Integer
   Dim strSort As String
   Dim strRecordSource As String
   Dim strSQL As String
   Dim strQuery As String
   Dim lngcount As Long
   
   intChoice = Nz(Me![fraChoice], 1)
   strSort = Switch(intChoice = 1, "CategoryName", _
      intChoice = 2, "Location", _
      intChoice = 3, "Room")
   
   'Create SQL and save sorted query
   strRecordSource = "tblCompanyInfo"
   strQuery = "qrySortedInfo"
   strSQL = "SELECT * FROM " & strRecordSource _
      & " ORDER BY [" & strSort & "];"

   Debug.Print "SQL for " & strQuery & ": " & strSQL
   lngcount = CreateAndTestQuery(strQuery, strSQL)
   Debug.Print "No. of items found: " & lngcount
   
End Sub

=========================

Public Function CreateAndTestQuery(strTestQuery As String, _
   strTestSQL As String) As Long
'Created by Helen Feddema 28-Jul-2002
'Last modified 11-May-2013

On Error Resume Next
   
   Dim qdf As DAO.QueryDef
   Dim rst As DAO.Recordset
   
   'Delete old query
   CurrentDb.QueryDefs.Delete strTestQuery

On Error GoTo ErrorHandler
   
   'Create new query
   Set qdf = CurrentDb.CreateQueryDef(strTestQuery, strTestSQL)
   
   'Test whether there are any records
   Set rst = CurrentDb.OpenRecordset(strTestQuery)
   With rst
      .MoveFirst
      .MoveLast
      CreateAndTestQuery = .RecordCount
   End With
   
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   If Err.Number = 3021 Then
      CreateAndTestQuery = 0
      Resume ErrorHandlerExit
   Else
   MsgBox "Error No: " & Err.Number _
      & " in CreateAndTestQuery procedure; " _
      & "Description: " & Err.Description
   End If
   
End Function

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.