txt檔案如下:
73090700001-0001 ,0056100,1 ,51Y9 , 0.115,5 ,9099-0009-24547,9099-0009-24547, ,
73090700002-0001 ,0056100,1 ,51Y9 , 0.115,5 ,9200-0032-70161,9200-0032-70161, ,
資料庫如下:
no FSC Qty Site Weight BoxType BBID
73090700001-0001 56100 1 51Y9 0.115 5 9099-0009-24547
代碼如下:
Private Sub Command2_Click()
Dim szsql$
Dim no, fsc
Dim combo1
Set cnn = New ADODB.Connection
If combo1.Text = "" Then
MsgBox "請選擇匯入資料!", vbInformation, "提示"
Else
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= db.mdb" '連接資料源
cnn.ConnectionTimeout = 30
cnn.Open
Open combo1.text For Input As #combo1.text
Do While Not EOF(1)
Input #combo1.text, no, fsc
szsql = "insert into PD(no,fsc) values ('" & Trim(no) & "','" & Trim(fsc) & "')"
cnn.Execute (szsql)
Loop
Close #1
cnn.CommitTrans
MsgBox "完成"
End If
End Sub
uj5u.com熱心網友回復:
Private Sub Command2_Click()
Dim Arr() As String, FN As Integer, Data As String
Set cnn = New ADODB.Connection
If Len(Dir(combo1.Text)) = 0 Then
MsgBox "請選擇匯入資料!", vbInformation, "提示"
Else
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= " & Replace(App.Path & "\db.mdb", "\\", "\") '連接資料源
cnn.Open
FN = FreeFile
Open combo1.Text For Input As FN
Do Until EOF(FN)
Line Input #FN, Data
If InStr(Data, ",") > 0 Then
Arr = Split(Data, ",")
'資料庫里的欄位和TXT檔案里的內容對應關系你自己處理吧,你都沒說清楚的
cnn.execute "insert into PD(no,fsc) values ('" & Arr(0) & "','" & Arr(1) & "')"
End If
Loop
Close #FN
cnn.Close
MsgBox "完成"
End If
Set cnn = Nothing
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/139657.html
標籤:VB基礎類
上一篇:未發現可創建的公共類部件
