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

June 12th, 2003, 03:59 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

June 13th, 2003, 07:05 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

June 16th, 2003, 03:49 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply Steven!
|

June 20th, 2003, 04:14 PM
|
Registered User
|
|
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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")
|

October 21st, 2003, 08:51 AM
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 29th, 2004, 03:14 PM
|
Registered User
|
|
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 29th, 2004, 05:13 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
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
|

April 11th, 2006, 01:58 PM
|
Registered User
|
|
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

October 11th, 2007, 10:36 AM
|
Registered User
|
|
Join Date: Oct 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

May 25th, 2010, 10:42 PM
|
Registered User
|
|
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |