VB连接数据库读取内容到combo中
在数据库db1.mdb(access)中有一张表department,有两个字段:id,name查询语句:selectnameformdepartment怎样在VB中,用...
在数据库db1.mdb(access)中有一张表department,有两个字段:id,name
查询语句:select name form department
怎样在VB中,用代码连接数据库,再将查询到的数据添加到combo中 展开
查询语句:select name form department
怎样在VB中,用代码连接数据库,再将查询到的数据添加到combo中 展开
1个回答
展开全部
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"
conn.CursorLocation = adUseClient
conn.Open db
Rs.Open "select * from aaa", conn, adOpenKeyset, adLockPessimistic
If Rs.EOF = False Then
Do Until Rs.EOF
Combo1.AddItem Rs!seqno, i
Rs.MoveNext
i = i + 1
Loop
End If
Rs.Close: Set Rs = Nothing
exit sub
Err:
MsgBox Err.Number
Unload Me
End Sub
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"
conn.CursorLocation = adUseClient
conn.Open db
Rs.Open "select * from aaa", conn, adOpenKeyset, adLockPessimistic
If Rs.EOF = False Then
Do Until Rs.EOF
Combo1.AddItem Rs!seqno, i
Rs.MoveNext
i = i + 1
Loop
End If
Rs.Close: Set Rs = Nothing
exit sub
Err:
MsgBox Err.Number
Unload Me
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询