标题:求助:窗体控件调取数据库中的内容
只看楼主
cunfu2018
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2018-12-12
结帖率:100%
 问题点数:0 回复次数:7 
求助:窗体控件调取数据库中的内容
求助.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
搜索更多相关主题的帖子: Sub If Dim Set End 
2018-12-12 10:06
icecool
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:20
帖 子:1214
专家分:1366
注 册:2005-3-14
得分:0 
因为你的text1.text的change事件中,查询语句没给条件,所以你的text1改变,RichTextBox1.Text只会一直显示数据库中第一条记录。
改为如下:
Set Rs = MyDB.OpenRecordset("Select * From 数据表 where 名称='" & Text1.Text & "'")

http://toorup.3v.do
loading...
2018-12-12 10:29
cunfu2018
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2018-12-12
得分:0 
回复 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
2018-12-12 11:16
icecool
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:20
帖 子:1214
专家分:1366
注 册:2005-3-14
得分:0 
这里错误提示要求对像,那么你的窗体中应该没有一个名称为mnupop的菜单,

你得通过菜单编辑器,编辑一个菜单,名称为mnupop,标题为操作,不可见,再设一个下一级菜单,名称mnu_del,标题为删除,可见

http://toorup.3v.do
loading...
2018-12-12 11:37
cunfu2018
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2018-12-12
得分:0 
回复 4楼 icecool
谢谢!我再认真着磨着磨
2018-12-12 11:46
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
得分:0 
我感到比较简单的一个程序,你是不是搞得有点复杂了,根据你的数据库为写的:

具体看附件:
求助回复.rar (259.21 KB)



请不要选我!!!
2018-12-12 11:58
cunfu2018
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2018-12-12
得分:0 
回复 4楼 icecool
调试好了,谢谢

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

2018-12-12 13:59
cunfu2018
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2018-12-12
得分:0 
回复 6楼 ZHRXJR
谢谢版主,因为初学,现阶段还处于模仿阶段,代码是学习他人.
如何实现右键弹出菜单,实现添加、删除、修改节点
2018-12-12 14:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-491974-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.146810 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved