Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
Dim objConnection
Dim objCommand
Dim objRecordset
Dim strConnectionString
Dim strSQL,sSQL
Dim kWh,PFC,kW,A1,A2,A3,V1,V2,V3,Line_Name,Meter_ID,Line_ID
Dim strkWh,strkW,strPFC,strA1,strA2,strA3,strV1,strV2,strV3,strName
Dim strMeter_ID,strLine_ID,strDT
Dim i,strDTM
Dim DTPStart,strStart,DTPEnd,strEnd
Dim MyArray()
Redim MyArray(26,240)
Dim rsCount,nRows,nRow,iRow,nCol,nRS
Dim nHTemp,nDTemp,strFDT
Dim olist
On Error Resume Next
strConnectionString = "Provider=MSDASQL;DSN=Energy;UID=sa;PWD=123456;"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.cursorlocation = 3
objConnection.Open
Set DTPStart = ScreenItems("DTPicker")
strFDT=Year(DTPStart.value) & "-" & Month(DTPStart.value)& "-" & Day(DTPStart.value)&" 00:00:00"
strStart=DateAdd("h",-1,strFDT)
strEnd=DateAdd("d",1,strFDT)
sSQL="select meter_ID,DT,kWh from Meter_Info where DT BETWEEN '"& strStart &"' And '"& strEnd &"' ORDER BY Meter_ID, DT"
Msgbox ssql
Set objRecordset = CreateObject("ADODB.Recordset")
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandType =1
objCommand.CommandText = sSQL
Set objRecordset=objCommand.Execute
rsCount=objRecordset.recordcount
nRows =rscount/240
Msgbox rscount&"记录:"&nRows
Set olist = ScreenItems("olist")
For i = 0 To 23
olist.TextMatrix(i+1, 0)=i&" 时"
Next
For nRow=0 To 25
For nCol=1 To 240
MyArray(nRow, nCol)="-"
Next
Next
If (objRecordset.Bof And objRecordset.Eof) Then
MsgBox("没有符合要求的记录")
Else
objRecordset.movefirst
iRow=1
nCol=1
For nRS=0 To rsCount-1
strFDT=objRecordset.Fields(1).Value
nHTemp= Hour(strFDT)
nRow=nHTemp
nDTemp=Day(strFDT)
If nHTemp=Hour(strStart) And nDTemp=Day(strStart) Then
nRow=0
End If
If nHTemp=Hour(strEnd) And nDTemp=Day(strEnd) Then
nRow=25
End If
MyArray(nRow,nCol)=objRecordset.Fields(0).Value
nRow=nRow+1
iRow=iRow+1
If iRow>nRows Then
iRow=1
nCol=nCol+1
End If
If nCol>240 Then
nCol=1
End If
objRecordset.movenext
Next
End If
For nRow=0 To 23
For nCol=1 To 240
oList.TextMatrix(nRow+1, nCol)="-"
Next
Next
For nRow=0 To 24
For nCol=1 To 240
If MyArray(nRow, nCol)<>"-" And MyArray(nRow+1, nCol)<>"-" Then
oList.TextMatrix(nRow+1, nCol)=MyArray(nRow+1,nCol)-MyArray(nRow,nCol)
End If
Next
Next
Set objCommand = Nothing
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing
If Err.Number <> 0 Then
Msgbox("Error="&CStr(Err.Number)&" "&Err.Description)
Err.Clear
End If
End Sub
标签:objRecordset,Dim,Set,nRow,MSFlexGrid,Next,nCol,应用 From: https://www.cnblogs.com/PLCMAN128/p/17978506