我把VBA中的通过ADO连接的代码:
Set cn = CreateObject("adodb.connection")
Set cnstr = CreateObject("adodb.command")
Dim db
Dim sqlstr
Dim data1, data2, data3, data4, data5
‘Set data1=HMIRuntime.Tags("TAg1_Float")
‘Set data2=HMIRuntime.Tags("TAg2_Integer")
‘Set data3 =HMIRuntime.Tags("TAg3_text")
‘Set data4=HMIRuntime.Tags("TAg4_bit")
‘Set data5 HMIRuntime.Tags("TAg5_bp")
db = "c:\Report DB.mdb"
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db & ";Persist Security Info=False"
cn.Open cnstr
sqlstr = "in s e rt into test1(DT,TAG1,TAG2,TAG3,TAG4,TAG5) values (now,‘" & data1 & "‘,‘" & data2 & " ‘,‘" & data3 & "‘,‘" & data4 & "‘,‘" & data5 & "‘)"
cn.Execute sqlstr
cn.Close
Set cn = Nothing
Set cnstr = Nothing
写入accesS,在VBA测试通过,放到VBS没错误 VBS不执行。 郁闷啊
Set cn = CreateObject("adodb.connection")
Set cnstr = CreateObject("adodb.command") //这个不要
Dim db
Dim sqlstr,cnstr //新添加一个cnstr变量
Dim data1, data2, data3, data4, data5
‘Set data1=HMIRuntime.Tags("TAg1_Float")
‘Set data2=HMIRuntime.Tags("TAg2_Integer")
‘Set data3 =HMIRuntime.Tags("TAg3_text")
‘Set data4=HMIRuntime.Tags("TAg4_bit")
‘Set data5 HMIRuntime.Tags("TAg5_bp")
db = "c:\Report DB.mdb"
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db & ";Persist Security Info=False"
cn.ConnectionString= cnstr //加上这句,将字符串cnstr赋值给 cn.ConnectionString
cn.Open cnstr
sqlstr = "in s e rt into test1(DT,TAG1,TAG2,TAG3,TAG4,TAG5) values (now,‘" & data1 & "‘,‘" & data2 & " ‘,‘" & data3 & "‘,‘" & data4 & "‘,‘" & data5 & "‘)"
cn.Execute sqlstr
cn.Close
Set cn = Nothing
Set cnstr = Nothing
在试试上面的语句,希望可以帮到你
Set cn = CreateObject("adodb.connection")
Set cnstr = CreateObject("adodb.command") //这个不要
Dim db
Dim sqlstr,cnstr //新添加一个cnstr变量
Dim data1, data2, data3, data4, data5
‘Set data1=HMIRuntime.Tags("TAg1_Float")
‘Set data2=HMIRuntime.Tags("TAg2_Integer")
‘Set data3 =HMIRuntime.Tags("TAg3_text")
‘Set data4=HMIRuntime.Tags("TAg4_bit")
‘Set data5 HMIRuntime.Tags("TAg5_bp")
db = "c:\Report DB.mdb"
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db & ";Persist Security Info=False"
cn.ConnectionString= cnstr //加上这句,将字符串cnstr赋值给 cn.ConnectionString
cn.Open cnstr
sqlstr = "in s e rt into test1(DT,TAG1,TAG2,TAG3,TAG4,TAG5) values (now,‘" & data1 & "‘,‘" & data2 & " ‘,‘" & data3 & "‘,‘" & data4 & "‘,‘" & data5 & "‘)"
cn.Execute sqlstr
cn.Close
Set cn = Nothing
Set cnstr = Nothing
在试试上面的语句,希望可以帮到你
用下面的代码可以, 感觉VBS 很弱智,也可能是不支持OLEDB的方式。 用ODBC 却成功了
Dim objconnection
Dim srtconnetionsrting
Dim value1,value2,value3,value4,value5
Dim strsql
Dim objcommand
srtconnetionsrting = "provider=msdasql;dsn=test;uid=;piw=;"
value1 = 101
value2=102
value3=103
value4=104
value5=105
strsql = "ins ert into test(DT,tag1,tag2,tag3,tag4,tag5) values (now,‘"&value1&"‘,‘"&value2&"‘,‘"&value3&"‘,‘"&value4&"‘,‘"&value5&"‘)"
Set objconnection = CreateObject("adodb.connection")
objconnection.connectionstring = srtconnetionsrting
objconnection.Open
Set objcommand = CreateObject("adodb.command")
With objcommand
.activeconnection = objconnection
.commandtext = strsql
End With
objcommand.Execute
Set objconnectiond = Nothing
objconnection.Close
Set objconnection = Nothing