473,468 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to display a SELECT query using VBA?

27 New Member
Hi,

I'm trying to display data from a table using a query.
My code:
Expand|Select|Wrap|Line Numbers
  1.     Dim db As Database
  2.     Dim rs As Recordset
  3.     Dim qdf As QueryDef
  4.     Dim strSql As String
  5.     Set db = CurrentDb
  6.  
  7.     strSql = "SELECT T_MAIN.Name FROM T_MAIN;"
  8.     Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
  9.  
  10.     With db
  11.     Set qdf = .CreateQueryDef("tmpQry", strSql)
  12.     DoCmd.OpenQuery "tmpQry", , acReadOnly
  13.     .QueryDefs.Delete "tmpQry"
  14.     End With
  15.     db.Close
  16.     qdf.Close
  17.  
The code gets to "set qdf = ..." working just fine
but in the following row I get a message:
"Run-Time Error '7874':
Microsoft Office can't find the object 'tmpQry'."

Does anybody know what I'm doing wrong?

Thanks in advance
Izhar
Jun 3 '10 #1
8 11880
patjones
931 Recognized Expert Contributor
Hi Izhar,

Everything that you're doing in the With...End With part of your code is not necessary. When line 8 is executed, the results of the SQL query get dumped into your recordset rs. From that point forward you can use the recordset to access the results of the SQL query.

Pat
Jun 3 '10 #2
izharmel
27 New Member
Hi Pat,
What I'm trying to do is display the query, the same way you would get it if you had done it manually (using a SELECT query with Access).

Thanks,
Izhar
Jun 3 '10 #3
patjones
931 Recognized Expert Contributor
I'm not really sure I know what you mean by that. Are you saying you want to display the results in a report? Or do you want the results to open up in table view?

Also, how are you calling this code...for instance is it being run after clicking a button in a form?

Pat
Jun 3 '10 #4
izharmel
27 New Member
yes, there's a button in a form. When the user presses the button, I want a table to be displayed (the result of the SELECT query).
Thanks,
Izhar
Jun 6 '10 #5
izharmel
27 New Member
Hi,
In the mean time I copied the data to a new table (using "SELECT ... INTO") and then
DoCmd.OpenTable "TableName", acViewNormal
is this the best way to do it?

Thanks in advance,
Izhar
Jun 6 '10 #6
ahmedtharwat19
55 New Member
@izharmel
Expand|Select|Wrap|Line Numbers
  1.     DoCmd.OpenQuery "tmpQry", , acReadOnly 
  2.     .QueryDefs.Delete "tmpQry" 
You are using one of two things against one another for quite some.

how can use one command to open and second command to delete in same vba command

you should do the first and when done from it use the second in another vba command.

If I mistake any one correct me...

with my regards,
ahmed tharwat
Jun 6 '10 #7
patjones
931 Recognized Expert Contributor
@izharmel
If this is what you want and that code accomplishes it, then I don't see a problem with it.

Pat
Jun 6 '10 #8
ahmedtharwat19
55 New Member
zepphead80 @izharmel If this is what you want and that code accomplishes it, then I don't see a problem with it.

Pat
what i mean , we can open it and when close the query we cannot use the query again in another vba code.

i`m sorry for Lack of understanding of the last post of mine.

Best Regards ,

Ahmed Tharwat
Jun 7 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Razia | last post by:
Hello everyone I want to create a select query according to the values in a single dimension array Select * from <table Name> where <Array items> Can anybody give me some suggestions
13
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything...
1
by: flit | last post by:
Hi all, How Can I do in the simplest way, display in html one table with the return of 3 collums? Like 1- consult mysql base 2- make the select query 3- show the values. I am a kind of...
0
by: trishbarker | last post by:
I've created a simple select query in Microsoft Access wherein I'm searching all records based on the "Final Payment Due Date". The Final Payment Due Date is a calculated expression based on two...
1
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day and display it in a query with each day's date...
1
by: 123123 | last post by:
I having one table its contain N columns But Select Query Using only for N-1 Columns , so, Select Query Dont Enter The N-1 Columns . How To Solve It
6
by: bill.mckinstry | last post by:
I have simple Select Query using two small tables. Table A includes Statement ID & Date. Table B includes Statement ID, Loan ID & Payment Amount. The Query selects payments and amounts from Table...
2
by: ravenfrost | last post by:
Hello, I'm trying to query a sql table to see if a value already exists. strMAC = "11:22:33:44:55:66" Call MsgBox(QueryDB("SELECT * FROM main WHERE MAC = '" & strMAC & "'", conn)) Function...
2
by: sheenaa | last post by:
Hi, I want to display the last recorded record in the database of SQL SERVER 2005. How can i display it with the select query in the grid view... The database contains the...
1
by: atishrg | last post by:
Hello All, I am facing one strange problem.. I am using a select query in my function which will return specified product details according to product code selected by user.. here is the...
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
marktang
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,...
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
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...
0
agi2029
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.