Click here to Skip to main content
15,790,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I want to know about how to populate text box values on combo box in vb access database.

or how to get database values on combo box at form load event.

database values are stored in another table by form 2 and the combo box is placed on 1st form which enters the whole data into database.

also i am trying to make a popup for date reminder on check box button.

Thanks in advance

What I have tried:

here is my code:

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset

Private Sub cmdExit_Click()
End Sub

Private Sub CmdSave_Click()
   Set rs = New ADODB.Recordset
   rs.Fields(CompanyName).Value = TxtCompanyName.Text
End Sub

Private Sub UserForm_Click()
   con.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\mahtab.ali\Documents\NoticeBook.mdb;Persist Security Info=False"
   rs.Open "Select * from CompanyName", con, adOpenDynamic, adLockPessimistic
End Sub

Sub Clear()
   ComboBox1.Value = ""
   TxtCompanyName.Text = ""
End Sub

Sub fillcombo()
   Do Until rs.EOF
      ComboBox1.AddItem rs!CompanyName
End If
End Sub
Updated 27-Jan-17 6:13am
[no name] 26-Jan-17 9:34am    
Without seeing your code and a description of the problem it's not possible for us to guess what "not working" means.
[no name] 26-Jan-17 10:18am    
Okay so now there is code. And a quick look at your code, I seem to recall needing to MoveFirst on a recordset before trying to iterate through it. But without a description of whatever your problem is, that is just a guess. Learning how to use the debugger to debug your code would probably tell you more.
Rebel Spirit86 26-Jan-17 10:23am    
thanks for the reply.

my problem is code is not connecting the combo box with the text box on from 2 and not getting table values. sometime it errors "Type Mismatch" & Compiler error Expected Handler"

I will provide u the whole criteria soon.

j snooze 26-Jan-17 17:35pm    
well I have a hard time picturing what you are trying to accomplish, but why are you trying to fill the combo box on the form click event vs the form load?
Also, on the save click, why are you adding a new recordset? why are you trying to set a new recordset field to the company textbox value? I would think you would be trying to run an update on the table setting it to the new company value selected?? These are all guesses.
Rebel Spirit86 27-Jan-17 8:12am    
yes j snooze friend.

i am updating the main table with collection of company name form combo box on form1 from other table which is saved by the company name in text box on form2. also load the combo box values on form 1.

1 solution

i've found some helpful code and changed it as my requirement and it works. it is showing values in combobox which were updated in new company textbox form.

Private Sub Form_Initialize()
On Error GoTo Form_Initialize_Err
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Database Folder\Notice Book\Notice Book project\database\Notice Book.mdb;Persist Security Info=False"
    rst.Open "SELECT DISTINCT [CompanyName] FROM CompanyDetails ORDER BY [CompanyName];", _
             cnn, adOpenStatic
    With Me.CmbSelectCompany
            .AddItem rst![CompanyName]
        Loop Until rst.EOF
    End With
    On Error Resume Next
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
    MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    Resume Form_Initialize_Exit
End Sub

thanks for ur time and help :)
Share this answer
Rebel Spirit86 30-Jan-17 10:48am    
Friends i've just got the solution but combobox is not refreshing the new added values as the runtime when i close the project and restart it the new added entry found in combobox list.. what to do now?????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900