'工程-->引用 添加 Microsoft activex data objects 2.5 library
'工程-->部件 添加 Microsoft flexgrid control 6.0
Dim Cnn As New ADODB.Connection
Cnn.ConnectionString = "Provider=microsoft.jet.oledb.4.0;data source=E:/Access DB/Database1.mdb;"
If Cnn.State <> ADODB.ObjectStateEnum.adStateClosed Then Cnn.Close
Cnn.Open
Dim Rs As ADODB.Recordset
Set Rs = New ADODB.Recordset
With Rs
Set .ActiveConnection = Cnn
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT good_code,customer,sum(price*amount)/sum(amount) AS avg_price FROM sales GROUP BY good_code,customer"
If .RecordCount > 0 Then
MSFlexGrid1.Clear
MSFlexGrid1.Cols = 3
MSFlexGrid1.Rows = .RecordCount + 1
MSFlexGrid1.TextMatrix(0,0) = "商品编号"
MSFlexGrid1.TextMatrix(0,1) = "顾客名称"
MSFlexGrid1.TextMatrix(0,2) = "平均价格"
.MoveLast: .MoveFirst
For i = 1 To .RecordCount
MSFlexGrid1.TextMatrix(i,0) = .Fields(0).Value & vbNullString
MSFlexGrid1.TextMatrix(i,1) = .Fields(1).Value & vbNullString
MSFlexGrid1.TextMatrix(i,2) = .Fields(2).Value & vbNullString
.MoveNext
Next
End If
.Close
End With
Set Rs = Nothing