我有两个为我创建记录集的函数,然后我将其读入电子表格。
然而,这一个excel报告的列中没有数据,并且由于某些奇怪的原因,它首次从文件中读取整个列的数据。
例如,某些类型的行具有带有值的列a,b,c,一些具有d,e,f,一些具有g,h,i。因此,例如,碰巧a,b,c首先具有值,但是d到i是几个记录行的空白。看起来因为记录集放在工作表中后,整个d到i的列范围是空白的。
为什么?
这是将数据放入电子表格的代码:
Public Sub PutRecordsetInSheet(ByVal RS As Object, ByVal TopLeft As Range, _
Optional ByVal Headers As Boolean = True)
'Lists field names in the top row if Headers=True
'Pastes the records below them.
Dim i As Integer
If Headers Then
For Each objField In RS.Fields
i = i + 1
TopLeft.Cells(1, i).Value = objField.Name
Next objField
TopLeft.Cells(2, 1).CopyFromRecordset RS
Else
TopLeft.Cells(1, 1).CopyFromRecordset RS
End If
End Sub
这是我用来加载文件的代码,我根据excel文件格式在电子表格中有参数:
Public Sub OpenFiles(SettingsSheet As Worksheet, FileCnt As Integer, _
ByRef FileToOpen() As String, ByRef FileSettings() As String, _
ByRef Connection() As Object, RecordSets() As Object, ByRef SheetsArr() As Variant)
'Set File Count
ReDim FileToOpen(1 To 3, 1 To FileCnt) As String
Dim SFile As Integer
'Prepare Dialogs
For cnt = 1 To FileCnt
FileToOpen(1, cnt) = SettingsSheet.Cells(cnt + 1, "B") 'Dialog Report File Description
FileToOpen(2, cnt) = SettingsSheet.Cells(cnt + 1, "C") 'Dialog File Extension Choices"
Next cnt
'Prompt File Dialogs
For cnt = 1 To FileCnt
Dim fDialog As FileDialog, result As Integer
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
'File Dialog Properties
With fDialog
.AllowMultiSelect = False
.Title = FileToOpen(1, cnt)
.InitialFileName = WorkingPath
'File filters
.Filters.Clear
.Filters.Add "Report Files", FileToOpen(2, cnt)
'Show the dialog. -1 means success!
.Show
If .SelectedItems.Count > 0 Then
FileToOpen(3, cnt) = .SelectedItems(1)
ElseIf .SelectedItems.Count = 0 Or .SelectedItems.Count = Empty Then
SFile = cnt
GoTo Cancelled:
End If
End With
Next
'Prepare Connection Settings
Dim CSet As String
ReDim FileSettings(1 To 3, 1 To FileCnt)
For cnt = 1 To FileCnt
CSet = LCase(Right(FileToOpen(3, cnt), Len(FileToOpen(3, cnt)) - InStrRev(FileToOpen(3, cnt), ".")))
x = Application.Match(CSet, SettingsSheet.Range("E1:E" & SettingsSheet.Cells(Rows.Count, "E").End(xlUp).Row), 0)
If Not (IsError(x)) Then
FileSettings(1, cnt) = SettingsSheet.Cells(x, "F") 'Provider
FileSettings(2, cnt) = SettingsSheet.Cells(x, "G") 'Extended Properties 1
FileSettings(3, cnt) = SettingsSheet.Cells(x, "H") 'Extended Properties 2
End If
Set x = Nothing
Next cnt
'Establish Connections and prepare Record Sets
ReDim Connection(1 To FileCnt)
ReDim RecordSets(1 To FileCnt)
For cnt = 1 To FileCnt
Set Connection(cnt) = CreateObject("ADODB.Connection")
Set RecordSets(cnt) = CreateObject("ADODB.Recordset")
With Connection(cnt)
.Provider = FileSettings(1, cnt)
.ConnectionString = "Data Source=" & FileToOpen(3, cnt) & ";" & _
"Extended Properties=" & Chr(34) & FileSettings(2, cnt) & ";" & _
FileSettings(3, cnt) & Chr(34) & ";"
.Open
End With
Next cnt
Exit Sub
Cancelled:
MsgBox "No " & FileToOpen(1, SFile) & " File Specified.", vbExclamation, "User Cancelled"
On Error Resume Next
Call DelTmpSheets(SheetsArr())
FinalReport.Delete
Err.Clear
AppDefaults
End
End Sub