Option Explicit
Private Values() As Variant
Private NumPoints As Integer
Private Sub LoadData()
Dim db As ADODB.Connection
Dim rs As ADODB.Recordset
Dim rs1 As ADODB.Recordset
Dim dbname As String
Dim i As Integer
Dim dbfilename As String
Dim ConnectString As String
Dim apppath As String
' 打开数据库
Dim strsql As String
Set db = New ADODB.Connection
If Right(App.Path, 1) = "\" Then
apppath = App.Path
Else
apppath = App.Path & "\"
End If
dbfilename = apppath & "data.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
dbfilename & ";Persist Security Info=False;"
On Error Resume Next
With db
.CursorLocation = adUseClient
.Open ConnectString
End With
' 获得数据库记录
Set rs = New ADODB.Recordset
strsql = " select * from bysj where bysj.date between #" & Textmindate & "# and #" & Textmaxdate & "#"
rs.Open strsql, db, 2, 3
' 查看数据库中记录数
rs.MoveLast
NumPoints = rs.RecordCount
ReDim Values(1 To NumPoints, 1 To 2)
' 加载数据
rs.MoveFirst
For i = 1 To NumPoints
Values(i, 1) = rs!Date
Values(i, 2) = rs!流量
rs.MoveNext
Next i
rs.Close
db.Close
End Sub
Private Sub Comexit_Click()
Unload Me
End Sub
Private Sub Command1_Click()
With MSChart1
.Top = 0
.Left = 0
.Width = Me.ScaleWidth
.Height = Me.ScaleHeight
End With
LoadData
If CDate(Textmaxdate.Text) < CDate(Textmindate.Text) Then
MsgBox "后面的日期应大于前面的!" + Chr(13) + "为了选出您想要的记录请修改过来.", 48, "日期错误"
Textmaxdate.SelStart = 0
Textmaxdate.SelLength = 8
Textmaxdate.SetFocus
Exit Sub
End If
Dim sql
sql = "select * from bysj where date between " + Chr(35) + Textmindate + Chr(35) + " and " + Chr(35) + Textmaxdate + Chr(35) + " order by date"
Adodc1.RecordSource = sql
Adodc1.Refresh
' 使用MSChart控件显示数据
MSChart1.ChartData = Values
MSChart1.RowCount = NumPoints
MSChart1.chartType = VtChChartType2dLine
MSChart1.Visible = True
End Sub
Private Sub Textmindate_LostFocus()
If Not IsDate(Textmindate.Text) Then
Textmindate.SelStart = 0
Textmindate.SelLength = 8
Textmindate.SetFocus
End If
End Sub
Private Sub Textmaxdate_LostFocus()
If Not IsDate(Textmaxdate.Text) Then
Textmaxdate.SelStart = 0
Textmaxdate.SelLength = 8
Textmaxdate.SetFocus
' Exit Sub
End If
End Sub
Public Function getNextValue(ByVal myDate As Date, ByVal n As Integer) As Double
Set rs1 = New ADODB.Recordset
Dim t As Double
myDate = Textmindate
Do While myDate < Textmaxdate
Set rs1 = db.Execute("SELECT TOP " & 3 & " 流量 FROM bysj WHERE date<#" & myDate & "# ORDER BY date DESC")
rs1.MoveFirst
While Not rs1.EOF
t = t + rs.Fields(0).Value
rs1.MoveNext
Wend
Set rs1 = Nothing
getNextValue = t / 2
myDate = DateAdd("n", 5, myDate)
Line (getNextValue, Textmindate)-(getNextValue, myDate), vbGreen
rs1.Close
Line (Textmindate, getNextValue)-(myDate, getNextValue)
Loop
End Function