注册 登录
编程论坛 VB6论坛

二级联动Combo1取得唯一值后Combo没有成功

clyfd 发布于 2023-09-23 13:31, 823 次点击
' 声明数据库连接对象
Dim conn As ADODB.Connection

Private Sub Form_Load()
    ' 获取连接
    Set conn = GetDBConnection()

    ' 绑定第一个下拉框
    Dim rs1 As New ADODB.Recordset
    rs1.Open "SELECT DISTINCT Field1 FROM myTable", conn, adOpenStatic, adLockReadOnly
    If Not rs1.EOF Then
        Do While Not rs1.EOF
            Combo1.AddItem rs1.Fields("Field1").Value
            rs1.MoveNext
        Loop
    End If
    rs1.Close
   
    ' 关闭记录集对象

    Set rs1 = Nothing
End Sub

Private Sub Combo1_Change()

   ' 获取第一个下拉框的选定值
    Dim selectedValue As String
    selectedValue = Combo1.Text

    ' 清空第二个下拉框
    Combo2.Clear

    ' 创建新的数据库连接和记录集对象
    Dim conn As ADODB.Connection
    Set conn = GetDBConnection()

    Dim rs2 As New ADODB.Recordset

    ' 绑定第二个下拉框
    rs2.Open "SELECT Field2 FROM myTable WHERE Field1='" & selectedValue & "'", conn, adOpenStatic, adLockReadOnly
    If Not rs2.EOF Then
        Do While Not rs2.EOF
            Combo2.AddItem rs2.Fields("Field2").Value
            rs2.MoveNext
        Loop
    End If
    rs2.Close

    ' 关闭记录集对象和数据库连接
    Set rs2 = Nothing
    conn.Close
    Set conn = Nothing
End Sub

请老师帮忙看看是 什么原因呢
只有本站会员才能查看附件,请 登录
2 回复
#2
cwa99582023-09-25 09:15
这个。。。。。
要用Private Sub Combo1_Click()事件
#3
clyfd2023-09-26 19:48
回复 2楼 cwa9958
哎呀  哎呀,这错误犯的,谢谢老师的指导
1