vb 连接数据例子

前端之家收集整理的这篇文章主要介绍了vb 连接数据例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

'工程-->引用 添加 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

原文链接:https://www.f2er.com/vb/262008.html

猜你在找的VB相关文章