VB Access 2010解析分隔行到列

时间:2011-09-14 18:33:54

标签: ms-access access-vba

Function splitWords(ByVal strLine)

Dim ParsedStrLine() As String

ParsedStrLine = Split(strLine, ", ")

For i = LBound(ParsedStrLine) To UBound(ParsedStrLine)
    splitWords = ParsedStrLine(i)

Next

End Function

我对VB几乎一无所知。 我正在尝试编写一个函数,它接受一个csv字段并追加到不同列中的同一个表。

THIS
someTable:
col_1 col_2
a     x, y, z

TO THIS
someTable:
col_1 col_2
a     x, y, z
x
y
z

如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

只有一行:

ParsedStrLine = Split(strLine, ", ")

For i = LBound(ParsedStrLine) To UBound(ParsedStrLine)
    splitWords = ParsedStrLine(i)
    CurrentDB.Execute "INSERT INTO tbl (Col1) VALUES ('" & splitWords & "'")
Next

但您可能希望使用整个记录集。

Dim db As Database
Dim rs As Recordset
Dim rsAdd As Recordset

Set db = CurrentDb

Set rs = db.OpenRecordset("SELECT * FROM Table1 WHERE Col_2 Is Not Null")
Set rsAdd = db.OpenRecordset("Table1")

Do While Not rs.EOF
    ary = Split(rs!col_2, ", ")
    For i = 0 To UBound(ary)
        rsAdd.AddNew
        rsAdd!col_1 = ary(i)
        rsAdd.Update
    Next
    rs.MoveNext
Loop