I execute the following sub procedure to generate a report in Excel based on a query I created via the Objects list Query using QBE.
Is there a way to run a similar function to take a string I created which I will call strDynamicSQL. This string contains a SQL statement based on listbox values that the user selected via a form. Now I want to run this dynamic SQL statement which I created in a string format and export the results of this dynamic query to an Excel spreadsheet.
Private Sub FallQueryLNToExcel()
Dim rstQueryFS As ADODB.Recordset
Dim objXL As Excel.Application
Dim objWS As Excel.Worksheet
Dim fld As ADODB.Field
Dim intCol As Integer
Dim intRow As Integer
Set rstQueryFS = New ADODB.Recordset
Set rstQueryFS = CurrentProject.Connection.Execute("qryLNFall", , adCmdStoredProc)
Set objXL = New Excel.Application
objXL.Workbooks.Add
strNextFile = GetNextFileName("C:\LEXFALL1.XLS")
objXL.ActiveWorkbook.SaveAs strNextFile
Set objWS = objXL.ActiveSheet
For intCol = 0 To rstQueryFS.Fields.Count - 1
Set fld = rstQueryFS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
intRow = 2
Do Until rstQueryFS.EOF
For intCol = 0 To rstQueryFS.Fields.Count - 1
objWS.Cells(intRow, intCol + 1) = _
rstQueryFS.Fields(intCol).Value
objWS.Cells.EntireColumn.AutoFit
Next intCol
rstQueryFS.MoveNext
intRow = intRow + 1
Loop
objXL.Visible = True
End Sub
BTW: My congrats to PHV for being acknowledged this week for all the great work in assisting others. I know PHV has helped me immensely.
Is there a way to run a similar function to take a string I created which I will call strDynamicSQL. This string contains a SQL statement based on listbox values that the user selected via a form. Now I want to run this dynamic SQL statement which I created in a string format and export the results of this dynamic query to an Excel spreadsheet.
Private Sub FallQueryLNToExcel()
Dim rstQueryFS As ADODB.Recordset
Dim objXL As Excel.Application
Dim objWS As Excel.Worksheet
Dim fld As ADODB.Field
Dim intCol As Integer
Dim intRow As Integer
Set rstQueryFS = New ADODB.Recordset
Set rstQueryFS = CurrentProject.Connection.Execute("qryLNFall", , adCmdStoredProc)
Set objXL = New Excel.Application
objXL.Workbooks.Add
strNextFile = GetNextFileName("C:\LEXFALL1.XLS")
objXL.ActiveWorkbook.SaveAs strNextFile
Set objWS = objXL.ActiveSheet
For intCol = 0 To rstQueryFS.Fields.Count - 1
Set fld = rstQueryFS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
intRow = 2
Do Until rstQueryFS.EOF
For intCol = 0 To rstQueryFS.Fields.Count - 1
objWS.Cells(intRow, intCol + 1) = _
rstQueryFS.Fields(intCol).Value
objWS.Cells.EntireColumn.AutoFit
Next intCol
rstQueryFS.MoveNext
intRow = intRow + 1
Loop
objXL.Visible = True
End Sub
BTW: My congrats to PHV for being acknowledged this week for all the great work in assisting others. I know PHV has helped me immensely.