4个回答
展开全部
如果想要对数据库进行查找操作,那么你需要先定义数据库字符串,和一个记录集对像
Public conn As ADODB.Connection ‘数据库对象
Public xs As ADODB.Recordset '记录集对象用来存放查找结果
Sub Connect() '连接到DB1数据库
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
conn.Open
End Sub
如果有一个数据库db1.mdb它里面有一个表
学生(姓名,学号)
现在想要查找学号为10的学生记录。
public sub cazhao()
dim sql as string
sql="select * from 学生 where 学号=10"
set xs =new ADODB.Recordset
xs.open sql,conn,2,2
end sub
执行完cazhao过程后,xs里面就是查找的结果了。比如把 xs的值赋给文本框:
text1.text=xs("姓名")
text2.text=xs("学号")
如果xs中有多条记录可以通过这样的方式遍历:
do while not xs.eof
想要赋值的对象=xs("字段名")
.
.
.
xs.movenext
loop
Public conn As ADODB.Connection ‘数据库对象
Public xs As ADODB.Recordset '记录集对象用来存放查找结果
Sub Connect() '连接到DB1数据库
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
conn.Open
End Sub
如果有一个数据库db1.mdb它里面有一个表
学生(姓名,学号)
现在想要查找学号为10的学生记录。
public sub cazhao()
dim sql as string
sql="select * from 学生 where 学号=10"
set xs =new ADODB.Recordset
xs.open sql,conn,2,2
end sub
执行完cazhao过程后,xs里面就是查找的结果了。比如把 xs的值赋给文本框:
text1.text=xs("姓名")
text2.text=xs("学号")
如果xs中有多条记录可以通过这样的方式遍历:
do while not xs.eof
想要赋值的对象=xs("字段名")
.
.
.
xs.movenext
loop
展开全部
'程序引用ado 画面添加combo1
Option Explicit
Public appdisk As String
Public conn As New ADODB.Connection
Public Rs As New ADODB.Recordset
Public db As String
Private sSQL As String
Private Sub Form_Load()
Dim i As Integer
On Error GoTo Err
appdisk = Trim(App.Path)
If Right(appdisk, 1) <> "\" Then appdisk = appdisk & "\"
db = appdisk
db = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & db & "alex.mdb"
''' db = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Text2.Text & ""
conn.CursorLocation = adUseClient
conn.Open db
i = 0
Set Rs = conn.OpenSchema(adSchemaTables)
'SQL Server:
'select * from sysobjects where type='U' and status>0
If Rs.EOF = False Then
Do Until Rs.EOF
If Left(Rs!TABLE_NAME, 4) <> "MSys" Then
Combo2.AddItem Rs!TABLE_NAME, i
i = i + 1
End If
Rs.MoveNext
Loop
End If
Rs.Close: Set Rs = Nothing
Exit Sub
Err:
MsgBox Err.Number
Unload Me
End Sub
Option Explicit
Public appdisk As String
Public conn As New ADODB.Connection
Public Rs As New ADODB.Recordset
Public db As String
Private sSQL As String
Private Sub Form_Load()
Dim i As Integer
On Error GoTo Err
appdisk = Trim(App.Path)
If Right(appdisk, 1) <> "\" Then appdisk = appdisk & "\"
db = appdisk
db = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & db & "alex.mdb"
''' db = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Text2.Text & ""
conn.CursorLocation = adUseClient
conn.Open db
i = 0
Set Rs = conn.OpenSchema(adSchemaTables)
'SQL Server:
'select * from sysobjects where type='U' and status>0
If Rs.EOF = False Then
Do Until Rs.EOF
If Left(Rs!TABLE_NAME, 4) <> "MSys" Then
Combo2.AddItem Rs!TABLE_NAME, i
i = i + 1
End If
Rs.MoveNext
Loop
End If
Rs.Close: Set Rs = Nothing
Exit Sub
Err:
MsgBox Err.Number
Unload Me
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用DAO3.6访问mdb数据库:
Dim db As Database
Dim rs As Recordset
Set db = OpenDatabase("g:\NWIND.MDB")
For i = 0 To db.TableDefs.Count - 1
'输出各数据表名
Print db.TableDefs(i).Name
Next i
db.Close
Set db = Nothing
Dim db As Database
Dim rs As Recordset
Set db = OpenDatabase("g:\NWIND.MDB")
For i = 0 To db.TableDefs.Count - 1
'输出各数据表名
Print db.TableDefs(i).Name
Next i
db.Close
Set db = Nothing
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把要实现的说明白些
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询