Pass variable in Query Criteria

fenhow

Registered User.
Local time
Today, 13:54
Joined
Jul 21, 2004
Messages
599
Is there any way to pass a variable from one criteria on a query to five other criterias in the same query.

I am using this code to select which person the query is running on

Like "*" & [Enter Part of The Landman Name or Enter For All Records] & "*"

I have five areas in this query where I would need to add the name, I just want to do it once?

Thanks.

Fen how
 
Use a FORM, not input parameters. Put it on a form and someone can select, or type it ONCE and you can use it as many times as necessary. You really shouldn't have users using tables/queries directly as you lose much control and you cannot limit what they do.
 
Thanks Bob, the report is created at runtime and I have a window that pops up asking for what person? They enter part of the name and the query pulls all the records with that persons name in the record. They do not have any access to the query or report execpt what is prompted when they select the button to run the report..

Does this make sense?

Fen
 
Thanks Bob, the report is created at runtime and I have a window that pops up asking for what person? They enter part of the name and the query pulls all the records with that persons name in the record. They do not have any access to the query or report execpt what is prompted when they select the button to run the report..

Does this make sense?

Fen

It makes sense, but still use a form to pass parameters as you can see you are suffering from the fact that an input parameter is good only once and if you want to use it again or for other things you can't and you have to enter it over and over; Use a form....
 
Ok, i understand. So how do i get if from the form to the query?

Thanks
 
If the form is still open (it can be hidden, but has to be open for this to work) you can just refer to the fields in your query underlying the report.

So, for example, if you had a date field you wanted to restrict things by you could use:
Code:
Between [Forms]![frmInput]![StartDate] And [Forms]![frmInput]![EndDate]
in the query's criteria
 
Or, in the case of your Landman name:

[Forms]![YourInputFormName]![YourLandmanTextBoxWhereTheUserTypesInTheName] & "*"

If you want to use just the first few letters.
 
So if i create a unbound textbox on the form where the button is to create the report say call it name,

I go to my query and place?

[Forms]![frmReports]![name]

If this is so do i place this in every place i want this var to show up?

Thanks.

Fen
 
That would be the thing to replace in any query criteria where you previously had
[Enter Part of The Landman Name or Enter For All Records]

But, if you want it to act like it previously did then you would use:

Like "*" & [Forms]![frmReports]![name] & "*"

But, I would rename the control named name as NAME is an Access reserved word and that can cause you grief if you try to use it as such.
 
THanks,

One more thing

With this

Like "*" & [Forms]![frmReports]![LandmanName] & "*"

how can i exclude all records that say "Not Assigned"

Fen
 
If it is the same field then it would be:

Like "*" & [Forms]![frmReports]![LandmanName] & "*" AND <> "Not Assigned"
 
Simple Software Solutions

Bob is right in wha he is suggesting, I, however, use a different approach. The reason why is that the unerlying query is bound to that form and that form only. Lets say you want to run the same query from a different place in your database you would have to create a duplicate query but reference it to the new calling form. This is a bind as you need to constantly make sure that each version is identical if you make any changes.

Lets imagine you want to pass a date range to the query for filtering purposes.

First create two public variables within your startup module.

Public DteDateLower As Date
Public DteDateUpper As Date

Next Create two Functions

Public Function GetDateLower() As Date

GetDateLower = DteDateLower

End Function


Public Function GetDateUpper() As Date

GetDateUpper = DteDateUpper

End Function


In your query on the condition row underneath your filter date column enter:

Between GetDateLower() And GetDateUpper()

Finally, in a form, any form, the user will enter a date range for the filter on the AfterUpdate Event of each control enter the following code:

Private Sub TxtDateLower_AfterUpdate

DteDateLower = Me.ActiveControl

End Sub


Private Sub TxtDateUpper_AfterUpdate

DteDateUpper = Me.ActiveControl

End Sub

So when you run the query the two functions are called which then pass the date range to the query irrespective of where it is called from. You can do this with any type of field using any conditions.

CodeMaster::cool:http://www.icraftlimited.co.uk
 
I can't seem to find or create a startup module. how do I create this please?

Thanks
 
From the database window click on Modules then select New. Save the code there and save it.
 
I believe that I have tried to take on board the relevant comments from the threads above but without success.

I have a table which is accessed via the form frmUpdateExistingItems.

To reduce the combo box pick list of the EquipmentNumber field of the records in the table, I want the operator to enter a string such as "FA" (for a FAN) or "GB" (for a gearbox) in the text box SearchString. This string is intended to be the criteria for the query qry21PrepShortListOfEqNumbers which is activated by the On Key Up property of the SearchString text box. The property sheet of the SearchString text box contains:

="[qry21PrepShortListOfEqNumbers]"

The query SQL code is:

SELECT tblMainEquipList.EquipID
FROM tblMainEquipList
WHERE (((tblMainEquipList.EquipID) Like "* & [forms]![frmUpdateExistingItems]![SearchString] & *"))
ORDER BY tblMainEquipList.EquipID;

I have entered GB in the SearchString text box as I know that there are records containing GB. This does not give a error message on leaving the SearchString text box.

The EquipmentNumber box has ="[qry21PrepShortListOfEqNumbers]" plastered on almost every permutation of On/Got/Lost/Key/Mouse/Up/Down etc properties. When trying to select an equipment number in the EquipmentNumber box no items are available.

Please advise what correction I need to undertake.

Barry Cuthbertson
 
I wish to report success.

My initial post included misleading information. Here is the clarification.

I have two text boxes, One is titled SearchString where the user enters letters that are part of an agreed code that represents a type of equipment (Fan = FA, Gearbox = GB etc). I wanted to then use this input to narrow down the number of equipment number items in the second text box (which is actually called EquipID). Mia Culpa:o.

Further thought and assistance from another forum has lead to some code shuffling and a successful outcome:). I am now using thus structure:

1. The query SQL code is:
SELECT tblMainEquipList.EquipID
FROM tblMainEquipList
WHERE (((tblMainEquipList.EquipID) Like "*" & [forms]![frmUpdateExistingItems]![SearchString] & "*"))
ORDER BY tblMainEquipList.EquipID; (or did I use % instead of *???)
2. Generated the SearchString_AfterUpdate() sub which includes:
DoCmd.OpenQuery "qry21PrepShortListOfEqNumbers", acViewNormal, acEd
Me.EquipID.Requery
3. Called up this routine in the SearchString text box AfterUpdate event property.
4. Revised the EquipID combobox RowSource to qry21repShortListOfEqNumbers without any of the =”[…….]” trimming.
5. Removed all Events properties from the EquipID box.

This now gives a reduced list of equipment numbers items in the EquipID box.

A footnote is that the equipment number is typically of the form CV01.GB01.FA01.MT01 which is a conveyor gearbox cooling fan motor.

I hope that this conclusion assists someone else in the Access community.

Regards
Barry Cuthbertson.
 

Users who are viewing this thread

Back
Top Bottom