用vbs调用
Private Sub CommandButton1_Click()
Dim i As Integer, sht As Worksheet ''i,j为整数变量;sht 为excel工作表对象变量,指向某一工作表
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set rt = CreateObject("ADODB.Recordset")
cn.Open "Provider=sqloledb.1;Integrated Security=SSPI;Persist Security Info=false;Initial Catalog=AE5E751855_0331_TLG_S_200804010439_200804010440;Data Source=AE5E751855\WINCC" ''与数据库建立连接,如果成功,返回连接对象cn
''下面的语句将读取数据表数据,并将它保存到excel工作表中
rs.Open "TAG:R,''''ProcessvalueArchive\NewTag_2'''',''''2008-1-9 5:19:20.000'''',''''2080-1-9 5:22:00.999''''"", cn ''执行strSQL所含的SQL命令,结果保存在rs记录集对象中"
rt.Open "select * from TagCompressed where ValueID=2", cn
Set sht = ActiveWorkbook.Worksheets("sheet3") ''把sht指向当前工作簿的sheet1工作表
sht.Cells(1, 1) = "日期"
sht.Cells(1, 2) = "时间"
sht.Cells(1, 3) = "状态值 1"
sht.Cells(1, 4) = "状态值 2"
i = 2
Do While Not rs.EOF ''当数据指针未移到记录集末尾时,循环下列操作
sht.Cells(i, 1) = Format(rs("TimeStamp"), "yyyy/mm/dd") ''把当前记录的字段1的值保存到sheet1工作表的第i行第1列
sht.Cells(i, 2) = Format(rs("TimeStamp"), "hh")
sht.Cells(i, 3) = rs("RealValue") ''把当前字段2的值保存到sheet1工作表的第i行第2列
sht.Cells(i, 4) = rt("RealValue")
rs.MoveNext ''把指针移向下一条记录
rt.MoveNext
i = i + 1 ''i加1,准备把下一记录相关字段的值保存到工作表的下一行
Loop ''循环
rs.Close ''关闭记录集,至此,程序将把某数据表的字段1和字段2保存在excel工作表sheet1的第1、2列,行数等于数据表的记录数
strSQL = "" ''清空上面定义的变量
cn.Close
Unload UserForm2
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
''Application.Quit
ThisWorkbook.Close SaveChanges:=False
End Sub
其中,select语句可以用,tag语句应该怎么用呢
问题补充:
应该是excel中,应用vba调用归档数据库,慢速归档的用select语句已经可以了(rt.Open "select * from TagCompressed where ValueID=2", cn
),而快速归档的( rs.Open "TAG:R,''''''''ProcessvalueArchive\NewTag_2'''''''',''''''''2008-1-9 5:19:20.000'''''''',''''''''2080-1-9 5:22:00.999''''''''"", cn )语句有错误,应该怎么改正呢
最佳答案
我这里有一个访问归档数据库的代码可供参考,这里的provider使用的不是sqloledb,而是专门访问winCC归档数据库的provider。
'' 1.1 Make connection string for ADODB as indicated below
''"Provider=WinCCOLEDBProvider.1;Catalog=CC_V6_Demo3_03_02_04_16_19_15R;Data Source=.\WinCC"
'' 1.2 Define command text in sSql for WinCC compressed archives, relative time
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=" + sDsn + ";"
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
nRec = NMAX
'' "TAG:R,1,''0000-00-00 00:05:00.000'',''0000-00-00 00:00:00.000''"
lRet = MsgBox("Opened with " & vbCr & sCon & vbCr & sSql & vbCr, vbOKCancel)
If lRet <> 1 Then Exit Sub
'' 2.1 Make connection
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = adUseClient '' 3
conn.Open
'' 2.2 Use command text for query
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1 '' adCmdText = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql
'' 2.3 Fill the recordset
On Error Resume Next ''Fehlerbehandlung - error handling
Set oRs = oCom.Execute
式中的sSql有以下方式可供参考:
''Sql=TAG:R,104,''0000-00-00 00:05:00.000'',''0000-00-00 00:00:00.000''
''Sql=TAG:R,''MARCHIVE\T01'',''0000-00-00 01:00:00.000'',''0000-00-00 00:00:00.000''
''Sql=TAG:R,''MARCHIVE\T01'',''0000-00-00 01:00:00.000'',''2008-4-8 08:10:20''
式中的“CC_V6_Demo3_03_02_04_16_19_15R”是你的winCC应用程序所使用的数据库,可用你的winCC应用程序数据库代替。用sqlserver 2000 数据库管理器就可看到该数据库
提问者对于答案的评价:
已经做出来了,谢谢你了
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc277582.html