473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

execute a query with parameter in a vba code

I wanna execute a query with one parameter from MS VBA.
the trouble is that the parameter is a control in a form.
and I still don't know what is the code I need...
Nov 12 '05 #1
3 26054
Simplest way it to just create the query string on the fly and execute it:

strSQL = "UPDATE ... WHERE SomeField = " & Forms!MyForm!MyTextBox & ";"
dbEngine(0)(0).Execute strSQL, dbFailOnError

Alternatively, you can supply the Parameter for the QueryDef, and execute
that:

Dim qdf As DAO.QueryDef
Set qdf = db.QueryDefs("MyQuery")
qdf.Parameters("Forms!MyForm!MyTextBox") = Forms!MyForm!MyTextBox
....

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"D-Zyl" <d_*****@hotmail.com> wrote in message
news:ef**************************@posting.google.c om...
I wanna execute a query with one parameter from MS VBA.
the trouble is that the parameter is a control in a form.
and I still don't know what is the code I need...

Nov 12 '05 #2
Allen Browne wrote:
Simplest way it to just create the query string on the fly and execute it:

strSQL = "UPDATE ... WHERE SomeField = " & Forms!MyForm!MyTextBox & ";"
dbEngine(0)(0).Execute strSQL, dbFailOnError

Alternatively, you can supply the Parameter for the QueryDef, and execute
that:

Dim qdf As DAO.QueryDef
Set qdf = db.QueryDefs("MyQuery")
qdf.Parameters("Forms!MyForm!MyTextBox") = Forms!MyForm!MyTextBox
...

If all your parameters are form controls, then you can:

For Each prm In qdf.Paramters
prm.value = eval(prm.name)
Next

I use that in a generic procedure, my latest domain function
replacements
(http://easyweb.easynet.co.uk/~trevor.../baslookup.zip) use this
technique.

--
Error reading sig - A)bort R)etry I)nfluence with large hammer
Nov 12 '05 #3
Good trick, Trevor.

You would need to check that the control was not Null first, as Eval() does
not handle nulls.

As Trevor would know, it is also a *very* good idea to declare these
parameters. In query design, choose Parameters on the Query menu, and enter
the name and data type. Something like:
Forms!MyForm!MyTextBox Long

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Trevor Best" <nospam@localhost> wrote in message
news:40**********************@auth.uk.news.easynet .net...
Allen Browne wrote:
Simplest way it to just create the query string on the fly and execute it:
strSQL = "UPDATE ... WHERE SomeField = " & Forms!MyForm!MyTextBox & ";"
dbEngine(0)(0).Execute strSQL, dbFailOnError

Alternatively, you can supply the Parameter for the QueryDef, and execute that:

Dim qdf As DAO.QueryDef
Set qdf = db.QueryDefs("MyQuery")
qdf.Parameters("Forms!MyForm!MyTextBox") = Forms!MyForm!MyTextBox
...

If all your parameters are form controls, then you can:

For Each prm In qdf.Paramters
prm.value = eval(prm.name)
Next

I use that in a generic procedure, my latest domain function
replacements
(http://easyweb.easynet.co.uk/~trevor.../baslookup.zip) use this
technique.

--
Error reading sig - A)bort R)etry I)nfluence with large hammer

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: The Plankmeister | last post by:
Hi... I have a query which I'm accessing through PHP as a stored procedure. However, I need to be able not to pass a couple of parameters in certain situations. When I do this, I get an error: ...
15
by: deko | last post by:
I need a way to create a table with a programmatically defined name. I have a Make Table query that will create the table with the name that I put in the query, but I don't know how to (or if I...
3
by: cassandra.flowers | last post by:
Hi, I was wondering if it is possible (Using access) to have a query parameter as a drop down box rather than a text box? e.g. typing as criteria for a query produces a box with a text box...
3
by: Uwe | last post by:
Howdy! I've googled and googled for this without success, and either it's so easy that nobody has ever asked, or there is some serious reason nobody would do this. But I'm going to ask anyway....
1
by: rdemyan via AccessMonster.com | last post by:
My application has a table that contains information on buildings. The building data comes from another database and is imported from a spreadsheet into my application. Originally, I thought...
6
by: DeanL | last post by:
Hi All, How do you run a SELECT query from code? I know that DoCmd.RunSQL only works with action or DDF queries but I need to run a query from VBA and add variables into the query as defined by...
0
by: HomerS007 | last post by:
Hi, I'm using asp.net 2.0 and sql server 2000 for my first ever project. On one of the page in the application, I want to limit what the user can see based on his/her login. It's a page that...
1
by: ezderek | last post by:
For many years if I wanted to run an action query from code, I've used code something like this --- dim qd as query dim sSQL as string dim db as database set db = currentdb() sSQL = "UPDATE...
1
by: jmarcrum | last post by:
Hey everyone, I have a question that's been troubling me for a bit. I work for Alabama Power. I have 1 table (tblOutages). Every morning I import a list of Power Outages for the state of...
1
by: codexxx | last post by:
Hi All, I have got a requirement from a client who wants the query parameter to be hidden in the browser. What he wants is like following. If a url link becomes like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.