发表于:2007-09-06 19:17:00
25楼
我的例子程序,自己看着办.一直在运行的东西
Dim StrBtime As String
Dim StrEtime As String
Dim errtime
Const UNIT = 0.03937 * 1440
Private Sub Command1_Click()
Call TimeConv
'**********************************************查询和显示数据*********************************************
If errtime > 0 Then
Dim connstr, myconn, mysql
connstr = "provider=winccoledbprovider.1;data source=.\wincc;catalog=cc_database_06_03_11_17_11_58R;user id=DBA,password=SQL;"
'*****************************************************************************************************
mysql = "TAG:R,'DATABASE\produ','" + StrBtime + "','" + StrEtime + "'"
mysql2 = "TAG:R,'DATABASE\tempu','" + StrBtime + "','" + StrEtime + "'"
mysql3 = "TAG:R,'DATABASE\upq','" + StrBtime + "','" + StrEtime + "'"
'mysql4 = "TAG:R,'DATABASE\prodd','" + mytime1 + "','" + mytime2 + "'"
'mysql5 = "TAG:R,'DATABASE\tempd','" + mytime1 + "','" + mytime2 + "'"
'mysql6 = "TAG:R,'DATABASE\dnq','" + mytime1 + "','" + mytime2 + "'"
Set myconn = CreateObject("adodb.connection")
myconn.ConnectionString = connstr
myconn.CursorLocation = 3
myconn.Open
If GetConState(myconn.State) = "adStateClosed" Then
MsgBox "数据库连接不成功,可能是WINCC生产监控系统程序未激活,请先激活监控系统", vbOKCancel, "数据库连接错误"
myconn.Close
Exit Sub
End If
Set ors = CreateObject("adodb.recordset")
Set orstemp = CreateObject("adodb.recordset")
Set orsupq = CreateObject("adodb.recordset")
Set ocom = CreateObject("adodb.command")
ocom.CommandType = 1
Set ocom.ActiveConnection = myconn
ocom.CommandText = mysql
Set ors = ocom.Execute
m = ors.Fields.Count
k = ors.RecordCount
MSHFlexGrid1.rows = k + 2
Set ocom2 = CreateObject("adodb.command")
ocom2.CommandType = 1
Set ocom2.ActiveConnection = myconn
ocom2.CommandText = mysql2
Set orstemp = ocom2.Execute
Set ocom3 = CreateObject("adodb.command")
ocom3.CommandType = 1
Set ocom3.ActiveConnection = myconn
ocom3.CommandText = mysql3
Set orsupq = ocom3.Execute
If (m > 0) Then
ors.Move 0
orstemp.Move 0
orsupq.Move 0
n = 1
upq = 0
pctime = DateAdd("h", 8, ors.Fields(1).Value)
Do While Not ors.EOF
pctime2 = DateAdd("h", 8, ors.Fields(1).Value) '修改显示记录的时间(格林威治时间)为本机时区
MSHFlexGrid1.TextMatrix(n, 0) = pctime2
MSHFlexGrid1.TextMatrix(n, 1) = ors.Fields(2).Value
MSHFlexGrid1.TextMatrix(n, 2) = orstemp.Fields(2).Value
MSHFlexGrid1.TextMatrix(n, 3) = orsupq.Fields(2).Value
n = n + 1
If (n > 10000) Then Exit Do
If (orsupq.Fields(2).Value = 10) Then
upq = upq + 1
End If
ors.MoveNext
orstemp.MoveNext
orsupq.MoveNext
Loop
percentok = FormatNumber((upq / k * 100), 1)
prodnum(0).Text = k
prodok(1).Text = upq
prodper(0).Text = percentok
End If
ors.Close
Set ors = Nothing
orstemp.Close
Set orstemp = Nothing
orsupq.Close
Set orsupq = Nothing
'**********************************************以上为读取序号的程序***************************
myconn.Close
Set myconn = Nothing
Else
MsgBox "输入的时间不正确"
End If
End Sub
Private Sub Command2_Click()
'Call TimeConv
'MsgBox "查询的时间段为" & StrBtime & "到" & StrEtime
Dim rpt As New Report
Dim txt As clsText
rpt.SetPrinter 28000, 12000, Portrait
rpt.SetMargin 5 * UNIT, 10 * UNIT, 10 * UNIT, 10 * UNIT
rpt.AttachFlexGrid Me.MSHFlexGrid1
rpt.LeftSection.AlignMode = tyContent
rpt.RightSection.AlignMode = tyContent
rpt.Align = tymiddle
rpt.ReadTemplate App.Path & "\test.txt"
rpt.Preview
End Sub
Private Sub Form_Load()
Call AutoValue
Call MsflexGird1Init
End Sub
'********************************生成组合框的内容*******************************************
Public Sub AutoValue()
For i = 2006 To 2010
Byear.Item(0).AddItem (i)
Next
For i = 0 To 12
Bmon.Item(10).AddItem (i)
Next
For i = 0 To 31
Bday.Item(9).AddItem (i)
Next
For i = 0 To 23
Bhour.Item(8).AddItem (i)
Next
For i = 0 To 59
Bmin.Item(7).AddItem (i)
Next
For i = 0 To 59
Bsec.Item(6).AddItem (i)
Next
For i = 2006 To 2010
Eyear.Item(1).AddItem (i)
Next
For i = 0 To 12
Emon.Item(11).AddItem (i)
Next
For i = 0 To 31
Eday.Item(5).AddItem (i)
Next
For i = 0 To 23
Ehour.Item(4).AddItem (i)
Next
For i = 0 To 59
Emin.Item(3).AddItem (i)
Next
For i = 0 To 59
Esec.Item(2).AddItem (i)
Next
End Sub
'**********************转换本地时区为格林威治时间*******************************8
Public Sub TimeConv()
testby = IsNumeric(Byear.Item(0).Text)
If Not testby Then
MsgBox "你输入的年份不是数字型(起始时间段),请重新输入", vbOKCancel, "错误的起始时间"
Byear.Item(0).SetFocus
Exit Sub
End If
Byeartext = Byear.Item(0).Text
testbm = IsNumeric(Bmon.Item(10).Text)
If Not testbm Then
MsgBox "你输入的月份不是数字型(起始时间段),请重新输入", vbOKCancel, "错误的起始时间"
Bmon.Item(10).SetFocus
Exit Sub
End If
BmonText = Bmon.Item(10).Text
testbd = IsNumeric(Bday.Item(9).Text)
If Not testbd Then
MsgBox "你输入的日期不是数字型(起始时间段),请重新输入", vbOKCancel, "错误的起始时间"
Bday.Item(9).SetFocus
Exit Sub
End If
BdayText = Bday.Item(9).Text
testbh = IsNumeric(Bhour.Item(8).Text)
If Not testbh Then
MsgBox "你输入的小时数不是数字型(起始时间段),请重新输入", vbOKCancel, "错误的起始时间"
Bhour.Item(8).SetFocus
Exit Sub
End If
BhourText = Bhour.Item(8).Text
testbm = IsNumeric(Bmin.Item(7).Text)
If Not testbm Then
MsgBox "你输入的分钟数不是数字型(起始时间段),请重新输入", vbOKCancel, "错误的起始时间"
Bmin.Item(7).SetFocus
Exit Sub
End If
BminText = Bmin.Item(7).Text
testbs = IsNumeric(Bsec.Item(6).Text)
If Not testbs Then
MsgBox "你输入的秒数不是数字型(起始时间段),请重新输入", vbOKCancel, "错误的起始时间"
Bsec.Item(6).SetFocus
Exit Sub
End If
BsecText = Bsec.Item(6).Text
testey = IsNumeric(Eyear.Item(1).Text)
If Not testey Then
MsgBox "你输入的年份不是数字型(终止时间段),请重新输入", vbOKCancel, "错误的终止时间"
Eyear.Item(1).SetFocus
Exit Sub
End If
EyearText = Eyear.Item(1).Text
testem = IsNumeric(Emon.Item(11).Text)
If Not testem Then
MsgBox "你输入的月份不是数字型(终止时间段),请重新输入", vbOKCancel, "错误的终止时间"
Emon.Item(11).SetFocus
Exit Sub
End If
EmonText = Emon.Item(11).Text
tested = IsNumeric(Eday.Item(5).Text)
If Not tested Then
MsgBox "你输入的日期不是数字型(终止时间段),请重新输入", vbOKCancel, "错误的起始时间"
Eday.Item(5).SetFocus
Exit Sub
End If
EdayText = Eday.Item(5).Text
testeh = IsNumeric(Ehour.Item(4).Text)
If Not testeh Then
MsgBox "你输入的小时数不是数字型(终止时间段),请重新输入", vbOKCancel, "错误的终止时间"
Ehour.Item(4).SetFocus
Exit Sub
End If
EhourText = Ehour.Item(4).Text
testemi = IsNumeric(Emin.Item(3).Text)
If Not testemi Then
MsgBox "你输入的分钟数不是数字型(终止时间段),请重新输入", vbOKCancel, "错误的终止时间"
Emin.Item(3).SetFocus
Exit Sub
End If
EminText = Emin.Item(3).Text
testes = IsNumeric(Esec.Item(2).Text)
If Not testes Then
MsgBox "你输入的秒数不是数字型(终止时间段),请重新输入", vbOKCancel, "错误的终止时间"
Esec.Item(2).SetFocus
Exit Sub
End If
EsecText = Esec.Item(2).Text
BeginDatetime = Byeartext + "-" + BmonText + "-" + BdayText + " " + BhourText + ":" + BminText + ":" + BsecText
EndDatetime = EyearText + "-" + EmonText + "-" + EdayText + " " + EhourText + ":" + EminText + ":" + EsecText
errtime = DateDiff("s", BeginDatetime, EndDatetime)
If errtime <= 0 Then
MsgBox "查询的时间段不正确:终止时间小于起始时间", vbOKCancel, "错误的时间差"
End If
If BhourText >= 8 Then
Subhour = CInt(BhourText) - 8
BeginNewday = DateDiff("d", 0, BeginDatetime)
Else
Subhour = CInt(BhourText) + 16
BeginNewday = DateDiff("d", 1, BeginDatetime)
End If
Strsubhour = CStr(Subhour) '根据时间是否大于等于8决定日期是否-1,做为新的起始时间
TbeginNewday = CDate(BeginNewday) '根据时间是否大于等于8决定日期是否-1,做为新的起始日期
SbeginNewday = CStr(TbeginNewday) 'convert datetime to char array
Newbtime = Strsubhour + ":" + BminText + ":" + BsecText
Newdatetimeb = SbeginNewday + " " + Newbtime
StrBtime = CStr(Newdatetimeb) '得到校正的格林威治时间(开始查询时间)
If EhourText >= 8 Then
SubhourE = CInt(EhourText) - 8
EndNewday = DateDiff("d", 0, EndDatetime)
Else
SubhourE = CInt(EhourText) + 16
EndNewday = DateDiff("d", 1, EndDatetime)
End If
StrsubhourE = CStr(SubhourE) '根据时间是否大于等于8决定日期是否-1,做为新的起始时间
TendNewday = CDate(EndNewday) '根据时间是否大于等于8决定日期是否-1,做为新的起始日期
SendNewday = CStr(TendNewday) 'convert datetime to char array
NewbtimeE = StrsubhourE + ":" + EminText + ":" + EsecText
NewdatetimeE = SendNewday + " " + NewbtimeE
StrEtime = CStr(NewdatetimeE) '得到校正的格林威治时间(开始查询时间)
End Sub
Public Sub MsflexGird1Init()
MSHFlexGrid1.rows = 40
MSHFlexGrid1.Cols = 6
MSHFlexGrid1.FixedCols = 1
MSHFlexGrid1.FixedRows = 1
MSHFlexGrid1.MergeCells = flexMergeFree
MSHFlexGrid1.ColWidth(0, 0) = 2500
MSHFlexGrid1.TextMatrix(0, 0) = "TIME"
MSHFlexGrid1.TextMatrix(0, 1) = "NUM"
MSHFlexGrid1.TextMatrix(0, 2) = "大底温度"
MSHFlexGrid1.TextMatrix(0, 3) = "大底品质"
End Sub
Public Function GetConState(intState As Integer) As String
Select Case intState
Case 0
GetConState = "adstateclosed"
Case 1
GetConState = "adstateopen"
End Select
End Function