需要帮助改进我的Access可视化基本代码以将文本文件分成列

时间:2013-10-21 12:35:33

标签: vba ms-access

我有一个文本文件,如下图所示

enter image description here

这是我的Access可视基本代码,它采用这3行并将它们放入列中

Sub ImportMyFile()
Const strFile As String = "C:\Users\Samuel\Desktop\new.txt"
Dim strLine As String
Dim rs As Recordset
Dim i As Byte

Set rs = CurrentDb.OpenRecordset("Table2")

Open strFile For Input As #1
Do Until EOF(1)
i = i + 1
Line Input #1, strLine

If i = 1 Then
rs.AddNew
rs!Category_NM = strLine
ElseIf i = 2 Then
rs!Activity_Nm = Mid(strLine, 10)
ElseIf i = 3 Then
rs!Class_Nm = Mid(strLine, 7)
End If

If i = 4 Or EOF(1) Then
rs.Update
i = 0
End If
Loop
Close #1
rs.Close
Set rs = Nothing

End Sub

问题是我有一些包含4行的部分,其名称在2中被拆分,例如

You
Tube
package: com.google.android.youtube
class: com.google.android.youtube.app.honeycomb.Shell$HomeActivity

我如何在我的代码中解释这一点?

我要么只是离开“管”并保持“你”或将它们结合起来“You Tube”

我正在处理成千上万的代码,并且真的想要自动化。

1 个答案:

答案 0 :(得分:1)

让我用作旗帜     0 - 需要添加记录     1 - 设置类别     2 - 套餐     3 - 设置课程&更新记录集

Sub ImportMyFile()
Const strFile As String = "C:\Users\Samuel\Desktop\new.txt"
Dim strLine As String
Dim rs As Recordset
Dim i As Byte

Set rs = CurrentDb.OpenRecordset("Table2")

Open strFile For Input As #1
Do Until EOF(1)

    Line Input #1, strLine

    If i = 0 Then
        rs.AddNew
        i = 1
    end if

    If left(strLine, 6) = "class:" Then
        rs!Class_Nm = Mid(strLine, 7)
        i = 3
    End If

    If left(strLine, 8) = "package:" Then
        rs!Activity_Nm = Mid(strLine, 10)
        i = 2
    end if

    If i = 1 Then
        rs!Category_NM = rs!Category_NM & " " & strLine
    end if

    If i=3 or EOF(1) Then
        rs.Update
        i = 0
    End If
Loop
Close #1
rs.Close
Set rs = Nothing

End Sub