I am trying to export a table from access to excel and transpose layout in excel. For example:
Tran Op # Fish Op # Date Start Date end Catch Transfer
1 1 14/05/2011 15/05/2011 Y N
2 2 N Y Y
resuly needed:
Transpose to:
Transfer Op # 1 2
Fishing Op# 1 2
Date Start 14/05/2011 12:33
Date End 14/05/2011 12:33
(BCD) not presented Y Y
obstructed in their duties Y N
Access to communication facilities denied Y Y
catch 10% greater N N
Code I am using:
Function SendToExcel(strQueryName As String)
On Error GoTo SendToExcel_Fail
Dim objWS As Excel.Worksheet
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim intColCount As Integer
Dim intRowCount As Integer
Dim gobjExcel As Excel.Application
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Invoke HourGlass
DoCmd.Hourglass True
'Try to create recordset and create Excel Object
If CreateRecordSet(rstData, rstCount, "qry_OriginalNonComplianceLayout") Then
If CreateExcelObj() Then
'add a workbooks
gobjExcel.Workbooks.Add
'Create a reference to the active sheet
Set objWS = gobjExcel.ActiveSheet
intRowCount = 1
intColCount = 1
'Loop through the fields collection
'make each field name a collumn heading in excel
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
objWS.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
'send record set to excel
objWS.Range("A2").CopyFromRecordset rstData, 35000
gobjExcel.Range("A1").Select
'Set AutoFilter
gobjExcel.Selection.AutoFilter
gobjExcel.Visible = True
Else
MsgBox "Excel not Successfully Launched", vbInformation
End If
Else
MsgBox "Too many Records to Send to Excel", vbInformation
End If
Exit_SendToExcel:
DoCmd.Hourglass False
Set objWS = Nothing
Set rstCount = Nothing
Set rstData = Nothing
Set fld = Nothing
Exit Function
SendToExcel_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SendToExcel
End Function
‘####
Function CreateExcelObj() As Boolean
On Error GoTo CreateExcelObj_Fail
Dim gobjExcel As Excel.Application
'Assume a false return value
CreateExcelObj = False
'Start Excel
Set gobjExcel = New Excel.Application
'if Excel start successfully, return true
gobjExcel.Visible = True
CreateExcelObj = True
Exit_CreateExcelObj:
Exit Function
CreateExcelObj_Fail:
MsgBox "Count not launch Excel.", vbCritical, "Warning"
CreateExcelObj = False
Resume Exit_CreateExcelObj
End Function
‘##############
Function CreateRecordSet(rstData As ADODB.Recordset, rstCount As ADODB.Recordset, _
strTableName As String)
On Error GoTo CreateRecordSet_Fail
'Create recordset that contains count of records in query resuklt
rstCount.Open "Select Count(*) as NumRecords From " & strTableName
'if more than 35000 records in query result, return false
'otherwise, create recordset from query
If rstCount!numrecords > 35000 Then
CreateRecordSet = False
Else
rstData.Open strTableName
CreateRecordSet = True
End If
Exit_CreateRecordSet:
Set rstCount = Nothing
Exit Function
CreateRecordSet_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_CreateRecordSet
End Function
‘#####
Problems:
When I run the code :
SendToExcel("TableName")
I get an error message (Error: 91 was generated by database. Object variable or with block variable not set.
I also do not now how to fix and have to transpose . Please help with fixing the code.
Tran Op # Fish Op # Date Start Date end Catch Transfer
1 1 14/05/2011 15/05/2011 Y N
2 2 N Y Y
resuly needed:
Transpose to:
Transfer Op # 1 2
Fishing Op# 1 2
Date Start 14/05/2011 12:33
Date End 14/05/2011 12:33
(BCD) not presented Y Y
obstructed in their duties Y N
Access to communication facilities denied Y Y
catch 10% greater N N
Code I am using:
Function SendToExcel(strQueryName As String)
On Error GoTo SendToExcel_Fail
Dim objWS As Excel.Worksheet
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim intColCount As Integer
Dim intRowCount As Integer
Dim gobjExcel As Excel.Application
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Invoke HourGlass
DoCmd.Hourglass True
'Try to create recordset and create Excel Object
If CreateRecordSet(rstData, rstCount, "qry_OriginalNonComplianceLayout") Then
If CreateExcelObj() Then
'add a workbooks
gobjExcel.Workbooks.Add
'Create a reference to the active sheet
Set objWS = gobjExcel.ActiveSheet
intRowCount = 1
intColCount = 1
'Loop through the fields collection
'make each field name a collumn heading in excel
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
objWS.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
'send record set to excel
objWS.Range("A2").CopyFromRecordset rstData, 35000
gobjExcel.Range("A1").Select
'Set AutoFilter
gobjExcel.Selection.AutoFilter
gobjExcel.Visible = True
Else
MsgBox "Excel not Successfully Launched", vbInformation
End If
Else
MsgBox "Too many Records to Send to Excel", vbInformation
End If
Exit_SendToExcel:
DoCmd.Hourglass False
Set objWS = Nothing
Set rstCount = Nothing
Set rstData = Nothing
Set fld = Nothing
Exit Function
SendToExcel_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SendToExcel
End Function
‘####
Function CreateExcelObj() As Boolean
On Error GoTo CreateExcelObj_Fail
Dim gobjExcel As Excel.Application
'Assume a false return value
CreateExcelObj = False
'Start Excel
Set gobjExcel = New Excel.Application
'if Excel start successfully, return true
gobjExcel.Visible = True
CreateExcelObj = True
Exit_CreateExcelObj:
Exit Function
CreateExcelObj_Fail:
MsgBox "Count not launch Excel.", vbCritical, "Warning"
CreateExcelObj = False
Resume Exit_CreateExcelObj
End Function
‘##############
Function CreateRecordSet(rstData As ADODB.Recordset, rstCount As ADODB.Recordset, _
strTableName As String)
On Error GoTo CreateRecordSet_Fail
'Create recordset that contains count of records in query resuklt
rstCount.Open "Select Count(*) as NumRecords From " & strTableName
'if more than 35000 records in query result, return false
'otherwise, create recordset from query
If rstCount!numrecords > 35000 Then
CreateRecordSet = False
Else
rstData.Open strTableName
CreateRecordSet = True
End If
Exit_CreateRecordSet:
Set rstCount = Nothing
Exit Function
CreateRecordSet_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_CreateRecordSet
End Function
‘#####
Problems:
When I run the code :
SendToExcel("TableName")
I get an error message (Error: 91 was generated by database. Object variable or with block variable not set.
I also do not now how to fix and have to transpose . Please help with fixing the code.