Often times it's necessary to use Excel to format raw data
files being exported from mainframe databases. Once the proper formatting is
applied, the file can then be imported into Access as a native table. But in most
cases, such files contain a large number of records, and creating local tables might not
always be feasible, especially if you need to have reporting (read only) capabilities on
the data.
Excel can be used to save a worksheet as a CSV file which can then
be linked through Access using the Text ISAM drivers. However, Excel doesn't always
wrap the cell values with quotes when saving to a CSV file (a number implies that it will
be written without the quotes). Therefore it's necessary to go through all the
columns and rows of the worksheet and write out the data file using VBA functions.
Here's a generic function which accepts a reference to an Excel
worksheet (which you would have opened through Automation) and the path to a destination
CSV file. It can be called from your code like this
With objXL
.Visible = True
.Workbooks.OpenText FileName:=strFile, Origin:= _
xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 2), _
Array(2, 2), Array(3, 2), Array(4, 2))
Set objWkb = .Workbooks(1)
Call fExportCommaDelimitedFile(objWkb.Worksheets(1), fFileDir(strFile) & _
fFileName(Dir(strFile)) & ".csv")
Function fExportCommaDelimitedFile(objSht As excel.Worksheet, _
strDestinationFile As String) _
As Boolean
Dim intFileNum As Integer
Dim lngColCount As Long
Dim lngTotalColumns As Long
Dim lngTotalRows As Long
Dim lngRowCount As Long
Const conQ = """"
Const conERR_GENERIC = vbObjectError + 2100
intFileNum = FreeFile()
On Error GoTo ErrHandler
Call sAppActivate
If Len(Dir(strDestinationFile)) > 0 Then
If MsgBox("The target file specified " & vbCrLf & vbCrLf _
& strDestinationFile & vbCrLf & vbCrLf & " already exists." _
& vbCrLf & vbCrLf & "Are you sure you want to overwrite it?", _
vbQuestion + vbYesNo, "Please confirm") = vbYes Then
Kill strDestinationFile
Else
Err.Raise conERR_GENERIC
End If
End If
Open strDestinationFile For Output As #intFileNum
With objSht
lngTotalColumns = .UsedRange.Columns.Count
lngTotalRows = .UsedRange.Rows.Count
Call SysCmd(acSysCmdInitMeter, "Writing CSV file...", lngTotalRows)
For lngRowCount = 1 To lngTotalRows
For lngColCount = 1 To lngTotalColumns
Print #intFileNum, conQ & RTrim$(.Cells(lngRowCount, lngColCount).Value) & conQ;
If lngColCount = lngTotalColumns Then
Print #intFileNum,
Else
Print #intFileNum, ",";
End If
Next lngColCount
Call SysCmd(acSysCmdUpdateMeter, lngRowCount)
DoEvents
Next lngRowCount
End With
fExportCommaDelimitedFile = True
ExitHere:
On Error Resume Next
Call SysCmd(acSysCmdRemoveMeter)
Close #intFileNum
Exit Function
ErrHandler:
fExportCommaDelimitedFile = False
Resume ExitHere
End Function
Private Sub sAppActivate()
Dim strCaption As String
On Error Resume Next
strCaption = Application.CurrentDb.Properties("AppTitle")
If Err Then strCaption = "Microsoft Access"
AppActivate strCaption
End Sub
|