如何用vb把txt文件的数据导入access数据库?
0.2281662.0000000.8542590.2269972.0000000.5722940.2337172.0000000.8574120.2492711.000...
0.228166 2.000000 0.854259
0.226997 2.000000 0.572294
0.233717 2.000000 0.857412
0.249271 1.000000 0.625431
txt文件里的数据类似上面格式,3列,空格分隔,行数不确定,
想导入access数据库,我的程序是这样的
Private Sub Command2_Click()
Dim log1, log2, log3 As Long
Dim Con As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\My Documents\log97.mdb;Persist Security Info=False"
Con.Open
Open App.Path & "\AA.txt" For Input As #1
Do While Not EOF(1)
Input #1, log1, log2, log3
Con.Execute ("insert into table(col1,col2,col3) values(log1,log2,log3)")
Loop
Close #1
ErrHandler:
' 用户按了“取消”按钮
Exit Sub
End Sub
Con.Execute ("insert into table(col1,col2,col3) values(log1,log2,log3)")这一段出错,如果把values(log1,log2,log3)"改成values(1,2,3)access里面的表里能出现一列记录1,2,3
。问题出来了,我把txt文件的数据赋值给变量log1,log2,log3.然后再把这三个变量加入到access中,就不行了,问题出在哪里,望大神赐教! 展开
0.226997 2.000000 0.572294
0.233717 2.000000 0.857412
0.249271 1.000000 0.625431
txt文件里的数据类似上面格式,3列,空格分隔,行数不确定,
想导入access数据库,我的程序是这样的
Private Sub Command2_Click()
Dim log1, log2, log3 As Long
Dim Con As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\My Documents\log97.mdb;Persist Security Info=False"
Con.Open
Open App.Path & "\AA.txt" For Input As #1
Do While Not EOF(1)
Input #1, log1, log2, log3
Con.Execute ("insert into table(col1,col2,col3) values(log1,log2,log3)")
Loop
Close #1
ErrHandler:
' 用户按了“取消”按钮
Exit Sub
End Sub
Con.Execute ("insert into table(col1,col2,col3) values(log1,log2,log3)")这一段出错,如果把values(log1,log2,log3)"改成values(1,2,3)access里面的表里能出现一列记录1,2,3
。问题出来了,我把txt文件的数据赋值给变量log1,log2,log3.然后再把这三个变量加入到access中,就不行了,问题出在哪里,望大神赐教! 展开
3个回答
展开全部
编写一个Schema.ini放在Txt文件的目录下
[customer.txt] //Txt文件名
ColNameHeader=True
format=Delimited(",") //指定分隔符
MaxScanRows=0
CharacterSet=ANSI
Col1="字段1" Char Width 20 //指定字段的数据类型
Col2="字段2" Char Width 20
然后在程序中执行SQL:
Select * Into customer From [Text;Database=c:\temp].customer.txt
[customer.txt] //Txt文件名
ColNameHeader=True
format=Delimited(",") //指定分隔符
MaxScanRows=0
CharacterSet=ANSI
Col1="字段1" Char Width 20 //指定字段的数据类型
Col2="字段2" Char Width 20
然后在程序中执行SQL:
Select * Into customer From [Text;Database=c:\temp].customer.txt
展开全部
要形成sql字符串提交.
Con.Execute ("insert into table(col1,col2,col3) values( " & log1 & "," & log2 & "," & log3 & ")")
Con.Execute ("insert into table(col1,col2,col3) values( " & log1 & "," & log2 & "," & log3 & ")")
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
刚回答错了,删除
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询