我写了一些VBA代码来从文本文件写入数据。 在文件中,我有以下文字:
H|3434|0104-00000107844/18|Peter|Smith|
D|345345345|"III"|
|
字符是用于分隔表中各列的定界符。
Private Sub Polecenie8_Click()
Dim fieldname As String
fieldname = "d:\odebrane\tekst1.txt"
Dim strLineInput As String
Dim tekst As String
Dim strLineArray As Variant
Dim FileNum As Integer
FileNum = FreeFile()
Open fieldname For Input As #FileNum
Do While Not EOF(FileNum)
Line Input #FileNum, strLineInput
strLineArray = Split(strLineInput, "|")
tekst = strLineArray(3)
Loop
Me.Tekst0 = tekst
Close #FileNum
End Sub
strLineArray(0)
等于D而不是H,我也不知道为什么。
我想将此数组保存在表中,所以我希望strLineArray(0)
等于H,strLineArray(5)
等于D。
答案 0 :(得分:0)
这样的事情怎么样?
Sub imptxttable()
Dim DB As DAO.Database
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim strSQL As String
Dim strfilepath As String
strfilepath = "your_file.txt"
strSQL = "Select * FROM " & strfilepath
Set DB = OpenDatabase("c:\test", False, False, "Text; Format=Delimited(|);HDR=Yes;CharacterSet=437")
Set rst = DB.OpenRecordset(strSQL)
Debug.Print rst.Fields(0).Name, rst.Fields(0)
With rst
.MoveLast
Debug.Print .RecordCount
.MoveFirst
For Each fld In rst.Fields
Debug.Print fld.Name
Next
End With
rst.Close
DB.Close
Set rst = Nothing
Set DB = Nothing
End Sub