I have a form which allows the user to select various options, which
options I then use to dynamically generate a query into the database.
I then want to display this query to the user. However, no matter
which way I go about it, I seem to run into a snag. Currently, I'm
doing this:
Dim ReportQuery As String
'Code to generate dynamic query
Dim db As Database
Dim rs As Recordset
Dim qdf As QueryDef
Set db = CurrentDb
Set qdf = db.CreateQueryDef "CreateReportQry", ReportQuery
qdf.OpenRecordset
DoCmd.OpenQuery "CreateReportQry", , acReadOnly
This works once but fails if run a second time because a query named
"CreateReportQry" already exists. I can modify the CreateQueryDef
function call to use "" as the name and create a temp query. However,
if I do that I can't find a way to use the OpenQuery function because
the query doesn't have a name to pass to the function. I've tried
deleting the record set at the end of the function but that gives me an
error "Object variable or with block variable not set" I'm not
positive but I suspect it may be because I'm trying to delete the
record set while the query is still being displayed. I'd try deleting
the record set when the query form is closed, but it's a dynamic query
and I can't find a way to trap the form close event. There's probably
a very simple way to do this but I can't seem to track it down. Any
assistance would be greatly appreciated. 2 4521
Dan <da*********@gmail.com> wrote: I have a form which allows the user to select various options, which options I then use to dynamically generate a query into the database.
I then want to display this query to the user. However, no matter which way I go about it, I seem to run into a snag. Currently, I'm doing this:
Dim ReportQuery As String 'Code to generate dynamic query
Dim db As Database Dim rs As Recordset Dim qdf As QueryDef
Set db = CurrentDb Set qdf = db.CreateQueryDef "CreateReportQry", ReportQuery qdf.OpenRecordset
DoCmd.OpenQuery "CreateReportQry", , acReadOnly
This works once but fails if run a second time because a query named "CreateReportQry" already exists.
You should be testing that it exists and delete it if it does...
I can modify the CreateQueryDef function call to use "" as the name and create a temp query. However, if I do that I can't find a way to use the OpenQuery function because the query doesn't have a name to pass to the function.
Depends how you want to use the recordset.....
I've tried deleting the record set
You should be deleting the query, not the recordset.....
at the end of the function but that gives me an error "Object variable or with block variable not set" I'm not positive but I suspect it may be because I'm trying to delete the record set while the query is still being displayed.
As above....
I'd try deleting the record set when the query form is closed, but it's a dynamic query and I can't find a way to trap the form close event. There's probably a very simple way to do this but I can't seem to track it down. Any assistance would be greatly appreciated.
CurrentDB.QueryDefs.Delete "CreateReportQry"
--
regards,
Bradley
A Christian Response http://www.pastornet.net.au/response
Dan wrote: I have a form which allows the user to select various options, which options I then use to dynamically generate a query into the database.
I then want to display this query to the user. However, no matter which way I go about it, I seem to run into a snag. Currently, I'm doing this:
Dim ReportQuery As String 'Code to generate dynamic query
Dim db As Database Dim rs As Recordset Dim qdf As QueryDef
Set db = CurrentDb Set qdf = db.CreateQueryDef "CreateReportQry", ReportQuery qdf.OpenRecordset
DoCmd.OpenQuery "CreateReportQry", , acReadOnly
This works once but fails if run a second time because a query named "CreateReportQry" already exists. I can modify the CreateQueryDef function call to use "" as the name and create a temp query. However, if I do that I can't find a way to use the OpenQuery function because the query doesn't have a name to pass to the function. I've tried deleting the record set at the end of the function but that gives me an error "Object variable or with block variable not set" I'm not positive but I suspect it may be because I'm trying to delete the record set while the query is still being displayed. I'd try deleting the record set when the query form is closed, but it's a dynamic query and I can't find a way to trap the form close event. There's probably a very simple way to do this but I can't seem to track it down. Any assistance would be greatly appreciated.
This may or may not be what you need
Dim rstFilter As Recordset
Dim rst As Recordset
Set rst = qdf.OpenRecordset
rst.Filter = "ID > 1 and ID < 1000"
rst.Sort = "ID Desc"
Set rstFilter = rst.OpenRecordset This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rahul Chatterjee |
last post by:
Hello All
I have an asp page in which I am performing the following
1. Querying a database view
2. Returning rows in to a recordset.
3. Looping thru the recordset and printing the data
4....
|
by: Bryan |
last post by:
I have a results table that is 5 columns wide. the recordset is
returned with 48 items. I have no problem displaying 5 per row until I
hit the last row where i get a ADODB.Field error '80020009'
...
|
by: Jennifer Smith |
last post by:
I want to be able to display my recordset as follows:
a e
b f
c g
d h
Instead of :
a b
c d
|
by: gonzal |
last post by:
Hi, just a quick question regarding performance and speed.
Q. Run a complicated query three times or create a TEMPORARY table and
access it as needed?
|
by: Lyn |
last post by:
I need some guidance for a technique that will allow me to accomplish the
following...
I have a table in which each record contains a photograph. I would like to
display in a form a thumbnail...
|
by: Tomaz Koritnik |
last post by:
Hi
I have many short HTML files stored in a binary stream storage to display
descriptions for various items in application. HTML would be display inside
application using some .NET control or...
|
by: Sonia Scott |
last post by:
I have done the following code, which seem to partially work. I got my HTML ALT (text) from the database shown, but for some reason cannot display the image.
Below is my connection code:
<%...
|
by: Drew |
last post by:
I posted this to the asp.db group, but it doesn't look like there is much
activity on there, also I noticed that there are a bunch of posts on here
pertaining to database and asp. Sorry for...
|
by: rghollenbeck |
last post by:
Here's my code so far:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim db As DAO.Database
Dim rs As DAO.RecordSet
Dim qdf As QueryDef
Set db =...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |