1

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

在此处输入图像描述

这是我的 Access Visual Basic 代码,它采用这 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

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

我要么只需要留下“Tube”并保留“You”,要么将它们组合起来“You Tube”

我正在使用成千上万的代码,并且真的想要一些自动的东西。

4

1 回答 1

1

让我们使用 i 作为标志 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
于 2013-10-21T12:59:22.530 回答