注册 登录
编程论坛 VB6论坛

求助:窗体控件调取数据库中的内容

cunfu2018 发布于 2018-12-12 10:06, 2653 次点击
只有本站会员才能查看附件,请 登录
想编写一个查询数据库内容的小程序,因为初学,有些地方弄不明白了,求助各位大虾.
附件中只能得到字段的第一个值,后面的取不到值,请问如何处理?谢谢

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
7 回复
#2
icecool2018-12-12 10:29
因为你的text1.text的change事件中,查询语句没给条件,所以你的text1改变,RichTextBox1.Text只会一直显示数据库中第一条记录。
改为如下:
Set Rs = MyDB.OpenRecordset("Select * From 数据表 where 名称='" & Text1.Text & "'")
#3
cunfu20182018-12-12 11:16
回复 2楼 icecool
非常感谢!如何给您分值?我还不会.
另外,下面代码中右键单击节点时,弹出菜单Me.PopupMenu mnupop提示:实时错误'424'要求对象.我查阅好多资料也没解决,麻烦您给看看,提点一句.谢谢!
Dim select_id As String
Dim ConnStr As String
Private Sub Command1_Click() '刷新
GetValue
End Sub
Private Sub Form_Load()
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库.mdb;Persist Security Info=False"
TreeView1.LineStyle = tvwRootLines
TreeView1.LabelEdit = tvwManual
GetValue
End Sub
Private Sub mnu_del_Click() '菜单:删除
    DelRecord select_id
End Sub
Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim nd As Node
If Button = vbRightButton Then
    Set nd = TreeView1.HitTest(x, y)
    If Not nd Is Nothing Then
        If Left(nd.Key, 1) = "R" Then
            select_id = nd.Text
            Me.PopupMenu mnupop
        End If
    End If
End If
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
Private Sub GetValue()
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 = ConnStr
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 DelRecord(ByVal id As String)
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 = ConnStr
With Cnn
    .CursorLocation = adUseClient
    .Open
End With
Cnn_ = adCmdText
Cnn_ = "delete from 数据表 where 名称='" & id & "'"
Set Cnn_c.ActiveConnection = Cnn
Set Rs = Cnn_c.Execute '执行删除
Cnn_ = "select * from 数据表"
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
#4
icecool2018-12-12 11:37
这里错误提示要求对像,那么你的窗体中应该没有一个名称为mnupop的菜单,

你得通过菜单编辑器,编辑一个菜单,名称为mnupop,标题为操作,不可见,再设一个下一级菜单,名称mnu_del,标题为删除,可见
#5
cunfu20182018-12-12 11:46
回复 4楼 icecool
谢谢!我再认真着磨着磨
#6
ZHRXJR2018-12-12 11:58
我感到比较简单的一个程序,你是不是搞得有点复杂了,根据你的数据库为写的:
只有本站会员才能查看附件,请 登录

具体看附件:
只有本站会员才能查看附件,请 登录


#7
cunfu20182018-12-12 13:59
回复 4楼 icecool
调试好了,谢谢

[此贴子已经被作者于2018-12-12 14:39编辑过]

#8
cunfu20182018-12-12 14:02
回复 6楼 ZHRXJR
谢谢版主,因为初学,现阶段还处于模仿阶段,代码是学习他人.
如何实现右键弹出菜单,实现添加、删除、修改节点
1