M
Mirco Wilhelm
Hi,
I can open the Excel file I want, but how can I get data from a field range,
like A14:H44 into a recordset Field1 to Field8 and write it into my
database?
I already searched for some code examples but only found this export to
Excel function.
would be great, if someone could help me rewrite it to an ImportRSFromExcel
function
_________________________________________________________________
Public Function ExportRSToExcel( _
ByVal objRS As Object, _
ByVal bolFieldNames As Boolean) _
As Boolean
On Error GoTo ExportRSToExcel_Error
'---------------------------------------------------------------------------
----------------
' Export an Access Recordset to a new Excel Workbook
'---------------------------------------------------------------------------
----------------
Dim objExcel As Excel.Application
Dim objExcelSheet As Excel.Worksheet
Dim intExcelCalcMode As Integer
Dim i As Integer
' create new excel object
Set objExcel = New Excel.Application
' make visible and block userinput
objExcel.Visible = True
objExcel.Interactive = False
' create new worksheet
objExcel.Workbooks.Add
' save old calcMode and set it temporary to manual
intexcelcalmode = objExcel.Calculation
objExcel.Calculation = xlCalculationManual
' set reference tonew worksheet
Set objExcelSheet = objExcel.ActiveWorkbook.Sheets(1)
' if fieldnames are set write them to the first line
If bolFieldNames = True Then
For i = 0 To objRS.Fields.Count - 1
objExcelSheet.Cells(1, i + 1).value = objRS.Fields(i).Name
Next i
' bold formatting
objExcelSheet.Range(objExcelSheet.Cells(1, 1),
objExcelSheet.Cells(1, i)).Font.Bold = True
' disable display refresh
objExcel.ScreenUpdating = False
' read recordset
objExcelSheet.Range("A2").CopyFromRecordset objRS
Else
' disable display refresh
objExcel.ScreenUpdating = False
' read recordset
objExcelSheet.Range("A1").CopyFromRecordset objRS
End If
' activate display refresh
objExcel.ScreenUpdating = True
' restore calculation mode
objExcel.Calculation = intExcelCalcMode
' activate user input
objExcel.Interactive = True
' destroy object references
Set objExcelSheet = Nothing
Set objExcel = Nothing
ExportRSToExcel = True
ExportRSToExcel_Error:
End Function
_________________________________________________________________
I can open the Excel file I want, but how can I get data from a field range,
like A14:H44 into a recordset Field1 to Field8 and write it into my
database?
I already searched for some code examples but only found this export to
Excel function.
would be great, if someone could help me rewrite it to an ImportRSFromExcel
function
_________________________________________________________________
Public Function ExportRSToExcel( _
ByVal objRS As Object, _
ByVal bolFieldNames As Boolean) _
As Boolean
On Error GoTo ExportRSToExcel_Error
'---------------------------------------------------------------------------
----------------
' Export an Access Recordset to a new Excel Workbook
'---------------------------------------------------------------------------
----------------
Dim objExcel As Excel.Application
Dim objExcelSheet As Excel.Worksheet
Dim intExcelCalcMode As Integer
Dim i As Integer
' create new excel object
Set objExcel = New Excel.Application
' make visible and block userinput
objExcel.Visible = True
objExcel.Interactive = False
' create new worksheet
objExcel.Workbooks.Add
' save old calcMode and set it temporary to manual
intexcelcalmode = objExcel.Calculation
objExcel.Calculation = xlCalculationManual
' set reference tonew worksheet
Set objExcelSheet = objExcel.ActiveWorkbook.Sheets(1)
' if fieldnames are set write them to the first line
If bolFieldNames = True Then
For i = 0 To objRS.Fields.Count - 1
objExcelSheet.Cells(1, i + 1).value = objRS.Fields(i).Name
Next i
' bold formatting
objExcelSheet.Range(objExcelSheet.Cells(1, 1),
objExcelSheet.Cells(1, i)).Font.Bold = True
' disable display refresh
objExcel.ScreenUpdating = False
' read recordset
objExcelSheet.Range("A2").CopyFromRecordset objRS
Else
' disable display refresh
objExcel.ScreenUpdating = False
' read recordset
objExcelSheet.Range("A1").CopyFromRecordset objRS
End If
' activate display refresh
objExcel.ScreenUpdating = True
' restore calculation mode
objExcel.Calculation = intExcelCalcMode
' activate user input
objExcel.Interactive = True
' destroy object references
Set objExcelSheet = Nothing
Set objExcel = Nothing
ExportRSToExcel = True
ExportRSToExcel_Error:
End Function
_________________________________________________________________