从文本文件创建Access表

时间:2013-05-21 20:03:37

标签: vba ms-access ado

我需要从文本文件创建一个access(2007)表。我提前知道哪些列应该存在,但是供应商会不时地提交并提交包含错误列数的文本文件。所以我不想提前指定列。我想将所有数据作为文本加载到任何列中。然后我会做QC。

列是以管道分隔的,每条记录有超过200列。没有列标题,但文件有一行标题文本,最后一行表示有多少条记录。文本文件中可能有1到5,000多条记录。记录用CRLF(窗口)标识。

这是我到目前为止所做的,它的工作原理(因为它读取文件并将正确的信息放在记录集中(列和记录),我可以计算记录的数量),但SELECT INTO除外给我一个错误:

Sub OpenTextADO(strFileName As String, strPath As String)

  Dim cn As ADODB.Connection
  Dim rs As ADODB.Recordset
  Dim fld As ADODB.Field
  Dim recs As Integer
  Dim strRecord As String
  Dim strSQL As String

  recs = 0

  Set cn = New ADODB.Connection

  If Right(strFileName, 3) = "txt" Then
    'cn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};" & "DBQ=" & strPath & "\"  'need schema.ini file
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & "\;Extended Properties='text;HDR=No;FMT=Delimited(|)'"  'need schema.ini file
  End If

  Set rs = New ADODB.Recordset
  rs.Open "SELECT * INTO TESTTEXT FROM [" & strFileName & "]", cn, adOpenStatic, adLockOptimistic, adCmdText


  'Do Until rs.EOF
  '  For Each fld In rs.Fields
  '    strRecord = strRecord & "|" & fld.Value
  '  Next fld
  '  strRecord = strRecord & vbCr
  '  recs = recs + 1
  '  rs.MoveNext
  'Loop

  'Debug.Print strRecord

  'recs = rs.RecordCount

  rs.Close
  Set rs = Nothing
  MsgBox "Text was opened and there are " & recs & " records in the table."

  cn.Close
  Set cn = Nothing

End Sub

注意:我包括OLEDB版本和文本驱动程序版本 - 它们似乎都运行相同。我还创建了一个如下所示的schema.ini文件:

[test.txt]
Format=Delimited(|)
ColNameHeader=False

尽管OLEDB版本中存在“HDR = No”,但两个驱动程序似乎都需要这样来忽略列标题。

我得到的错误是:“无法更新。数据库或对象是只读的。”

我感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您是否可以对文本文件进行顺序读取,使用文件第一行数据行中的竖线分隔字段计数来创建具有适当列数的表,然后将后续行写入该表?我只是将以下内容放在一起,但似乎有效。

Public Function import_txt_to_db(strFile As String) As Boolean
On Error GoTo ErrHandle

Dim strLine As String
Dim intFileNum As Integer

Dim blnFirstLine As Boolean
blnFirstLine = True

Dim varArray As Variant

intFileNum = FreeFile

Open strFile For Input Access Read As intFileNum

Do While Not EOF(intFileNum)

    Line Input #intFileNum, strLine
    varArray = Split(strLine, "|")

    If blnFirstLine = True Then
        'Use count of fields in first line to determine # of columns to create
        Dim intColCount As Integer
        intColCount = UBound(varArray)

        Dim strQry As String
        strQry = "CREATE TABLE tblImport ("
        Dim intCtr As Integer
        For intCtr = 1 To intColCount + 1
            strQry = strQry & "[COLUMN_" & intCtr & "] TEXT(255),"
        Next intCtr

        strQry = Left(strQry, Len(strQry) - 1) & ")" 'get rid of terminal comma

        CurrentDb.Execute strQry

        blnFirstLine = False

    End If

    Dim strQry2 As String
    strQry2 = "INSERT INTO tblImport VALUES('" & Replace(strLine, "|", "','") & "')"

    CurrentDb.Execute strQry2

Loop

Close #intFileNum
import_txt_to_db = True

Exit Function

ErrHandle:
import_txt_to_db = False

End Function

我使用下面的五行文本文件进行了简单的测试

Thomas|Jefferson|Virginia
Bill|Clinton|Arkansas
Jimmy|Carter|Georgia
Lyndon|Johnson|Texas
George|Washington|Virginia

运行代码后,这是我的(简单)表:

vba-generated table