【问题标题】:Create Access table from text file从文本文件创建 Access 表
【发布时间】:2013-06-25 01:22:30
【问题描述】:

我需要从一个文本文件创建一个访问 (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”,但两个驱动程序似乎都需要这个来忽略列标题。

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

感谢您的帮助。

【问题讨论】:

  • 你做了多少?使用导入向导怎么样?
  • 我现在使用导入向导,但这变得不切实际。我开始每周可能会收到 5 或 6 个这样的文件,现在我每周会收到 20-30 个,而且可能会收到更多。

标签: vba ms-access ado


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多