Dumping data to new worksheets - 90% there

imodan

New Member
Joined
Feb 5, 2009
Messages
3
Hi all. First of all I don't know VBA. I understand what it's doing but as far as the syntax is concerned for putting new stuff in, it's all greek to me I'm afraid.

Below is the code I have right now that works beautifully at:

1. Connecting to the db
2. Running some SQL
3. Dumping the sql to an excel spreadsheet.

Now all I want to do is to run about another 5 queries and output each to a new worksheet within the same workbook.

Could anyone give me a hand with what's required here?


Thankyou so much

Code below.


Private Sub cmdGetQuery_Click()
' Open Database connection
DataAccess.OpenDatabaseConnection

' Get Recordset
Dim rs As ADODB.Recordset
Set rs = DataAccess.GetResultSet("SELECT * FROM wrap_tst2.dbo.amd_fee_payee_and_freq")

' Populate Sheet with the data
Dim dbSheet As Worksheet
Set dbSheet = Sheets("db")
Call CopyRecordSetToSheet(rs, dbSheet.Range("A5").Address)


End Sub

Sub CopyRecordSetToSheet(ByVal dataRs As Recordset, ByVal startCellAddress As String)
Dim lp As Integer
For lp = 0 To rowsToCopy - 1
Rows(templateRowAddress & ":" & templateRowAddress).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Next

Dim fldCount As Integer
fldCount = dataRs.Fields.Count
For iCol = 1 To fldCount
ActiveSheet.Cells(4, iCol).Value = dataRs.Fields(iCol - 1).Name
Next

' Check version of Excel
If 1 = 1 Then
'EXCEL 2000,2002,2003, or 2007: Use CopyFromRecordset
ActiveSheet.Range(startCellAddress).Select
' Copy the recordset to the worksheet, starting in cell A2
Selection.CopyFromRecordset dataRs
'Note: CopyFromRecordset will fail if the recordset
'contains an OLE object field or array data such
'as hierarchical recordsets

End If


End Sub

Function TransposeDim(v As Variant) As Variant
' Custom Function to Transpose a 0-based array (v)

Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
Dim tempArray As Variant

Xupper = UBound(v, 2)
Yupper = UBound(v, 1)

ReDim tempArray(Xupper, Yupper)
For X = 0 To Xupper
For Y = 0 To Yupper
tempArray(X, Y) = v(Y, X)
Next Y
Next X

TransposeDim = tempArray

End Function
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top