按列位置将Excel电子表格导入MS Access

时间:2018-08-31 10:54:18

标签: vba csv ms-access

嗨,大家好,我遇到的问题是我要导入的文件没有列名,由于其他原因,我无法轻松地添加它们。但是,我确实知道我想要哪些列。因此,我用列位置制作了一个表,以便我的vba可以遍历并逐行添加各个字段。但我似乎得到的是,该变量未被读取,它认为FieldID是列名。那么,如何让vba理解我希望它从中获取值的列?

Dim db As DAO.Database
Set db = CurrentDb
Dim rsImport As DAO.Recordset
Dim rsFileColumns As DAO.Recordset
Dim CSVCn As Object
Dim rsCSV As Object
Dim strSQL, strName, strName1 As String
Dim rsImporttbl As DAO.Recordset

Set rsImport = db.OpenRecordset("Select * FROM ImportFiles ORDER BY FileID ASC")
If Not (rsImport.EOF And rsImport.BOF) Then 'This Table how the directory and order in which we want to import the tables.
    rsImport.MoveFirst                      'This helps if there any directory changes or if we need to switch the sequence of import
    Do Until rsImport.EOF = True
        Directory = rsImport!Directory
        FileID = rsImport!FileID
        FileName = rsImport!FileName
        FileType = rsImport!FileType
        DbName = rsImport!DatabaseName

        Set rsFileColumns = db.OpenRecordset("Select FieldName, FieldID From ImportField WHERE FileID =" & FileID & ";") 'This Tells the program which columns we want to import
            If Not (rsFileColumns.EOF And rsFileColumns.BOF) Then 'Also if needed we do have the column numbers stored
                rsFileColumns.MoveFirst
                Do Until rsFileColumns.EOF = True
                    FieldID = rsFileColumns!FieldID
                        Set CSVCn = CreateObject("ADODB.Connection")
                        Set rsCSV = CreateObject("ADODB.Recordset")

                        strSQL1 = "Provider=" & _
                                  CurrentProject.Connection.Provider & _
                                  ";Data Source=" & Directory & _
                                  ";Extended Properties='text;HDR=YES;FMT=Delimited'"
                        CSVCn.Open strSQL1 'Tells it where to look for the CSV to import

                        strSQL = "SELECT * FROM " & FileName & FileType 'Tells what the file is called to import
                        rsCSV.Open strSQL, CSVCn, adOpenForwardOnly, adLockReadOnly, adCmdText

                        Set rsImporttbl = db.OpenRecordset(DbName, dbOpenTable, dbAppendOnly + dbFailOnError) 'This is the table its appending the data to.

                        Do Until rsCSV.EOF
                            Field1 = rsCSV!FieldID 'Using the column number stored find the value
                            rsImporttbl.AddNew
                                                'please insert it into the same field number into the databases empty verison
                            rsImporttbl.Update
                            rsCSV.MoveNext
                        Loop
                        rsImporttbl.Close
                        rsCSV.Close
                rsFileColumns.MoveNext
                Loop
            Else
                MsgBox "There are no Records in ImportFields for this table: " & FileName
            End If
            rsFileColumns.Close
            Set rsFileColumns = Nothing

    rsImport.MoveNext
    Loop
Else
    MsgBox "There are no Records in ImportFiles"
End If
rsImport.Close
Set rsImport = Nothing
db.Close

2 个答案:

答案 0 :(得分:1)

只需不用爆炸符号即可,您可以使用FieldID访问特定字段:

Field1 = rsCSV.Fields(FieldID)不是rsCSV!FieldID

答案 1 :(得分:0)

大家好,我进一步介绍了这两个表,在这两个表之间有两个表ImportFiles表和ImportFields表,我可以将程序定向到哪里以及要导入的字段。这是代码。

Dim db As DAO.Database
Set db = CurrentDb
Dim rsImport As DAO.Recordset
Dim rsFileColumns As DAO.Recordset
Dim CSVCn As Object
Dim rsCSV As Object
Dim strSQL, strName, strName1 As String
Dim rsImporttbl As DAO.Recordset
Dim ColumnIDs As New VBA.Collection
Dim ColumnVals As New VBA.Collection

Set rsImport = db.OpenRecordset("Select * FROM ImportFiles ORDER BY FileID ASC")
If Not (rsImport.EOF And rsImport.BOF) Then 'This Table how the directory and order in which we want to import the tables.
    rsImport.MoveFirst                      'This helps if there any directory changes or if we need to switch the sequence of import
    Do Until rsImport.EOF = True
        Directory = rsImport!Directory
        FileID = rsImport!FileID
        FileName = rsImport!FileName
        FileType = rsImport!FileType
        DbName = rsImport!DatabaseName

        Set rsFileColumns = db.OpenRecordset("Select FieldID From ImportField WHERE FileID =" & FileID & ";") 'This Tells the program which columns we want to import
        Set ColumnIDs = RSToColl(rsFileColumns, "FieldID")

        Set CSVCn = CreateObject("ADODB.Connection")
        Set rsCSV = CreateObject("ADODB.Recordset")
        strSQL1 = "Provider=" & _
                    CurrentProject.Connection.Provider & _
                    ";Data Source=" & Directory & _
                    ";Extended Properties='text;HDR=YES;FMT=Delimited'"
        CSVCn.Open strSQL1 'Tells it where to look for the CSV to import
        strSQL = "SELECT * FROM " & FileName & FileType 'Tells what the file is called to import
        rsCSV.Open strSQL, CSVCn, adOpenForwardOnly, adLockReadOnly, adCmdText

        Set rsImporttbl = db.OpenRecordset(DbName, dbOpenTable, dbAppendOnly + dbFailOnError) 'This is the table its appending the data to.
Dim l As Long
Dim value1 As Integer

        Do Until rsCSV.EOF
            For Each Item In ColumnIDs
                Field1 = rsCSV.Fields(Item)
                ColumnVals.Add Field1
            Next
            value1 = 0
            rsImporttbl.AddNew
                For l = 1 To ColumnIDs.Count
                    Value2 = ColumnVals.Item(l)
                    rsImporttbl.Fields(value1) = Value2
                    value1 = value1 + 1
                Next
            rsImporttbl.Update
            value1 = 0
            Set ColumnVals = Nothing
        rsCSV.MoveNext
        Loop
Loop
Else
    MsgBox "There are no Records in ImportFiles"
End If
rsImport.Close
Set rsImport = Nothing
db.Close