select 字段1 from 表 查询一列,得到记录集rs然后rs与mshflexgrid绑定set mshflexgrid1.datasource=rs如果不是绑定,需要用程序循环填充一列。
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 = NothingMSFlexGrid1换成MSHFlexGrid1
查询一列,得到记录集rs然后rs与mshflexgrid绑定set mshflexgrid1.datasource=rs如果不是绑定,需要用程序循环填充一列。
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 = NothingMSFlexGrid1换成MSHFlexGrid1