Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 12th, 2003, 03:59 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using DoCmd.RunSQL to return values

Greetings

Hopefully this is the right forum for this question.
I am developing an application with an Access frontend and MSSQL backend. The problem is that in my VBA code, I need to return values from tables such as the count of some records, or sum of some records etc.

To return values quickly, I know I can use stored procedures. I also know that I can run SQL on the backend using "DoCmd.RunSQL(sql_goes_here)". Finally, I know that I can use recordsets for extracting data. My question is:
Is it possible to use the "DoCmd.RunSQL(sql_goes_here)" command, but return the result to a variable? This would save lots of time.
e.g.
Dim intResult as integer
intResult = DoCmd.RunSQL("SELECT COUNT(*) FROM tblProducts")

Cheers

Joe
Reply With Quote
  #2 (permalink)  
Old June 13th, 2003, 07:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Short answer: NO

DoCmd.RunSQL is for action SQL only - eg: inserting, deleting and updating - it doesn't actually return anything

Steven

There are 10 kinds of people in the world - those who understand binary - and those with friends
Reply With Quote
  #3 (permalink)  
Old June 16th, 2003, 03:49 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply Steven!
Reply With Quote
  #4 (permalink)  
Old June 20th, 2003, 04:14 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This works

Dim dan As Variant
dan = CurrentDb.QueryDefs("Query1").OpenRecordset.Fields ("Sum_of_What_ever")

Test is an Access query

OR

dan = CurrentDb.openrecordset("SQL").OpenRecordset.Field s("Sum_of_What_ever")
Reply With Quote
  #5 (permalink)  
Old October 21st, 2003, 08:51 AM
Authorized User
 
Join Date: Oct 2003
Location: Cleveland, OH, USA.
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's another option that I use quite often using a field alias:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim intResult As Integer
Dim strSQL As String

Set db = CurrentDb

strSQL = "SELECT COUNT(*) As RecordCount FROM tblProducts"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapShot)

intResult = rs("RecordCount")

rs.Close
db.Close


Utilizing an alias can be beneficial when running these types
of queries. You can use any value you'd like for the alias
name as long as it follows the rules for column names in a
table.

If you are using VBA within Access you can also use a domain
function. Three of the most used as called DLookup, DCount,
and DSum. In this case, DCount() will give you the record
count like this:

intResult = DCount("ProdID", "tblProducts", "")

Here, you can use any valid existing column name from the table
in the first argument.

Both options should do exactly what you need. If you haven't
already, you may want to look in MS-Access online help for more
info on these 3 domain functions listed above (there are others
as well starting with D*) - I still use them quite extensively
myself in end-user delivered applications.

Best Wishes and Good Luck.
:D
Reply With Quote
  #6 (permalink)  
Old July 29th, 2004, 03:14 PM
Registered User
 
Join Date: Jul 2004
Location: Boise, ID, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, now what if what we need to do is to get the result of a SQL query into a string array? Is there a way to do that?

- one_eye
Reply With Quote
  #7 (permalink)  
Old July 29th, 2004, 05:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Probably want to use a variant array to accomodate non-string data type fields:

Public Sub FillArrayADO()
    Dim rst As ADODB.Recordset
    Dim cnn As ADODB.Connection
    Dim strConnection As String
    Dim strSQL As String
    Dim arrRows As Variant
    Dim intRow As Integer

    strConnection = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=C:\Test.mdb;"

    Set cnn = New ADODB.Connection
    cnn.Open (strConnection)

    Set rst = New ADODB.Recordset
    strSQL = "SELECT * FROM tblTest"
    rst.Open strSQL, cnn, adOpenStatic, adLockReadOnly, adCmdText

    ' Load array
    arrRows = rst.GetRows()

    ' Print array elements
    For intRow = 0 To UBound(arrRows, 2)
        Debug.Print " " & _
            arrRows(0, intRow) & " " & _
            arrRows(1, intRow)
    Next intRow

    rst.Close
    cnn.Close

End Sub

HTH,

Bob




Reply With Quote
  #8 (permalink)  
Old April 11th, 2006, 01:58 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When I try to do the ADO example provided by wschieman, I get a message that says the Database has been placed in a state by user Admin on machine 'MYMACHINE' that prevents it from opened or locked. What does this mean? I am the Admin and Ithink I have all of the permission enabled. What am I missing?

Computers have lots of memory, but they lack imagination.
Reply With Quote
  #9 (permalink)  
Old October 11th, 2007, 10:36 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get the same error. I'm actually getting some of the data I use from a second database, so technically I could just reference the other database, but I want to have the entire table in my database, so it's really not an option, any assistance would be great, even though this topic is kinda old.

The issue for me is that I'm doing this solely on an access database, with no MySQL backend.
Reply With Quote
  #10 (permalink)  
Old May 25th, 2010, 10:42 PM
Registered User
 
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need Help exporting to EXCEL

Hey guys,
I am new to this. The database which i have exports reports to PDF and am trying to code it to export the reports to excel via transferspreadsheet method but its not working. In what way can i make it to export the reports to excel.
Any help is very appreciated

Shah
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Docmd.runsql anne.burrows VB How-To 2 October 25th, 2006 08:21 AM
DoCmd.RunSQL dates and time TarkaDahl Access VBA 3 May 11th, 2006 11:19 AM
Removing the MsgBox in DoCmd.RunSQL Update arfa Access 2 March 24th, 2006 08:05 PM
docmd.runsql "select RodMead Access VBA 2 July 31st, 2004 02:55 PM
Help with DoCmd.RunSQL command ricmar Access VBA 3 July 21st, 2004 03:32 PM



All times are GMT -4. The time now is 08:51 PM.


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