How to issue a SQL query to Access table from Access form?

P

pahan

Hello all!
I have an Access form and I need to issue a query with two parameters based
on values entered in a form. I have found an example in Access's help which
looked like this (modified to suit my task):

Private Sub GenerateReport()

Dim db As Database
Dim q1 As QueryDef
Dim q1_res As Recordset
Dim q1_text As String

Set db = CurrentDb
q1_text = "PARAMETERS [Number] Integer, [Time] Datetime;SELECT
Count(*) FROM
WHERE [num_column]=[Number] AND [time_column]=[Time];"
Set q1 = db.CreateQueryDef("", q1_text)
For i = 0 To ListBox6.ItemsSelected.Count - 1
For j = 0 To ListBox8.ItemsSelected.Count - 1
q1.Parameters![Number] = ListBox6.ItemsSelected.Item(i)
q1.Parameters![Time] = ListBox8.ItemsSelected.Item(j)
Set q1_res = q1.OpenRecordSet(dbOpenSnapshot)
' Processing result here
q1_res.Close
Next j
Next i
End Sub

But when I run it, I got an error "User-defined type not defined" at the
first line "Dim db As Database". From (very bad) Access help I found that the
above code is applicable only to remote datasources connected to Access (am I
right?). What is the right way to issue a SQL query to Access table from
Access form?
Greatly thanks!
 
M

Marshall Barton

pahan said:
I got an error "User-defined type not defined" at the
first line "Dim db As Database".


That error almost always indicates that you do not have the
DAO library selected in References.

You probably have the ADO library selected (possibly by
default). If you do not intend to use ADO then you should
uncheck that reference.
 
P

pahan

Thanks, you were right. And can I programmatically enable this library while
starting my application?
 
D

Douglas J. Steele

It's not a case of enabling when you start your application. Simply ensure
that your application has the DAO library referenced (and that you don't
have the ADO library referenced if you're not using it). You'll need to do
this once per database. If you distribute the database to other users,
nothing more needs to be done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top