组合框在vba?

问题描述 投票:-1回答:1

我有问题我写这个代码填充组合框,但组合框没有填充

Private Sub refArt_Enter()
    refArt.Clear
    refArt.AddItem ""
      Dim conn As ADODB.Connection
      Dim rs As ADODB.Recordset
      Dim rq As String
      Set conn = getConn()
      Set rs = New ADODB.Recordset
      rq = "SELECT ref FROM tblArticles;"
      rs.Open rq, conn, adOpenDynamic, adLockBatchOptimistic
      Do While Not rs.EOF
          refArt.AddItem rs("ref").value
          rs.MoveNext
      Loop
End Sub
excel vba ms-access access-vba
1个回答
0
投票

我在另一个网站上发现了这个,并根据我的需要使用了该结构,但是如果你根据自己的需要进行编辑,它应该可以工作。需要指出的一点是,您应该在UserForm_Intialize事件上调用宏而不是CommandButton事件,这样每次加载用户窗体时它都会重新填充数据库中的数据。

   Private Sub UserForm_Initialize()
        On Error GoTo UserForm_Initialize_Err
        Dim cnn As New ADODB.Connection
        Dim rst As New ADODB.Recordset
        cnn.Open "Set Connection String Here"
        rst.Open "SELECT ref FROM tblArticles"
        rst.MoveFirst
        With Me.ComboBox1
            .Clear
            Do
                .AddItem rst![ref]
                rst.MoveNext
            Loop Until rst.EOF
        End With
UserForm_Initialize_Exit:
        On Error Resume Next
        rst.Close
        cnn.Close
        Set rst = Nothing
        Set cnn = Nothing
        Exit Sub
UserForm_Initialize_Err:
        MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
        Resume UserForm_Initialize_Exit
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.