Hi. I'm having a problem to pull data from MS Access to my combobox. I know how to search one data using another data (e.g: using IC num to search name) but this time I just want to pull the data (e.g: name) that has several rows to my combobox. I think the method is the same but don't know where to change it.

Then when I added new names to the combobox, want to save it to my database, can each name be saved in different rows? I added the names to the combobox 1st, after that then I saved it.

This is my idea and I don't know it can be done or not. Here's my codes:

SEARCH CODE

Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\~LaiLi~\My Documents\Visual Studio 2005\Projects\MoreWever\MoreWever\bin\Debug\Inventory.mdb"
        sql = "SELECT * FROM Profile WHERE StudID = '" & StudIDTextBox.Text & "'"
        con.Open()
        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader

        cmd = New OleDbCommand(sql, con)
        dr = cmd.ExecuteReader

        While dr.Read()
            StudNameTextBox.Text = dr("StudName")
        End While

        con.Close()
End Sub

ADD TO COMBOBOX CODE

With StudNameComboBox
            If .Text <> "" Then
                Dim ItemFoundBoolean As Boolean
                Dim ItemIndexInteger As Integer

                Do Until ItemFoundBoolean Or ItemIndexInteger = .Items.Count
                    If .Text = .Items(ItemIndexInteger).ToString() Then
                        ItemFoundBoolean = True
                        Exit Do
                    Else
                        ItemIndexInteger += 1
                    End If
                Loop
                If ItemFoundBoolean Then
                    MessageBox.Show("Duplicate item.", "Add failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Else
                    .Items.Add(.Text)
                    .Text = ""

                End If
            Else
                MessageBox.Show("Enter a new name.", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End With

ADD TO DATABASE CODE

con.ConnectionString = "PROVIDER=Microsoft.Jet.Oledb.4.0; Data source =C:\Documents and Settings\~LaiLi~\My Documents\Visual Studio 2005\Projects\MoreWever\MoreWever\bin\Debug\Inventory.mdb"
        sql = "SELECT * FROM Profile"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Profile")
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsnewrow As DataRow

        dsnewrow = ds.Tables("Profile").NewRow()

        dsnewrow.Item("StudName") = StudNameTextBox.Text
        dsnewrow.Item("StudID") = StudIDTextBox.Text

        ds.Tables("Profile").Rows.Add(dsnewrow)
        da.Update(ds, "Profile")

Also, if I want to add/pull into listbox, is the coding still the same?
Thank you.

Recommended Answers

All 2 Replies

In order to add names from the database to the combobox you can more or less use the same code as for the search. Observe the change in the SQL string.

Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\~LaiLi~\My Documents\Visual Studio 2005\Projects\MoreWever\MoreWever\bin\Debug\Inventory.mdb"
        sql = "SELECT * FROM Profile"
        con.Open()
        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader

        cmd = New OleDbCommand(sql, con)
        dr = cmd.ExecuteReader

        While dr.Read()
            If Not StudNameComboBox.Items.Contains(dr("StudName")) Then
               StudNameComboBox.Items.Add(dr("StudName"))
            End If
        End While

        dr.Close()
        con.Close()

The IF NOT statement makes sure that no duplicate names are added.

That is really easy on the databound settings of the combobox, sometimes you dont need code for that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.