0% found this document useful (0 votes)
21 views2 pages

As String: Startuppath

The document describes code for populating a combo box from a database. It defines variables to store the database connection string and commands. A dataset is initialized and populated with data from a database table using a data adapter. The combo box's data source, display member, and value member are set to fields from the dataset to display the data in the combo box.

Uploaded by

Romeo Balingao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

As String: Startuppath

The document describes code for populating a combo box from a database. It defines variables to store the database connection string and commands. A dataset is initialized and populated with data from a database table using a data adapter. The combo box's data source, display member, and value member are set to fields from the dataset to display the data in the combo box.

Uploaded by

Romeo Balingao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

'DECLARE A STRING VARIABLE TO STORE THE STRING CONNECTION.

Dim strcon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath &


"\userdb.accdb"
3.
'INITIALIZE THE STRING CONNECTION TO BE OPEN
4.
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(strcon)
5.
'INITIALIZE THE COMMANDS THAT ARE USED TO FILL THE DATASET AND UPDATE THE DATA SOURCE
6.
Dim da As New OleDb.OleDbDataAdapter
7.
'INITIALIZE YOUR DATASET
8.
Dim ds As New DataSet
9.
'DECLARE A STRING VARIABLE TO STORE THE QUERY IN IT
10.
Dim sql As String
1.
2.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.

Public Sub cbofill(ByVal sql As String, ByVal cbo As ComboBox)


Try
'OPENING THE CONNECTION
con.Open()
'SET YOUR COMMANDS AND A DATABASE CONNECTION
' THAT ARE USED TO FILL THE DATASET AND UPDATE
' THE DATA SOURCE.
da = New OleDb.OleDbDataAdapter(sql, con)
'SET A NEW DATASET
ds = New DataSet
'ADDING AND REFRESHING THE ROWS OF YOUR DATATABLE
'(FILLING THE DATATABLE)
da.Fill(ds, "userdb")
With cbo
'SET YOUR DATASOURCE TO GET THE
' COLLECTION OF TABLES CONTAINED
' IN THE DATASET
.DataSource = ds.Tables(0)
'SET THE FIELDS OF THE DATATABLE TO
' BE SHOWN ON THE COMBOBOX
.DisplayMember = "username"
'SET THE ACTUAL VALUE OF THE
' ITEMS IN THE COMBOBOX

25.
26.
27.
28.
29.
30.
31.
32.
33.
1.
2.
3.
4.
5.
6.
7.

.ValueMember = "user_id"
End With
Catch ex As Exception
'CATCH ANY ERRORS
MsgBox(ex.Message)
End Try
'CLOSING THE CONNECTION
con.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'STORE YOUR QUERY IN THE STRING VARIABLE
sql = "SELECT * FROM tbluser"
'CALL YOUR SUB PROCEDURE AND PUT YOUR QUERY IN THE FIRST PARAMETER.
'AND FOR THE SECOND PARAMETER IS THE NAME OF THE COMBOBOX.
cbofill (sql, ComboBox1)
End Sub

You might also like