Hi, Sam.
I execute a SQL statement in code and would like to look at the recordset
returned in order to debug. How do I display the recordset?
One way to do this is to create a new query and name it qryTemp. Then paste
the following code in a standard module:
' * * * * * Start Code * * * * *
Public Function showRecSet(sqlStmt As String) As String
On Error GoTo ErrHandler
Dim qry As QueryDef
Set qry = CurrentDb().QueryDefs("qryTemp")
qry.SQL = sqlStmt
DoCmd.OpenQuery "qryTemp"
showRecSet = "Success"
CleanUp:
Set qry = Nothing
Exit Function
ErrHandler:
MsgBox "Error in showRecSet( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
showRecSet = "Fail"
GoTo CleanUp
End Function
' * * * * * End Code * * * * *
Save the module and compile the code. The next time you need to debug with
the Recordset, press <CTRL><G> to open the Immediate window. Copy the SQL
statement and paste it into the Immediate window with a call to the function.
The following is an example (watch for word wrap -- should be three lines):
? showRecSet ("SELECT F1.FeeID, F1.Fee, SUM(F2.Fee) AS RunningTotal " & _
"FROM TblFees AS F1 INNER JOIN TblFees AS F2 ON F2.FeeID <=
F1.FeeID " & _
"GROUP BY F1.FeeID, F1.Fee;")
Then press <ENTER> to execute the function. If it's successful, then the
word "Success" will appear below in the Immediate window. If it fails for
some reason, then the word "Fail" will appear below in the Immediate window.
Move to the Access window to view the Recordset in a datasheet. When
finished viewing, close the query. (The code won't do it for you). That
way, the next time the function is executed, the temp query's SQL Property
will be changed, and then the temp query will open again. If the temp query
is open from a previous run of the function, the SQL Property will _not_
change, and the temp query will still display the former Recordset.
HTH.
Gunny
See
http://www.QBuilt.com for all your database needs.
See
http://www.Access.QBuilt.com for Microsoft Access tips.
(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. (Only "Answers" have green
check-marks.) Remember that the best answers are often given to those who
have a history of rewarding the contributors who have taken the time to
answer questions correctly.