求助:窗体控件调取数据库中的内容
求助.rar
(251.6 KB)
想编写一个查询数据库内容的小程序,因为初学,有些地方弄不明白了,求助各位大虾.附件中只能得到字段的第一个值,后面的取不到值,请问如何处理?谢谢
Private Sub Text1_Change()
If Text1.Text <> "" Then
Dim MyWs As Workspace
Dim MyDB As Database
Dim Rs As Recordset
Set MyWs = DBEngine.Workspaces(0)
Set MyDB = MyWs.OpenDatabase(App.Path & "\数据库.mdb")
Set Rs = MyDB.OpenRecordset("Select * From 数据表")
If Rs.Fields("名称") = Text1.Text Then RichTextBox1.Text = Rs.Fields("代码")
Else
RichTextBox1.Text = ""
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim Cnn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Cnn_c As New
Set Cnn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库.mdb;Persist Security Info=False"
With Cnn
.CursorLocation = adUseClient
.Open
End With
Cnn_ = adCmdText
Cnn_ = "select * from 数据表"
Set Cnn_c.ActiveConnection = Cnn
Set Rs = Cnn_c.Execute
TreeView1.Nodes.Clear
If Rs.RecordCount > 0 Then
For i = 1 To Rs.RecordCount
TreeView1.Nodes.Add , , "R" & i, Rs.Fields("名称")
Rs.MoveNext
Next
Rs.Close
End If
Cnn.Close
Set Cnn_c = Nothing
Set Rs = Nothing
Set Cnn = Nothing
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
If Left(Node.Key, 1) = "R" Then
Text1.Text = Node.Text
End If
End Sub