Solved

Pass Form Data to Query Using VBA

Posted on 2013-11-19
7
857 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 21

Accepted Solution

by:
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the update. Glad to hear you got it working.

Good luck with your project.
0
 
LVL 27

Expert Comment

by:jjafferr
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This article will show you how to use shortcut menus in the Access run-time environment.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

728 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

14 Experts available now in Live!

Get 1:1 Help Now