473,388 Members | 1,493 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,388 software developers and data experts.

Databound list box populated with parameterized query?

Hello.

I am developing an Access 2000 form, and I want to populate a
databound list box using a saved query that requires parameters.
Is there any way to do this? I tried creating a DAO querydef
object and setting the parameters, and then assigned the querydef
object to the list box "rowsource" property. This did not work.

Thanks
-Mark
Feb 7 '06 #1
7 6191
> I am developing an Access 2000 form, and I want to populate a
databound list box using a saved query that requires parameters.
Is there any way to do this? I tried creating a DAO querydef
object and setting the parameters, and then assigned the querydef
object to the list box "rowsource" property. This did not work.


I use a function to pass parameters to queries most of the time. If I need
to pass a file path or some other string that may contain quotes or
potential problem characters, then I use a parameter query.

If you can get the value needed from a control, or put it in a hidden
textbox on a form, you can use this function in the query.

example:

SELECT * FROM qryMyQuery WHERE MyField_ID = QryPrm("frmMain", "txtMyId")

Public Function QryPrm(ByVal strFrm As String, ByVal strCtl As String, _
Optional ByVal strSubFrm As String, Optional ByVal strSubFrmCtl As
String) _
As Variant
If Len(strSubFrmCtl) = 0 Then
If Len(strSubFrm) = 0 Then
QryPrm = Forms(strFrm).Controls(strCtl)
Else
QryPrm = Forms(strFrm).Controls(strCtl).Form.Controls(strSu bFrm)
End If
Else
QryPrm =
Forms(strFrm).Controls(strCtl).Form.Controls(strSu bFrm).Form.Controls(strSubFrmCtl)
End If
End Function
Feb 7 '06 #2
"deko" <de**@nospam.com> wrote:

If you can get the value needed from a control, or put it in a hidden
textbox on a form, you can use this function in the query.

example:

SELECT * FROM qryMyQuery WHERE MyField_ID = QryPrm("frmMain", "txtMyId")


That works, thank you very much.

I find it odd that Access does not support a syntax for executing queries
with parameters,
without resorting to object references for the parameters. But as long as
there is a way to
do it, I guess I won't complain too much.

-Mark

Feb 8 '06 #3
"deko" <de**@nospam.com> wrote:

If you can get the value needed from a control, or put it in a hidden
textbox on a form, you can use this function in the query.

example:

SELECT * FROM qryMyQuery WHERE MyField_ID = QryPrm("frmMain", "txtMyId")


That works, thank you very much.

I find it odd that Access does not support a syntax for executing queries
with parameters,
without resorting to object references for the parameters. But as long as
there is a way to
do it, I guess I won't complain too much.

-Mark


Feb 8 '06 #4
Maybe I'm missing something here ...but

I cant see that using the function in this case is any different than
coding:

SELECT * FROM qryMyQuery WHERE MyField_ID = forms!frmMain!txtMyId

seeing you have the form and the control hard coded as parameters in
the query anyway.

It would be nice if the form and control names could be provided at run
time but I can't see a simple way of doing this.

Mark wrote:
"deko" <de**@nospam.com> wrote:

If you can get the value needed from a control, or put it in a hidden
textbox on a form, you can use this function in the query.

example:

SELECT * FROM qryMyQuery WHERE MyField_ID = QryPrm("frmMain", "txtMyId")


That works, thank you very much.

I find it odd that Access does not support a syntax for executing queries
with parameters,
without resorting to object references for the parameters. But as long as
there is a way to
do it, I guess I won't complain too much.

-Mark


Feb 8 '06 #5
> I cant see that using the function in this case is any different than
coding:

SELECT * FROM qryMyQuery WHERE MyField_ID = forms!frmMain!txtMyId


It's not. That function is just my way to get variables into queries. I
find it easy to work with, debug and troubleshoot.

There's different ways to do it - that's just my preference, most of the
time.

I also like to do this (for SELECT queries):

Dim db as DAO.Database
Dim qdfs as DAO.Querydefs
Dim qdf as DAO.Querydef
Set db = CurrentDB
Set qdfs = db.Querydefs
Set qdf = qdfs(strQryName)
strSql = "dynamically created statement here"
qdf.SQL = strSql

And this (for simple action queries):

db.Execute strSql, dbFailOnError

And sometimes this (for more complex action queries):

qdf.Parameters("prmWhatever") = "parameter"
qdf.Execute

It all depends on the situation at hand.
Feb 9 '06 #6
Thanks for that.

Your idea of using a function I think may have solved a problem for me.

I have a financial application where records may be kept for a number
of Clients (/holders/entities - call them what you like). I have forms
such as Cheque (check), Direct deposit, Payments, Bank Statement etc
which all have a BankAccount combo box.
I only want it to show bank accounts for the holder who I am working on
in the drop down list, not bank accounts for every one, so the row
source of the combo box on each form is "Select
BankAccount.AccountNumber, ........from BankAccount where holderId =
forms!formname!holderId. As I can only work with 1 holder at a time
regardless of which form I use the HolderId will be the same. This
means instead of having a separate query on each combo box with only
the formname different in the WHERE clause I could have a common query
if I had a common HolderId reference point. I in fact do have that. I
have a class module calle gblHolder that looks up and holds a number of
attributes about a holder. Whenever I change holders this gets
populated so I dont have to have queries to read the database for the
same holder info all the time.

I tried using gblHolder.HolderId in the query - i.e. Select ..... from
BankAccount where holderId = gblHolder.HolderId but this didn't work -
seems queries wont look at class modules.

But using your method I can call a function that looks up gblHolder and
returns HolderId to the query. Now I can use a generalised query in
all forms that have a BankAccount combo box instead of a slightly
different one on each form.

Thanks

Feb 9 '06 #7
> Your idea of using a function I think may have solved a problem for me.

Glad to hear it. In once scenario, similar to what you described, I have a
form with a "Transaction Type" and a "Transaction Account" combo box.

The Transaction Type RowSource looks like this:

SELECT tblTxType.TxType_ID, tblTxType.TxTypeName
FROM tblTxType INNER JOIN tblTxJournal ON tblTxType.TxType_ID =
tblTxJournal.TxType_ID
WHERE tblTxJournal.Entity_ID = QryPrm("frm1", "Entity_ID")
UNION SELECT 0, "<All Transaction Types>" FROM tblTxType
ORDER BY TxTypeName;

And the Transaction Account RowSource looks like this:

SELECT tblTxAcct.TxAcct_ID, tblTxAcct.TxAcctName
FROM tblTxAcct INNER JOIN tblTxJournal ON tblTxAcct.TxAcct_ID =
tblTxJournal.TxAcct_ID
WHERE (tblTxJournal.Entity_ID = QryPrm("frm1", "Entity_ID")) And
(tblTxJournal.TxType_ID = QryPrm("frm1", "cbx1TxType") Or QryPrm("frm1",
"cbx1TxType") = 0)
UNION SELECT 0, "<All Accounts>" FROM tblTxAcct
ORDER BY TxAcctName;

So TxTypes are filtered to only those Types containing transactions for the
current Entity, and TxAccts are filtered to only those Accounts containing
transactions, belonging to the selected Type, belonging to the current
Entity.

Feb 10 '06 #8

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

Similar topics

1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
8
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf =...
11
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These...
9
by: zacks | last post by:
I have written a serialized class that has several properties that are typed as a list of type class. When I deserialize an XML file, the list is populated just fine. But I am having trouble...
9
by: Jakob Lithner | last post by:
1) I have a DataGridView with edit capability. But in some columns I want to limit the input with a DropDownList. There is no inbuilt column for DropDownLists so I intended to add one myself. I...
1
by: IMB | last post by:
I'm using VS2005 and am working with a ASP.NET 2.0 web site and I'm coding in VB.NET. I understand that I can add an item to a databound dropdownlist and make it the default selection using the...
0
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
3
by: ssims | last post by:
For some reason my databound DropDownList will not populate. I've tried a variety of things...here's the latest. - I pulled a DropDownList on to the page. - Then I pulled a SqlDataAdapter and...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.