Solved

Using the same variable in several queries

Posted on 2014-10-02
12
119 Views
Last Modified: 2014-10-03
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?
0
Comment
Question by:TIgerV
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 40357160
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
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40357176
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
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40357393
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
 
LVL 75
ID: 40357630
"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
 

Author Comment

by:TIgerV
ID: 40358151
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
 
LVL 34

Expert Comment

by:PatHartman
ID: 40358195
So have you tried the form or tempvar solution yet?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:TIgerV
ID: 40358206
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
 
LVL 34

Expert Comment

by:PatHartman
ID: 40358218
They BOTH work.  The form field reference doesn't require any code but the tempvar solution does.  That is the difference.
0
 
LVL 75
ID: 40358228
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
 
LVL 34

Expert Comment

by:PatHartman
ID: 40358242
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
 

Author Closing Comment

by:TIgerV
ID: 40359981
Thank you!
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40360032
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now