-1

I have problem I write this code for padding combobox but combobox didn't padding

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73

1 Answers1

0

I found this on another website and have used the structure for my needs, but if you edit it to your needs it should work. One thing to point out is that you should call the macro on the UserForm_Intialize event and not a CommandButton Event that way it will repopulate the data from the Database each time the userform loads.

   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
Zack E
  • 696
  • 7
  • 23