http://community.csdn.net/Expert/topic/4065/4065952.xml?temp=.7573206
我运行后,添加进的数据库,每一条都重复地写进5次,如果有10条记录的话,写进数据库就变成50条了,这是怎么回事呢,我一直检查,也查不出错来
是不是txt文件内容重复呢
呵呵,想过头了,冰说的没错,哪儿不能用循环了
Option Explicit
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
Dim strTest() As String
Dim strString() As String
Dim str As String, strTxt As String
Dim i
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=" & App.Path & "\db1.mdb;Persist " _
& "Security Info=False;Jet OLEDB:Database Password="
Open App.Path & "\test.txt" For Input As #1
While Not EOF(1)
Line Input #1, str
If str = "" Then Exit Sub
strTxt = ""
strTest = Split(str)
For i = 0 To UBound(strTest)
If strTest(i) <> "" Then
strTxt = strTxt & strTest(i) & "/"
End If
Next i
Debug.Print strTxt
strString = Split(strTxt, "/")
If InStrRev(strString(2), "福建", , 1) Then
sql = "insert into tb (ip1,ip2,address,opt) " & _
"values (" & strString(0) & "," & strString(1) & "," & _
"" & strString(2) & "," & strString(3) & ")"
cn.Execute sql
End If
Wend
Close #1
Set cn = Nothing
End Sub