VB6 red write DB using Microsoft DAO 3.6 Object Library

' -----------------------------read db
Private Sub Form_Load()
'MsgBox App.Path & "\wgscd.mdb"
Dim tblCountry As Recordset
Dim dbSys As Database
Dim s As String
Set dbSys = OpenDatabase(App.Path + "\wgscd.mdb", False)
Set tblCountry = dbSys.OpenRecordset("Students")' OR Use like : dbSys.OpenRecordset("select * from Students")
With tblCountry
.MoveFirst
Do While Not .EOF
s = s & !sid & ";" & !Name & " score:" & .Fields("score") 'note:"!sid" is present as .Fields("sid")
.MoveNext
Loop

End With
MsgBox s

End Sub

'--------------write db-----------------------------
Private Sub Command1_Click()

'MsgBox App.Path & "\wgscd.mdb"
Dim tblCountry As Recordset
Dim dbSys As Database
Dim s As String
Set dbSys = OpenDatabase(App.Path + "\wgscd.mdb", False)
Set tblCountry = dbSys.OpenRecordset("Students")
With tblCountry
.AddNew
!sid = 5 + rnd(500)
!Name = "bb" & Now
.Fields("score") = rnd(100)
.Update
.MoveFirst
End With

 MsgBox tblCountry.RecordCount

End Sub


bind data with TDBGrid (Library TrueDBGrid80 C:\WINDOWS\system32\tdbg8.oca ComponentOne True DBGrid Pro 8.0)

step:
1.Drag a Data control to the form with named "Data1"
2.Drag a TDBGrid control to the form with named "TDBGrid1"
2.Set control "TDBGrid1"'s property "DataSource" with value "Data1"
3.hen code :
Dim sysDb As Database
Private Sub Command1_Click()
Dim rsData As Recordset
Dim sPath As String
Dim rs As Recordset
sPath = App.Path + "/testDB.mdb"
Set sysDb = OpenDatabase(sPath, 3)
Set rsData = sysDb.OpenRecordset("select * from students")
Set rs = rsData.Clone
Set Data1.Recordset = rs 'must use set
TDBGrid1.Columns("id").BackColor = vbRed
'Me.TDBGrid1.ReBind

End Sub

refer link:
Database.OpenRecordset Method (DAO) link:https://msdn.microsoft.com/en-us/library/office/ff820966.aspx

转载于:https://www.cnblogs.com/wgscd/p/5176887.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值