Solved

Pass Form Data to Query Using VBA

Posted on 2013-11-19
7
865 Views
Last Modified: 2013-11-21
Hello,

I have the following problem. I have a form that filters the results of job tickets, and I would like to be able to click a value that appears in the RepairID column which will then run an "On Click" VBA event that will store the RepairID that I just clicked. That then stored RepairID will then be used to run a query. The RepairID only needs to be temporarily stored until the next "On Click" event is ran.

This is my envisioned way of it working however if someone has a better method I would like to know it.

Thanks!
0
Comment
Question by:mickferrari
  • 3
  • 2
  • 2
7 Comments
 
LVL 21
ID: 39661485
There are several ways to handle this.

1) In all version of Access you can simply reference the form control in the query.  This does require the form to remain opened. It can be set to not be visible.

Example in Query where:

Where RepairID  = Eval("forms.Yourformname.RepairIDControlNameHere")


2) If Access 2007 and later you can use teh TempVars collection as temporary storage. This does not require the form to remain opened.

In the form:

TempVars.Add “RepairID  ”, Me.RepairIDControlNameHere

This can be set with VBA code or a Macro

Example use in a query:

Where RepairID  = TempVars!RepairID
0
 
LVL 27

Expert Comment

by:jjafferr
ID: 39661624
Hi Mick

Usually the Query is not a final destiny, but a Record Source for a Form or a Report,
if that is the case, then you can send the Report (or Form) Filter directly from your open Form, like this:

docmd.openreport "Your Report Name", acviewpreview
Reports!YourReportName.Filter= "RepairID = " & me.RepairID
Reports!YourReportName.Filteron= true

Thus not touching the Query :)

jaffer
0
 

Author Comment

by:mickferrari
ID: 39664132
Hi TheHiTechCoach,

I have tried your solution but I'm getting an error (please see attachment).

This is the code I am using to store the data:

TempVars.Add “RepairID”, Me.RepairID
Untitled.jpg
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 21

Accepted Solution

by:
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 500 total points
ID: 39664581
mickferrari,

Which event are you using to  execute TempVars.Add “RepairID”, Me.RepairID ? Are you using the On Click event of a text box control?

Is the control on the form named RepairID (this may not be the same names as the field in the control source)?


@jaffer,  Filtering at the query level is usually more efficient than filtering at the form or report level. You want to retrieve as few records as possible at the query level.

It is also more efficient to use the Where parameter of the DoCmd.OpenReport over applying a filter later.  

Example:

Docmd.Openreport "Your Report Name", acviewpreview, , "RepairID = " & me.RepairID

Open in new window

0
 

Author Comment

by:mickferrari
ID: 39664757
TheHiTechCoach,

I've got it working now, thanks a lot for your help.

Just to answer your other questions. I am using an onclick event to a text box and the control name is RepairID.
0
 
LVL 21
ID: 39664856
Thanks for the update. Glad to hear you got it working.

Good luck with your project.
0
 
LVL 27

Expert Comment

by:jjafferr
ID: 39667125
Hi TheHiTechCoach

I know that :)
My way is an option, to avoid having a few similar queries, but with different criteria's :)

And yes, the criteria can be set from the DoCmd.OpenReport line,
but there is also an ORDERBY command that can be sent using my way, which cannot be set using DoCmd.OpenReport command :)

jaffer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

830 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