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

How to run a query that exists in Access from VB

I tried Googling this but I get a whole lot of replies about running
the SQL statement in VB via ADO.

All I want to do is run an existing ACCESS 2000 query from VB with no
information returned to VB. I am presuming that I would use
Automation somehow.

Thanks in advance.
Nov 13 '05 #1
12 34774
Hmm... you read the book... how about the manual this time...

Straight outta Compton... no, wait, the help file:

Execute Method (ADO Connection)
Executes the specified query, SQL statement, stored procedure, or
provider-specific text.

Syntax

For a non-row-returning command string:

connection.Execute CommandText, RecordsAffected, Options

For a row-returning command string:

Set recordset = connection.Execute (CommandText, RecordsAffected,
Options)

Return Value

Returns a Recordset object reference.

Parameters

CommandText A String value that contains the SQL statement, table
name, stored procedure, a URL, or provider-specific text to execute.

RecordsAffected Optional. A Long variable to which the provider
returns the number of records that the operation affected.

Options Optional. A Long value that indicates how the provider should
evaluate the CommandText argument. Can be one or more CommandTypeEnum
or ExecuteOptionEnum values.

Remarks

Using the Execute method on a Connection object executes whatever query
you pass to the method in the CommandText argument on the specified
connection. If the CommandText argument specifies a row-returning
query, any results that the execution generates are stored in a new
Recordset object. If the command is not a row-returning query, the
provider returns a closed Recordset object.

Nov 13 '05 #2
On 9 Jul 2005 19:08:44 -0700, pi********@hotmail.com wrote:
Hmm... you read the book... how about the manual this time...

Straight outta Compton... no, wait, the help file:

Execute Method (ADO Connection)


Nothing like an answer from a wiseguy that's wrong. Maybe you should
try reading the question.

How do I run a query that EXISTS IN ACCESS 2000 FROM VB?

As I suspected in the original post, it can be done via Automation
using the Access Application object.

Thanks.
Nov 13 '05 #3
On Sun, 10 Jul 2005 18:33:50 GMT, Bookreader <bo***********@yahoo.com>
wrote:
On 9 Jul 2005 19:08:44 -0700, pi********@hotmail.com wrote:
Hmm... you read the book... how about the manual this time...

Straight outta Compton... no, wait, the help file:

Execute Method (ADO Connection)


Nothing like an answer from a wiseguy that's wrong. Maybe you should
try reading the question.

How do I run a query that EXISTS IN ACCESS 2000 FROM VB?

As I suspected in the original post, it can be done via Automation
using the Access Application object.

Thanks.


Good, now both of you can feel superior to the other and everyone is
happy!
David

Nov 13 '05 #4
On 10 Jul 2005 15:23:02 -0500, not@here (David Schofield) wrote:

Good, now both of you can feel superior to the other and everyone is
happy!
David


Actually, I knew I shouldn't have even responded to that guy. But, I
did allow the third poster an opportunity to feel superior to both of
us! (hardee har)

I'd still like an exact answer to my original question, if you would
please.

Here's the code I have so far:

Dim objAccess As Access.Application
Set objAccess = New Access.Application
'MsgBox objAccess.Reports.Count

objAccess.OpenCurrentDatabase "C:\Current Database\new.mdb"
objAccess.Visible = True
objAccess.DoCmd.OpenQuery ("Create_Invoice_Footer")
objAccess.Run ("Create_Invoice_Footer")

Are both the OpenQuery and Run lines needed?

Is it possible to get any resulting error message back from Access or
know that the query was successful?

Thanks again.
Nov 13 '05 #5
On Sun, 10 Jul 2005 20:31:32 GMT, New Guy <Ne****@yahoo.com> wrote:
I'd still like an exact answer to my original question, if you would
please.

Here's the code I have so far:

Dim objAccess As Access.Application
Set objAccess = New Access.Application
'MsgBox objAccess.Reports.Count

objAccess.OpenCurrentDatabase "C:\Current Database\new.mdb"
objAccess.Visible = True
objAccess.DoCmd.OpenQuery ("Create_Invoice_Footer")
objAccess.Run ("Create_Invoice_Footer")

Are both the OpenQuery and Run lines needed?

Thanks again.


I did find out that I don't need the "objAccess.Run
("Create_Invoice_Footer")" line.
Nov 13 '05 #6
On Sun, 10 Jul 2005 20:43:51 GMT, New Guy <Ne****@yahoo.com> wrote:
On Sun, 10 Jul 2005 20:31:32 GMT, New Guy <Ne****@yahoo.com> wrote:
I'd still like an exact answer to my original question, if you would
please.

Here's the code I have so far:

Dim objAccess As Access.Application
Set objAccess = New Access.Application
'MsgBox objAccess.Reports.Count

objAccess.OpenCurrentDatabase "C:\Current Database\new.mdb"
objAccess.Visible = True
objAccess.DoCmd.OpenQuery ("Create_Invoice_Footer")
objAccess.Run ("Create_Invoice_Footer")

Are both the OpenQuery and Run lines needed?

Thanks again.


I did find out that I don't need the "objAccess.Run
("Create_Invoice_Footer")" line.

Hi
You put me in an embarrassing position.

Your original post said
"All I want to do is run an existing ACCESS 2000 query from VB with no
information returned to VB"

That implied that you are programming a VB program and want to run an
stored action query (ie insert (append), update or delete) in a JET
(ie Access) database. The way to do this is as PietLinden suggests,
though you could use DAO instead of ADO.

But the example code you since posted suggests you are going to follow
this up with other things in a full Access environment, eg create
reports.In this case automation is certainly indicated.

I suggest you go to
http://support.microsoft.com/default...NoWebContent=1
"Microsoft Office 2000 automation Help file is available in the
Download Center."
the download gives many examples of automating Access 2000.

Can't you do it all in one instance of Access and avoid automation? My
own experiences are it is nothing but trouble, eg it leaves dangling
references and the called aplication doesn't close but sits there
invisibly; any failures are hard to recover from; etc etc. Access 2000
also has many bugs in this area and maybe not all were cured by
service packs. I would prefer to use the shell method if I had to.
David

Nov 13 '05 #7
On 10 Jul 2005 18:04:02 -0500, not@here (David Schofield) wrote:

I suggest you go to
http://support.microsoft.com/default...NoWebContent=1
"Microsoft Office 2000 automation Help file is available in the
Download Center."
the download gives many examples of automating Access 2000.
Thanks for this link. That's what I really need.
Can't you do it all in one instance of Access and avoid automation? My
own experiences are it is nothing but trouble, eg it leaves dangling
references and the called aplication doesn't close but sits there
invisibly; any failures are hard to recover from; etc etc. Access 2000
also has many bugs in this area and maybe not all were cured by
service packs. I would prefer to use the shell method if I had to.
David


I'm not sure I get your question about one instance of Access.

I just want to have the user click a button in a VB program to run a
query that resides in Access. Where does more than one instance of
Access fit into this?

All I want the user to know is that he has to push the button.

Thanks.
Nov 13 '05 #8
On Sat, 09 Jul 2005 22:52:53 GMT, Bookreader <bo***********@yahoo.com> wrote:

¤ I tried Googling this but I get a whole lot of replies about running
¤ the SQL statement in VB via ADO.
¤
¤ All I want to do is run an existing ACCESS 2000 query from VB with no
¤ information returned to VB. I am presuming that I would use
¤ Automation somehow.
¤

You can use ADO (Microsoft ActiveX Data Objects 2.x Library) to do this:

Dim strConnectionString As String
Dim objConn As ADODB.Connection
Dim objCommand As ADODB.Command

strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=e:\My
Documents\db1.mdb;" & _
"Jet OLEDB:Engine Type=4"

Set objConn = New ADODB.Connection
objConn.Open strConnectionString

Set objCommand = New ADODB.Command

objCommand.ActiveConnection = objConn

' Assign to ADO Command object
objCommand.CommandText = "qdelTable10"
objCommand.CommandType = adCmdStoredProc

objCommand.Execute

objConn.Close

Set objCommand = Nothing
Set objConn = Nothing
Paul
~~~~
Microsoft MVP (Visual Basic)
Nov 13 '05 #9
On Mon, 11 Jul 2005 08:29:57 -0500, Paul Clement
<Us***********************@swspectrum.com> wrote:
On Sat, 09 Jul 2005 22:52:53 GMT, Bookreader <bo***********@yahoo.com> wrote:

¤ I tried Googling this but I get a whole lot of replies about running
¤ the SQL statement in VB via ADO.
¤
¤ All I want to do is run an existing ACCESS 2000 query from VB with no
¤ information returned to VB. I am presuming that I would use
¤ Automation somehow.
¤

You can use ADO (Microsoft ActiveX Data Objects 2.x Library) to do this:

Dim strConnectionString As String
Dim objConn As ADODB.Connection
Dim objCommand As ADODB.Command

strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=e:\My
Documents\db1.mdb;" & _
"Jet OLEDB:Engine Type=4"

Set objConn = New ADODB.Connection
objConn.Open strConnectionString

Set objCommand = New ADODB.Command

objCommand.ActiveConnection = objConn

' Assign to ADO Command object
objCommand.CommandText = "qdelTable10"
objCommand.CommandType = adCmdStoredProc

objCommand.Execute

objConn.Close

Set objCommand = Nothing
Set objConn = Nothing
Paul
~~~~
Microsoft MVP (Visual Basic)


I see. A query existing in ACCESS is a stored procedure.?

Does this return an error message if the statement fails?

Is ADO preferable to the Acess.Application method below. Just for my
education, can you tell me why ADO is better?
Dim objAccess As Access.Application
Set objAccess = New Access.Application
'MsgBox objAccess.Reports.Count

objAccess.OpenCurrentDatabase "C:\Current Database\new.mdb"
objAccess.Visible = True
objAccess.DoCmd.OpenQuery ("Create_Invoice_Footer")
objAccess.Run ("Create_Invoice_Footer")
Thanks.
Nov 13 '05 #10
On Mon, 11 Jul 2005 22:59:59 GMT, New Guy <Ne****@yahoo.com> wrote:

Is ADO preferable to the Acess.Application method below. Just for my
education, can you tell me why ADO is better?


Hi
I suggested several reasons in my last post. ADO will also be a lot
quicker. You would only use the automation method if you wanted to do
access-specific things like run a report as opposed to just using the
data.
If you hadn't been so rude to pietlinden you might have found this out
earlier.

David

Nov 13 '05 #11
On Mon, 11 Jul 2005 22:59:59 GMT, New Guy <Ne****@yahoo.com> wrote:

¤ On Mon, 11 Jul 2005 08:29:57 -0500, Paul Clement
¤ <Us***********************@swspectrum.com> wrote:
¤

¤ I see. A query existing in ACCESS is a stored procedure.?
¤

It's about as close as you can get in Access.

¤ Does this return an error message if the statement fails?
¤

Yes, a run time error is generated if the statement fails to execute so you should have error
handling code to take care of it.

¤ Is ADO preferable to the Acess.Application method below. Just for my
¤ education, can you tell me why ADO is better?
¤ Dim objAccess As Access.Application
¤ Set objAccess = New Access.Application
¤ 'MsgBox objAccess.Reports.Count
¤
¤ objAccess.OpenCurrentDatabase "C:\Current Database\new.mdb"
¤ objAccess.Visible = True
¤ objAccess.DoCmd.OpenQuery ("Create_Invoice_Footer")
¤ objAccess.Run ("Create_Invoice_Footer")

ADO is preferable to Access automation. First, ADO is more efficient and second if you use Access
automation then the Access application must be installed to run the code.
Paul
~~~~
Microsoft MVP (Visual Basic)
Nov 13 '05 #12
am i missing something or could you just use
DoCmd.OpenQuery "qryYourQueryName"

Nov 13 '05 #13

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

Similar topics

5
by: Jahmil Richardson | last post by:
Attempting to query MS Access database with the ODBC drivers for PHP. When using the LIKE statement no records are returned. Example: $query = " Select * From Dispatch Where Customer Like...
5
by: meyvn77 | last post by:
I wrote a bunch of SQL statement in SQL SERVER and now im trying to do the same thing in access. OMG what a pain that Access can't handle updates or CAST() the way SQL server can. OK I thought I...
0
by: abhishekjethwani | last post by:
How to write a query to access tree structure form multi table. I m having five tables from them first table give me a data which act as the parameter for query for the second table and the two...
2
candicemc
by: candicemc | last post by:
Can anyone help me please?! I am a beginner and i am in desperate need of help. I have to create an insert query in access using a table i've already created called "madlibs" Then I have to create...
5
by: Jim | last post by:
I'm a vb.net newbie. I have an query that is entirely within an Access database. It's a simple update query - "usp_Append_tbl_RefNos". It works in Access, How can I run that query from a vb...
4
by: ashutoshvyas | last post by:
I am designing a data report in VB6. In data environment it is possible to selet tables and 'select quries'. But I want to generate a report based on parameters passed by user, so i had created a...
1
by: rune | last post by:
Hi I'm trying to convert an SQL Query from Access to SQL Server. The Access Query goes like this: SELECT Format(EntryDate, 'ddd mm dd') AS FROM JournalEntries This query returns the name...
9
by: anthony | last post by:
In Access 2007, why does a query field designed as Term: ! ! return 00:00:00 when cboTerm actually contains 08SP. This works as expected in Access 2003?
0
by: csin | last post by:
I have an Access DB backend for the application I am running, I want to use the built in ability in Access to remove duplicate entries... Say I have table1 with fields field1 field2 and field3,...
12
by: jenniferhelen | last post by:
I am working with a query that has 6 columns and 101 rows; I would like to transpose the rows and columns. I have tried using a crosstab query but Access limits the row "fields" to 3 and this was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
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...

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.