Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2007, 10:05 PM
Authorized User
 
Join Date: Sep 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select query execution

Hi All -

I want to execute a SQL (select statement) at the click of a command button and store the result in a variable. How can I do that ?

DoCmd.RunSql doesn't work because it is only for action queries and I am trying to execute a select query. I cannot find a way to do it.

Thanks for helping.
 
Old November 2nd, 2007, 06:35 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You want to store the result of the select query in a variable? How many columns and rows in the select query results?

Assuming it is one column, one row, and I will assume it is an integer and the select query looks like:

"SELECT Count(ID) as IDCount FROM tblMyTable"

Dim sSQL As String
Dim rs As ADODB.Recordset
Dim iValue As Integer

sSQL = "SELECT Count(ID) as IDCount FROM tblMyTable"

Set rs = New ADODB.Recordset
rs.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly

iValue = rs("IDCount")

rs.Close

etc...

Did that help?



mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old November 2nd, 2007, 06:40 AM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this - it uses an ADO connection and recordset object:

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

Dim CustId As Integer
Dim CustName As String
Dim SQL As String

' Open the ADO connection
If cnn.State = adStateOpen Then
   cnn.Close
End If
cnn.Open CurrentProject.Connection

CustID = 5

' Define the SQL using a variable
SQL = ""
SQL = SQL & "SELECT "
SQL = SQL & " CUST_NAME "
SQL = SQL & "FROM CUSTOMERS "
SQL = SQL & "WHERE CUST_ID = " & CustId

' Open the ADO recordset
If rst.State = adStateOpen Then
    rst.Close
End If
rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic

' If no rows returned, notify the user and exit
If rst.BOF And rst.EOF Then
   MsgBox "Cust Name not found for Cust ID " & CustId & "!"
   rst.Close
   cnn.Close
   Exit Sub
End If

' If cust name exists, load it into the cust name variable
If Not IsNull(rst("CUST_NAME")) Then
   CustName = rst("CUST_NAME")
Else
   CustName = ""
End If

' Close the recordset and connection objects
rst.Close
cnn.Close


I tried to be a little thorough - it includes checks for being sure to close the
connection and/or recordset objects first if either are already open for some
reason. Though it's not actually necessary in this example, it may be something
to think about if you insert a chunk of code like this elsewhere.

The other thing is that I checked to be sure that if there IS a null value in
the customer name in this example, is to not try to load a null into a string
variable.

Hope that helps.

Warren
 
Old November 2nd, 2007, 08:31 AM
Authorized User
 
Join Date: Sep 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That was very helpful and narrative.

Thank you wscheiman and mmcdonal.
 
Old November 2nd, 2007, 08:50 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I never understood why this:

' Define the SQL using a variable
SQL = ""
SQL = SQL & "SELECT "
SQL = SQL & " CUST_NAME "
SQL = SQL & "FROM CUSTOMERS "
SQL = SQL & "WHERE CUST_ID = " & CustId

Is not written as:

' Define the SQL using a variable
SQL = "SELECT CUST_NAME FROM CUSTOMERS WHERE CUST_ID = " & CustId

Is there a rationale? I have seen it a few times on this forum.


mmcdonal

Look it up at: http://wrox.books24x7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Execution Plan of a Query asuni SQL Server 2005 0 August 6th, 2008 03:42 AM
QUERY EXECUTION based on IF condition drani C# 0 November 9th, 2007 04:14 PM
Slow Query Execution prasanta2expert SQL Language 1 November 30th, 2006 07:35 AM
Slow Query Execution prasanta2expert SQL Server 2000 1 November 28th, 2006 09:58 AM
Query execution error chayanvinayak PHP Databases 0 May 3rd, 2006 05:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.