I have this VBA macro in Excel. When a user clicks on a button on the sheet the macro returns the results to the sheet. What i would like to ask,is how can i run more than one query (that returns different results) in the same sheet,using the below code?
Sub Stats2()
Workbooks("macro.xls").Sheets("Sheet3").Select
ActiveSheet.Range("A1").Select
Dim objConn As ADODB.Connection
Dim rsData As ADODB.Recordset
Dim strSQL As String
szconnect = "Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=*******88;Data Source=****"
''#Create the Connection and Recordset objects.
Set objConn = New ADODB.Connection
Set rsData = New ADODB.Recordset
On Error GoTo errHandler
''#Open the Connection and execute the stored procedure
objConn.Open szconnect
strSQL = "select * from CATEGORY_TYPE "
objConn.CommandTimeout = 0
Set rsData = objConn.Execute(strSQL)
For iCols = 0 To rsData.Fields.Count - 1
ActiveSheet.Range("A3").Select
ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column + iCols).Value = rsData.Fields(iCols).Name
ActiveSheet.Cells.Font.Name = "Arial"
ActiveSheet.Cells.Font.Size = 8
ActiveSheet.Cells.EntireColumn.AutoFit
Next
ActiveSheet.Range(ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column), ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column + rsData.Fields.Count)).Font.Bold = True
j = 2
If Not rsData.EOF Then
''#Dump the contents of the recordset onto the worksheet
On Error GoTo errHandler
ActiveSheet.Cells(ActiveCell.Row + 1, ActiveCell.Column).CopyFromRecordset rsData
If Not rsData.EOF Then
MsgBox "Data set too large for a worksheet!"
End If
rsData.Close
End If
Unload frmSQLQueryADO
Exit Sub
errHandler:
MsgBox Err.Description, vbCritical, "Error No: " & Err.Number
''#Unload frmSQLQueryADO
End Sub