I am trying to rewrite something that originally worked in Access, then I rewrote to work in Access with ODBC connnection to SQL, and now need to work in Access Project. I have a form that allows users to build queries. It builds the SQL code and then lets them open the queries (detail query and a grouping version of the query) (the ODBC to SQL version builds pass through queries) where the results are displayed in a spreadsheet type of format.
I cannot write forms to display the results because I do not know what columns will be included, particularly from the grouping version of the query.
These are for read only displays. The users cannot change the data.
I have been able to run using a stored procedure that the SQL code is passed to and executed and also I have been able to create a recordset with the cmd function. But, neither method will display the results. I know the results are in the recordset because I display a record count as in the code below.
How can I get either the cmd function or the stored procedure to open as a spreadsheet within the VBA code? Note that if I click on the stored procedure in the database window and manually enter some SQL code, I do get the spreadsheet type of display of the data.
Here's the code I've experimented with:
Dim cmd As ADODB.Command
Dim rst As Recordset
Dim strSQL As String
Set cmd = New ADODB.Command
Set rst = New ADODB.Recordset
cmd.ActiveConnection = CurrentProject.Connection
' cmd.CommandText = "Matching Records"
' cmd.CommandType = adCmdTableDirect
strSQL = "SELECT * FROM " & strTableName & " WHERE " & strWhere _
& " ORDER BY " & strOrderBy & ";"
cmd.CommandType = adCmdStoredProc
Dim par As New ADODB.Parameter
Set par = cmd.CreateParameter("@strSQL", adChar, adParamInput, 1000, strSQL)
cmd.Parameters.Append par
cmd.CommandText = "RunQuery"
' cmd.Execute
' MsgBox "executed command"
' Set rst = cmd.Execute
rst.Open cmd.Execute
' rst.Open strSQL
rst.MoveLast
rst.MoveFirst
MsgBox rst.RecordCount
rst.Close
Set rst = Nothing
Set cmd = Nothing
I cannot write forms to display the results because I do not know what columns will be included, particularly from the grouping version of the query.
These are for read only displays. The users cannot change the data.
I have been able to run using a stored procedure that the SQL code is passed to and executed and also I have been able to create a recordset with the cmd function. But, neither method will display the results. I know the results are in the recordset because I display a record count as in the code below.
How can I get either the cmd function or the stored procedure to open as a spreadsheet within the VBA code? Note that if I click on the stored procedure in the database window and manually enter some SQL code, I do get the spreadsheet type of display of the data.
Here's the code I've experimented with:
Dim cmd As ADODB.Command
Dim rst As Recordset
Dim strSQL As String
Set cmd = New ADODB.Command
Set rst = New ADODB.Recordset
cmd.ActiveConnection = CurrentProject.Connection
' cmd.CommandText = "Matching Records"
' cmd.CommandType = adCmdTableDirect
strSQL = "SELECT * FROM " & strTableName & " WHERE " & strWhere _
& " ORDER BY " & strOrderBy & ";"
cmd.CommandType = adCmdStoredProc
Dim par As New ADODB.Parameter
Set par = cmd.CreateParameter("@strSQL", adChar, adParamInput, 1000, strSQL)
cmd.Parameters.Append par
cmd.CommandText = "RunQuery"
' cmd.Execute
' MsgBox "executed command"
' Set rst = cmd.Execute
rst.Open cmd.Execute
' rst.Open strSQL
rst.MoveLast
rst.MoveFirst
MsgBox rst.RecordCount
rst.Close
Set rst = Nothing
Set cmd = Nothing