Populate database record in to combo box
In this tutorial we are going to populate the records from
database access in to combo box object.
Step 1:
First, we are going to create database in ms access. Open
your MS Access > create New blank database then name it as Database.mdb and
click Create button, make sure that it is created or saved with access 2003
format.
Step 2:
Right click your table and select the Design
View.
Step 3:
In this case im going to use the tblCourse as the name of my
table name then press Ok, but you can change it whatever you want.
Step 4:
Now, add a new field name CourseName and set the Data Type
to Text. Then close the table, it will ask you to save the changes and click
Yes.
Step 5:
Double click the table and add the following records. Close
the table then save, then Close ms access.
Step 6:
Open your VB Application, Add a module then type the
following code to establish a database connection.
Option Explicit
Public con As ADODB.Connection
Public rs As ADODB.Recordset
Public Sub ConnectDB()
Set con = New ADODB.Connection
con.CursorLocation = adUseClient
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & App.Path & "\Database.mdb;Persist Security
Info=False"
con.Open
End Sub
Step 7:
Go back to the form and add 1 label & 1 combo box,
change the Font Property to Bold and 14 font size,
Step 8:
Type this code in Form_Load Event
Option Explicit
Private Sub Form_Load()
Module1.ConnectDB
Set rs = New ADODB.Recordset
rs.Open
"Select * from tblCourse", con, adOpenDynamic, adLockOptimistic
Do While
Not rs.EOF
Combo1.AddItem rs!CourseName
rs.MoveNext
Loop
Set rs = Nothing
End Sub
And there you go, the record successfully populated in combo
box
\
Populate database record in to combo box
Reviewed by code-dev
on
12:02 AM
Rating:
No comments: