Private Sub Button7_Click()
On Error Resume Next
Set conn = GetConnection
Set rs = New ADODB.Recordset
If optWeight1.Value = True Then
rs.Open "SELECT weight1.Tarikh, weight1.Sampel1, weight1.Sampel2, weight1.Sampel3, weight1.Sampel4, weight1.Sampel5 FROM weight1 WHERE Tarikh between # " & DTPicker1.Value & " # and # " & DTPicker2.Value & " # ", conn, adOpenStatic, adLockOptimistic
With rs
If Not .EOF Then
'show data from database
FG_ShowRecordset MSFlexGrid7, rs
'add column
MSFlexGrid7.Cols = MSFlexGrid7.Cols + 1
MSFlexGrid7.TextMatrix(0, 6) = "Purata"
MSFlexGrid7.Cols = MSFlexGrid7.Cols + 1
MSFlexGrid7.TextMatrix(0, 7) = "Julat"
'mean
Dim x
For x = 1 To MSFlexGrid7.Rows
MSFlexGrid7.TextMatrix(x, 6) = (MSFlexGrid7.TextMatrix(x, 1) + MSFlexGrid7.TextMatrix(x, 2) + MSFlexGrid7.TextMatrix(x, 3) + MSFlexGrid7.TextMatrix(x, 4) + MSFlexGrid7.TextMatrix(x, 5)) / 5
Next
End If
.Close
End With
conn.Close
Set conn = Nothing
Set rs = Nothing
Set conn = Nothing
Set rs = Nothing
End If
End Sub
'==================================================================================
' function
'
'by : FlyGuy @ [url]www.visualbasicforum.com[/url]
'==================================================================================
Private Sub FG_ShowRecordset(myFG As MSFlexGrid, myRST As ADODB.Recordset)
Dim iField As Integer, iNofFields As Integer
Dim lRow As Long
Screen.MousePointer = vbHourglass
With myRST
.MoveFirst
iNofFields = .Fields.Count
End With
With myFG
.Redraw = False
.AllowUserResizing = flexResizeColumns
.ScrollTrack = True
.FixedCols = 0
.FixedRows = 0
.Cols = iNofFields
.Rows = 1
' setup the header
For iField = 0 To iNofFields - 1
.TextMatrix(0, iField) = myRST.Fields(iField).Name
Next iField
End With
With myRST
Do
' increase the number of rows
lRow = myFG.Rows
myFG.Rows = myFG.Rows + 1
' add the values to the current row
For iField = 0 To iNofFields - 1
If Not IsNull(.Fields(iField).Value) Then
myFG.TextMatrix(lRow, iField) = .Fields(iField).Value
End If
Next iField
' proceed to the next record
.MoveNext
Loop Until .EOF
End With
With myFG
.FixedRows = 1
.Redraw = True
End With
Screen.MousePointer = vbNormal
End Sub