lokeshsu
Board Regular
- Joined
- Mar 11, 2010
- Messages
- 178
Hi All,
i have this cross tab query and it works fine in the access and i am trying to pull the data in the excel using the below vba code and i am getting an error message runtime error 3709. need help in fixing the issue
i have this cross tab query and it works fine in the access and i am trying to pull the data in the excel using the below vba code and i am getting an error message runtime error 3709. need help in fixing the issue
Code:
Sub ApplyCrossTab()
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim Myworkbook As String
Dim SQL As String
Dim i As Integer
'Set con = New Connection
'Set rs = New Recordset
'Identify the workbook you are referencing
Myworkbook = Application.ThisWorkbook.FullName
'Open connection to the workbook
'Set con = New ADODB.Connection
con.ConnectionString = Connection
con.Provider = "Microsoft.ACE.OLEDB.12.0;"
con.Open "C:\lokesh bkp\Project\Forecasting\VzT.accdb"
'Build SQL Statement
SQL = "TRANSFORM Sum(Query1.Val) AS SumOfVal " & _
"SELECT Query1.Skill, Segment.RE " & _
"FROM Query1 LEFT JOIN Segment ON Query1.Skill = Segment.Skill " & _
"GROUP BY Query1.Skill, Segment.RE " & _
"PIVOT Format([Date],""mmm-yy"");"
MsgBox SQL
'Load the Query into a Recordset
rs.Open SQL
'Place the Recordset onto Sheet2
With ActiveWorkbook.Sheets("MTD Data").Range("A1")
.CopyFromRecordset rs
End With
'Place the Recordset onto Sheet2
With Sheets("MTD Data")
.Range("A1").CopyFromRecordset rs
'Add column heading names
For i = 1 To rs.Fields.Count
.Cells(1, i).Value = rs.Fields(i - 1).Name
Next i
End With
End Sub