Wincc ODBC連接ACCESS數(shù)據(jù)庫(kù), MshflexGird控件顯示數(shù)據(jù)
wincc自帶安裝SQL數(shù)據(jù)庫(kù),連接不上,退而求其次只能使用ACCESS數(shù)據(jù)庫(kù)了,好多人說ODBC方法過時(shí)了,但是對(duì)于我們這些對(duì)數(shù)據(jù)查詢量較小,查詢頻率較低的高級(jí)語(yǔ)言初學(xué)者來(lái)說,該方法還是比較實(shí)用。
MshflexGird控件,個(gè)別電腦上可能沒有注冊(cè),可以在網(wǎng)上下載重新注冊(cè),這次嘗試了好多次可以注冊(cè),但是在添加的時(shí)候總是報(bào)錯(cuò),使用較笨的方法安裝了VB6.0,
MshflexGird控件的屬性比如列數(shù),行數(shù),列寬,列頭注釋等等。百度一下有好多文檔介紹
腳本部分:
Dim objConnection
Dim strConnectionString
Dim strSQL
Dim objrs
Dim chaxun
Dim i
Dim j
'定義報(bào)表控件
Dim aa
'chaxun=HMIRuntime.Tags("管號(hào)查詢輸入").Read
'數(shù)據(jù)庫(kù)連接字符串
strConnectionString = "Provider=MSDASQL;DSN=REPORT;UID=;PWD=;"
strSQL = "select * from report order by 編號(hào) ASC;"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objrs = CreateObject("ADODB.RecordSet")
objrs.open strSQL,objConnection
'數(shù)據(jù)循環(huán)寫入MSFlexgrid控件
Set aa = ScreenItems("控件5")
aa.rows = 2
'設(shè)置列頭文字
aa.FormatString = "^序號(hào)^管號(hào)^管徑^管長(zhǎng)^壁厚^材質(zhì)^試壓壓力Mpa^保壓時(shí)間S^試壓日期^結(jié)束時(shí)間^開始時(shí)間^操作員^班次^結(jié)論^標(biāo)準(zhǔn)^規(guī)格^溫度℃"
'設(shè)置列寬度
For j=2 To 14
aa.ColWidth(j)=1500
Next
j=j+1
aa.ColWidth(1)=2000
aa.ColWidth(15)=2000
'寫入數(shù)據(jù)
On Error Resume Next
objrs.moveFirst
Do While Not objrs.eof
For i=0 To 16
aa.TextMatrix(aa.Rows-1,i)=objrs.fields(i).value
Next
aa.Rows=aa.Rows+1
objrs.movenext
Loop
'釋放連接
objrs.close
Set objrs = Nothing
objConnection.Close
Set objConnection = Nothing