Rik_StHelens
Registered User.
- Local time
- Today, 06:02
- Joined
- Sep 15, 2009
- Messages
- 164
Hi All,
I am exporting data from a query to an excel file using the following code from the Knowledge Base:
http://support.microsoft.com/kb/904953
I have the code set up as follows:
I get a "Compile Error - User Defined Type Not Defined" on the 1st line of the private sub.
I am not that strong on VB so any help would be great.
Thanks for your time in advance!
I am exporting data from a query to an excel file using the following code from the Knowledge Base:
http://support.microsoft.com/kb/904953
I have the code set up as follows:
Code:
Public Sub WorkArounds()
On Error GoTo Leave
Dim strSQL, SQL As String
Dim Db As ADODB.Connection
Set Db = New ADODB.Connection
Db.CursorLocation = adUseClient
Db.Open "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\MB Lister 1\My Documents\XMLCreator.accdb"
SQL = "qryExportToExcelToXML"
CopyRecordSetToXL SQL, Db
Db.Close
MsgBox "Access has successfully exported the data to excel file.", vbInformation, "Export Successful."
Exit Sub
Leave:
MsgBox Err.Description, vbCritical, "Error"
Exit Sub
End Sub
Private Sub CopyRecordSetToXL(SQL As String, con As ADODB.Connection)
Dim rs As New ADODB.Recordset
Dim x
Dim i As Integer, y As Integer
Dim xlApp As Excel.Application
Dim xlwbBook As Excel.Workbook, xlwbAddin As Excel.Workbook
Dim xlwsSheet As Excel.Worksheet
Dim rnData As Excel.Range
Dim stFile As String, stAddin As String
Dim rng As Range
stFile = "M:\MML\ExcelToXML.xls"
'Instantiate a new session with the COM-Object Excel.exe.
Set xlApp = New Excel.Application
Set xlwbBook = xlApp.Workbooks.Open(stFile)
Set xlwsSheet = xlwbBook.Worksheets("Sheet1")
xlwsSheet.Activate
'Getting the first cell to input the data.
xlwsSheet.Cells.SpecialCells(xlCellTypeLastCell).Select
y = xlApp.ActiveCell.Column - 1
xlApp.ActiveCell.Offset(1, -y).Select
x = xlwsSheet.Application.ActiveCell.Cells.Address
'Opening the recordset based on the SQL query and saving the data in the Excel worksheet.
rs.CursorLocation = adUseClient
If rs.State = adStateOpen Then
rs.Close
End If
rs.Open SQL, con
If rs.RecordCount > 0 Then
rs.MoveFirst
x = Replace(x, "$", "")
y = Mid(x, 2)
Set rng = xlwsSheet.Range(x)
xlwsSheet.Range(x).CopyFromRecordset rs
End If
xlwbBook.Close True
xlApp.Quit
Set xlwsSheet = Nothing
Set xlwbBook = Nothing
Set xlApp = Nothing
End Sub
I get a "Compile Error - User Defined Type Not Defined" on the 1st line of the private sub.
I am not that strong on VB so any help would be great.
Thanks for your time in advance!