473,326 Members | 2,813 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,326 software developers and data experts.

Loop through the queries collection

I want to loop through the queries collection and then use these names
to define a value list for a combo box. What is the best way to do
this?

Thanks in advance.
Oct 16 '08 #1
3 17374
tr******@comcast.net wrote:
I want to loop through the queries collection and then use these names
to define a value list for a combo box. What is the best way to do
this?

Thanks in advance.
Here's a method to list the queries. As far as a value list...do that
only if you have a teeny tiny database with very few queries.

Sub QueryList()
Dim qdf As QueryDef

For Each qdf In CurrentDb.QueryDefs
If Left(qdf.Name, 1) <"~" Then
Debug.Print qdf.Name
End If
Next qdf
MsgBox "Done"
End Sub
Here's a method I might use instead for a list/combobox.
Private Type QueryList
QueryName As Variant
End Type
Private Function FillQueryList(fld As Control, ID As Variant, row As
Variant, col As Variant, Code As Variant) As Variant
On Error Resume Next

Static strRows() As QueryList
Static Entries As Integer
Dim qdf As QueryDef
Dim ReturnVal As Variant

ReturnVal = Null

Select Case Code
Case acLBInitialize ' Initialize.

Entries = 0
ReDim Preserve strRows(Entries)

'creates header/column row. Useful if colheads is True
strRows(Entries).QueryName = "Query Name"

For Each qdf In CurrentDb.QueryDefs
If Left(qdf.Name, 1) <"~" Then
Entries = Entries + 1
ReDim Preserve strRows(Entries)
strRows(Entries).QueryName = qdf.Name
End If
Next qdf

ReturnVal = True
Case acLBOpen ' Open.
ReturnVal = Timer ' Generate unique ID for control.
Case acLBGetRowCount ' Get number of rows.
ReturnVal = Entries + 1
Case acLBGetColumnCount ' Get number of columns.
ReturnVal = 1
Case acLBGetColumnWidth ' Column width.
ReturnVal = -1 ' -1 forces use of default width.
Case acLBGetValue ' Get data.
Select Case col
Case 0
'this example has a single column.
ReturnVal = strRows(row).QueryName
End Select
Case acLBEnd ' End.
Erase strRows
End Select
FillQueryList = ReturnVal
End Function

Now create a listbox, 1 column to display query name.

In the Listbox's RowSourceType row (under Data tab) enter
FillQueryList
and then blank out the RowSource row.

Drop this code into the Form's code module.

Save and run.

BTW, I would spend a few minutes and study this code. If you have a
combo/list box that is slow due to the number of records this method
will speed it up. You now have the template to do this for any other
times/types.

Moby
http://www.youtube.com/watch?v=7vxYaEAyTd4
Oct 16 '08 #2
Very nice Salad. BTW, the value list for this combo box is limited and
is relatively static. I could have hand-typed the value list, but I
wanted to know how to do this out of curiosity. With your code, you
actually solved another problem I have had, so I got a two-for-one
today.

Thanks as always.

Oct 16 '08 #3
tr******@comcast.net wrote:
Very nice Salad. BTW, the value list for this combo box is limited and
is relatively static. I could have hand-typed the value list, but I
wanted to know how to do this out of curiosity. With your code, you
actually solved another problem I have had, so I got a two-for-one
today.

Thanks as always.
Not a prob.

I had a complicated query in the past that I needed to filter and used
that in a combo box's rowsource. It started getting slow. I moved to
the method I supplied and it was lightening fast.

The problem with value lists is the number of rows it can display. It's
easy to exceed the length of the max value list.
Oct 16 '08 #4

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

Similar topics

20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
11
by: BillCo | last post by:
Does anyone know of a way to search through the SQL code of queries to find table name matches? Bear in mind there are literilly 1000's of queries in this database and doing it the old fashioned...
44
by: Greg Strong | last post by:
Hello All, Is it better to create a query in DAO where a report has 4 sub-reports each of whose record source is a query created at runtime and everything is in 1 MDB file? From what I've...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
1
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to...
3
by: Fredrik Melin | last post by:
The error I usally get is: Index out of range (e.g. I removed a item in the collection, therefor my For I = 0 to Col.count -1 gets invalid) What I want to do is loop through a collection as it...
4
by: outforblood74 | last post by:
Ok here's the deal, the for each statment using VB 6 doesn't loop all the objects. I would like it to close all the IE browsers that are open, but it doesn't work. And now I'm concerned that its...
5
by: B. Chernick | last post by:
Is this a bug or just bad programming? I've never encountered this problem before. (Bare minimum sample form to illustrate.) I've also tried this with a class. Same result. The error is: For...
10
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.