关于Microsoft Office Access的问题

怎样用Microsoft Office Access从wincc数据库中中导出归档变量?如果有能不能写详细点?

问题补充:
有没有更详细的注释,我对数据库不是很清楚,如果有人不嫌麻烦的话,请赐教,我将非常感谢!也可以发我邮箱:401772473@qq.com

最佳答案

在wincc6.0中使用vb脚本采用adox动态创建Access数据库,导出SQL server中的归档数据。楼主可以参考如下代码

数据库创建数据导出代码:

Dim begintime,endtime
Dim dcat,dconn,drs,dpstr
Dim dfm,ddlg,dtbl
Dim sPro,sDsn,sSer,sptr,sConn
Dim sSql1,sSql2
Dim sRs1,sRs2
Dim sCmd1,sCmd2
Dim m, n, s, b
b=timeconv(begintime,endtime,0)
If b=False Then
Exit Sub 
Else
'create new database and table
set dcat = createobject("adox.catalog")
set dconn = createobject("adodb.connection")
set drs = createobject("adodb.recordset")
Set ddlg = ScreenItems("dialog")
ddlg.Filter = "MDB文件(*.mdb)|*.mdb|AllFiles(*.*)|*.*|"
ddlg.FilterIndeX = 1
ddlg.InitDir = "E:\vb code"
ddlg.Flags = 6
ddlg.Action = 2

If ddlg.Filename = "" Then
MsgBox "you must input name"
Exit Sub
Else
dfm =ddlg.FileName
End If
dpstr = "Provider=Microsoft.Jet.OLEDB.4.0;"
dpstr = dpstr & "Data Source=" & dfm
dcat.Create dpstr
Set dtbl = CreateObject("adox.table")
dcat.ActiveConnection = dpstr
dtbl.Name = "MyTable" 
dtbl.Columns.Append "time"
dtbl.Columns.Append "tag1value"
dtbl.Columns.Append "tag2value"
dcat.Tables.Append dtbl
dconn.Open dpstr
drs.CursorLocation = 3
drs.Open "MyTable",dconn, 1, 2
'open sql server database
sPro = "Provider = WinCCOLEDBProvider.1;"
sDsn = "Catalog = CC_test_10_04_21_18_05_57R;"
sSer = "Data Source = .\WinCC"
sptr = sPro + sDsn + sSer
sSql1="TAG:R,'SpeedAndTemp\motor_actual'," &begintime& "," &endtime
sSql2="TAG:R,'SpeedAndTemp\oil_temp'," &begintime& "," &endtime

Set sconn = CreateObject("ADODB.Connection")
sconn.ConnectionString = sptr
sconn.CursorLocation = 3
sconn.Open

Set sRs1 = CreateObject("ADODB.Recordset")
Set scmd1 = CreateObject("ADODB.Command")
sCmd1.CommandType = 1
Set sCmd1.ActiveConnection = sconn
sCmd1.CommandText = sSql1
Set sRs1 = sCmd1.Execute

Set sRs2 = CreateObject("ADODB.Recordset")
Set scmd2 = CreateObject("ADODB.Command")
sCmd2.CommandType = 1
Set sCmd2.ActiveConnection = sconn
sCmd2.CommandText = sSql2
Set sRs2 = sCmd2.Execute
m = sRs1.Fields.Count

If (m > 0) Then
MsgBox "bie"
sRs1.Movefirst
srs2.movefirst
MsgBox "now"
n = 0
Do 
n= n + 1
'If (n>1000) Then Exit Do

drs.AddNew 
drs.Fields(0).Value = FormatDateTime(srs1.fields(1).value)
drs.Fields(1).Value = srs1.fields(2).value
drs.Fields(2).ValUe = srs2.fields(2).value
drs.Update
'End If
dRs.MoveNext
srs1.movenext
srs2.movenext
Loop While Not sRs1.EOF     
dRs.Close
srs1.close
srs2.close
Else
End If
Set drs=Nothing
Set srs1=Nothing
Set srs2=Nothing
sconn.close
dconn.close
Set sconn=Nothing
Set dconn=Nothing
End If 
MsgBox"export success!"

时间判断转换代码:

Function timeconv(begintime,endtime,choice)
'there is still some problems:checktime should return a boolen flag to show whether the number
'is in the range, then in function timeconv shoule check this flag to decide 
'whether Exit funciton Or Not, this function needs to improve!
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'begin time returns the begintime you choose from combobox after some processing
'endtime is as upon
'choice is input choice of what process,0 is no processing and 1 is 
'adjust to (GMT)Greenwich Mean Time 
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Dim byear,bmonth,bday,bhour,bmin,bsec
Dim eyear,emonth,eday,ehour,emin,esec
Dim btime,etime,newbtime,newetime
Dim timeerr
Dim temph,tempd,stemph,stempd

Set byear = ScreenItems("byear")
Set bmonth = ScreenItems("bmonth")
Set bday = ScreenItems("bday")
Set bhour = ScreenItems("bhour")
Set bmin = ScreenItems("bmin")
Set bsec = ScreenItems("bsec")

Set eyear = ScreenItems("eyear")
Set emonth = ScreenItems("emonth")
Set eday = ScreenItems("eday")
Set ehour = ScreenItems("ehour")
Set emin = ScreenItems("emin")
Set esec = ScreenItems("esec")
'check whether entered begintime is legal or valid

If (Not IsNumeric(byear.text))Or (Not IsNumeric(bmonth.text))Or (Not IsNumeric(bday.text))Or _
   (Not IsNumeric(bhour.text))Or (Not IsNumeric(bmin.text))Or (Not IsNumeric(bsec.text)) Then
   MsgBox "some of you entered begintime is not numeric or null!",vbOKCancel,"wrong time"
   timeconv=False
   Exit Function
End If
'check whether time is outof range
Call checktime(byear.text,9999,0,"begin year")
Call checktime(bmonth.text,12,1,"begin month")
Call checktime(bday.text,31,1,"begin day")
Call checktime(bhour.text,23,0,"begin hour")
Call checktime(bmin.text,59,0,"begin minutes")
Call checktime(bsec.text,59,0,"begin second")

'check whether entered endtime is legal or valid
If Not IsNumeric(eyear.text)Or Not IsNumeric(emonth.text)Or Not IsNumeric(eday.text)Or Not _ 
       IsNumeric(ehour.text)Or Not IsNumeric(emin.text)Or Not IsNumeric(esec.text) Then
   MsgBox "some of you entered endtime is not numeric or null!",vbOKCancel,"wrong time"
   timeconv = False
   Exit Function
End If
'check whether time is outof range
Call checktime(eyear.text,9999,0,"end year")
Call checktime(emonth.text,12,1,"end month")
Call checktime(eday.text,31,1,"end day")
Call checktime(ehour.text,23,0,"end hour")
Call checktime(emin.text,59,0,"end minutes")
Call checktime(esec.text,59,0,"end second")

'check whether begin time is later than end time
btime= byear.text & "-" & bmonth.text &"-"& bday.text &" "& bhour.text &":"& bmin.text&":"&bsec.text
etime=eyear.text+"-"+emonth.text+"-"+eday.text+" "+ehour.text+":"+emin.text+":"+esec.text

timeerr = DateDiff("s",btime,etime)
If timeerr<0 Then
MsgBox"you entered wrong time range,begin time later than end time"
timeconv = False
Exit Function
End If
'check whether it is export time or show time,
'chioce=0--show Time,chioce=1---export Time
If choice = 0 Then
begintime = btime
endtime = etime
Else
'time convert to equalize with SQL server
If bhour.text >= 8 Then
    temph=CInt(bhour.text)-8
    tempd=DateDiff("d",0,btime)
Else
   temph=CInt(bhour.text)+16
   tempd=DateDiff("d",1,btime)
End If
   stemph=CStr(temph)
   stempd=CStr(CDate(tempd))

'new time after convert
   newbtime=stempd+" "+stemph+":"+bmin.text+":"+bsec.text
If ehour.text >= 8 Then
    temph=CInt(ehour.text)-8
    tempd=DateDiff("d",0,etime)
Else
   temph=CInt(ehour.text)+16
   tempd=DateDiff("d",1,etime)
End If
   stemph=CStr(temph)
   stempd=CStr(CDate(tempd))
'new time after convert
   newetime=stempd+" "+stemph+":"+emin.text+":"+esec.text
begintime =newbtime
endtime=newetime
End If 'end of chioce 
timeconv = True 
End Function

Sub checktime(input,upper,downer,note)
'check if time is outof ranges
If CInt(input)<downer Or CInt(input)>upper Then
MsgBox"you entered wrong range of " ¬e
Exit Sub
End If
End Sub

使用listview控件显示数据:

dim conn, rs, cmd, pstr,ssql
Dim opdlg,fm, ListView1,trend,item1
Dim m,n,b,s
dim begintime,endtime
b=timeconv(begintime,endtime,0)
'MsgBox CDate(begintime)
if b=false then
exit sub
else 
set conn = createobject("adodb.connection")
set rs = createobject("adodb.recordset")
set cmd = createobject("adodb.command")
Set opdlg = ScreenItems("dialog")
Set trend = ScreenItems("curve")

With opdlg 
.Flags = 4
.Filter = "MDB文件(*.mdb)|*.mdb|AllFiles(*.*)|*.*|"
.FilterIndex = 2
.InitDir = "E:\vb code"
.Action = 1
End With

If opdlg.FileName = "" Then
MsgBox "you must choose a file"
   Exit Sub
Else
fm = opdlg.FileName
End If
'MsgBox begintime
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;"
pstr = pstr & "Data Source=" & fm
conn.connectionstring=pstr
conn.cursorlocation=3
conn.open
ssql="select * from MyTable where cdate(time) between #"&begintime&"# And #"&endtime&"#" 
rs.cursorlocation = 3
rs.open ssql,conn,1,2
m=rs.fields.count
'MsgBox ssql
Set ListView1 = ScreenItems("listview")
ListView1.ColumnHeaders.Clear
ListView1.ColumnHeaders.Add , , CStr(Rs.Fields(0).Name), 100
ListView1.ColumnHeaders.Add , , CStr(Rs.Fields(1).Name), 70
ListView1.ColumnHeaders.Add , , CStr(Rs.Fields(2).Name), 70
If (m > 0) Then
MsgBox "here"
Rs.MoveFirst
n = 0
s=rs.recordcount
MsgBox s
Do While Not Rs.EOF
n = n + 1
s = Left(CStr(Rs.Fields(0).Value), 23)
Set Item1 = ListView1.ListItems.Add()
Item1.Text = Left(CStr(Rs.Fields(0).Value), 23)
Item1.SubItems(1) = FormatNumber(Rs.Fields(1).Value, 1)
Item1.SubItems(2) = FormatNumber(Rs.Fields(2).Value, 1)
trend.index = 0
trend.Datax=CDate(rs.fields(0).value) 
trend.datay= rs.fields(1).value
trend.InsertData = True 
trend.index = 1
trend.Datax=CDate(rs.fields(0).value) 
trend.datay= rs.fields(2).value
trend.InsertData = True

If (n > 1000) Then Exit Do
Rs.MoveNext
Loop
Rs.Close
Else
End If
Set Rs = Nothing
conn.Close
Set conn = Nothing
end if 'end of b=true

使用wincc自带function trend控件显示曲线,使用复选框选择:

显示复选框与关闭
Sub X6309X94AE3X0000X79F0_OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y) 
Dim checkbo
Set checkbo=ScreenItems("checkbox")
checkbo.Visible= Not checkbo.Visible
End Sub
复选框的输入输出域改变时VBS脚本,选择曲线
Sub Process_OnPropertyChanged(ByVal Item, ByVal value)         
Dim curve, checkbo
Dim pp,i
Set curve= ScreenItems("curve")
Set checkbo=ScreenItems("checkbox")
pp=checkbo.Process 
For i=0 To 1
curve.Index=i
If (pp Mod 2)=1 Then
curve.ItemVisible=True 
Else 
curve.ItemVisible=False
End If
pp=Fix(pp/2)
Next

End Sub

提问者对于答案的评价:
谢谢你们的回答!

原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc267136.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2019年6月11日 上午5:03
下一篇 2019年6月11日 上午5:03

相关推荐

  • HELP ME!!安装WINCC

    安装SQL2000时提示a previous program installation created pending …

    SIMATIC WinCC 2019年6月11日
  • wincc自定义对象

    wincc自定义对象怎么使用,有没有视频教程 最佳答案 WINCC 如何有效组态自定义对象?https://support.industry.siemens.com/cs…

    SIMATIC WinCC 2020年11月1日
  • 请教一个显示隐藏触发的问题

    打算用画面窗口做一个弹窗报警,当某设备停机时弹出XX停机,但是在显示隐藏里只有1显示/隐藏或者0显示/隐藏,请问下专家们如何实现1到0时才显示呢,设备运行信号从1到0时 …

    SIMATIC WinCC 2021年7月5日
  • cp1613卡的与STEP7的以太网通讯

    想请教一下.上位机的组态软件是WINCC,用CP1613卡与西门子300 PLC以太网通讯.以太网模块CP343-1.想请教一下CP1613与CP343-1通讯时的整个组…

    SIMATIC WinCC 2019年6月11日
  • WINCC变量计数

    我使用WINCC V6.2 SP2版,在变量管理器输入变量过程中(已经输入了100多个与PLC连接变量),提示“变量的计数超过了软件许可所允许的范围。—–…

    SIMATIC WinCC 2019年6月11日
  • 诊断地址

    WINCC 最佳答案 请说清楚问题,一般都有诊断报警 提问者对于答案的评价:如何在上位机(WINCC)上显示各子站中哪个是通讯是断弦了,

    SIMATIC WinCC 2019年6月11日
  • 请教wincc的问题

    怎样知道一个变量用在哪幅图中? 最佳答案 使用交叉索引,更新数据管理器,在更新后的表中找到该变量名称,双击变量名称就会跳转到该控件。 提问者对于答案的评价:谢谢大家 

    SIMATIC WinCC 2019年6月11日
  • 同时控制用户归档的ID和Job

    我编写了一段代码,要依次写入三次归档到用户归档,代码如下  HMIRuntime.Tags("@UA_T1_ID").Write …

    SIMATIC WinCC 2019年6月11日
  • WINCC7.0 WEB发布授权问题

    之前购买的WINCC订货号为:6AV6 381-2BN07-0AV0我想了解下 这个版本的wincc是否有web发布的授权  是几用户的呢?…

    SIMATIC WinCC 2021年7月5日
  • WINCC程序打包

    求助高手,请问能不能将WinCC设计的人机界面程序,打包成一个可以安装/卸载的可执行文件。这样就不用每次都要先运行WinCC,然后再运行程序了的? 问题补充:谢谢各位,我想问一下R…

    SIMATIC WinCC 2019年6月11日