Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 从文本文件创建访问表_Vba_Ms Access_Ado - Fatal编程技术网

Vba 从文本文件创建访问表

Vba 从文本文件创建访问表,vba,ms-access,ado,Vba,Ms Access,Ado,我需要从文本文件创建access(2007)表。我提前知道应该存在哪些列,但有时供应商会出错并提交一个包含错误列数的文本文件。所以我不想预先指定列。我想将所有数据作为文本加载到存在的任何列中。然后我会做QC 这些列由管道分隔,每个记录有200多列。没有列标题,但文件有一行标题文本,末尾有一行说明有多少条记录。一个文本文件中可能有1条到5000多条记录。记录用CRLF(windows)标识 到目前为止,我所拥有的是这样的,它是有效的(因为它读取文件并在记录集中放置正确的信息(列和记录),我可以计算

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

这些列由管道分隔,每个记录有200多列。没有列标题,但文件有一行标题文本,末尾有一行说明有多少条记录。一个文本文件中可能有1条到5000多条记录。记录用CRLF(windows)标识

到目前为止,我所拥有的是这样的,它是有效的(因为它读取文件并在记录集中放置正确的信息(列和记录),我可以计算记录的数量),除了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”,但这两个驱动程序似乎都需要这样来删除列标题

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


非常感谢您提供的帮助。

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

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
运行代码后,下面是我的(简单)表:


您要做多少?使用导入向导怎么样?我现在使用导入向导,但这越来越不切实际了。我开始每周收到5到6份这样的文件,现在我每周收到20到30份,可能还会收到更多。