vb6的ado编写简单的access数据库编程

2024-11-16 13:05:58
推荐回答(1个)
回答1:

'在工程里引用 microsoft activex data objects 2.7 library
'然后随便画个命令控件,在命令控件的单击事件里(click)输入以下代码
'以下是查询记录的代码:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Jet OLEDB:Database Password = 111;Data Source=" & App.Path & "\1.mdb" '1.mdb 替换成你的数据库
cn.Open
rs.Open "select * from 表1 where 查询条件 ", cn, adOpenStatic, adLockOptimistic
do while not rs.eof
'这里编程输出查询结果
rs.MoveNext
loop
rs.Close

'修改记录:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Jet OLEDB:Database Password = 111;Data Source=" & App.Path & "\1.mdb" '1.mdb 替换成你的数据库
cn.Open
rs.Open "select * from 表1 where 查询条件 ", cn, adOpenStatic, adLockOptimistic
do while not rs.eof
rs!字段1=内容
rs!字段2=内容
...
rs.MoveNext
loop
rs.Update
rs.Close

'添加记录:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Jet OLEDB:Database Password = 111;Data Source=" & App.Path & "\1.mdb" '1.mdb 替换成你的数据库
cn.Open
rs.Open "select * from 表1 where 查询条件 ", cn, adOpenStatic, adLockOptimistic
do while not rs.eof
rs.AddNew
rs!字段1=内容
rs!字段2=内容
...
rs.MoveNext
loop
rs.Update
rs.Close