p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access VBA (http://p2p.wrox.com/forumdisplay.php?f=80)
-   -   Using DoCmd.RunSQL to return values (http://p2p.wrox.com/showthread.php?t=472)

jscully June 12th, 2003 03:59 AM

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

Steven June 13th, 2003 07:05 AM

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

jscully June 16th, 2003 03:49 AM

Thanks for the reply Steven!

cluderay June 20th, 2003 04:14 PM

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")

wscheiman October 21st, 2003 08:51 AM

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

cyclops_one_eye July 29th, 2004 03:14 PM

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

Bob Bedell July 29th, 2004 05:13 PM

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





rapconsulting April 11th, 2006 01:58 PM

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.

onikage October 11th, 2007 10:36 AM

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.

Mshah May 25th, 2010 10:42 PM

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


All times are GMT -4. The time now is 01:44 AM.

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