Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Text in a Combo 1

Status
Not open for further replies.

smil3y

IS-IT--Management
Jan 8, 2004
79
0
0
AU
I am loading a combo box with data from an access database. I need for the user to be able to add and edit what ever is in the combo.

As soon as I edit the text the index goes to -1. Have attached my code.

Is there an easy way to do this.
*******************************************
Code:
Private Sub UpdateDescriptioncbo()
        Dim iIndex As Integer
        'clear contents before reloading
        cboSupport.Items.Clear()
        dS.Clear()
        'get data
        Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\FuelLog.mdb"
        Con.Open()

        sql = "SELECT * FROM tblsupport WHERE tblSupport.RigName = '" & cboEditRig.Text & "'"
        dA = New OleDb.OleDbDataAdapter(sql, Con)
        dA.Fill(dS, "Support")
        Con.Close()

        Rows = dS.Tables("Support").Rows.Count  'get number of records
        inc = -1                                'set inc as no records

        If inc <> Rows - 1 Then
            cboSupport.Enabled = True           'there are more than zero rows
            inc = Rows - 1                      'inc now equals the number of rows
        End If


        'cboSupport.Items.Clear()

        If (Rows < 1) Then
            cboSupport.Items.Add("<No Entries>") 'there are no rows - tell the user
            cboSupport.Enabled = False
        Else
            For iIndex = 0 To Rows - 1          'add the data to the list
                cboSupport.Items.Add(dS.Tables("Support").Rows(iIndex).Item(2))
                cboSupport.Items.Add("Support")

                Debug.Print(dS.Tables("Support").Rows(iIndex).Item(2))
            Next
        End If

        cboSupport.SelectedIndex = 0

        iCurrentState2 = CurrentState.NOW_IDLE

        Call UpdateButtons()


    End Sub



    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim indx As Integer
        Dim sMessage As String
        Dim cB As New OleDb.OleDbCommandBuilder(dA)
        Dim dsNewRow As DataRow

       ' Debug.Print(cboSupport.SelectedIndex)


        If (Len(cboSupport.Text) < 1) Then                  'Need to ensure data is entered
            sMessage = "Please enter a support vehicle name in "
            sMessage = sMessage & "the combo box."
            indx = MsgBox(sMessage, vbOKOnly + vbInformation, "Error")
            Exit Sub
        End If


        If (iCurrentState2 <> CurrentState.NOW_ADDING) Then 'We are EDITING data
            indx = MsgBox("Change all entries for '" & _
              sCurrentDescription & "' to '" & cboSupport.Text & _
              "'?", vbYesNo + vbQuestion, "Editing")
            If cboSupport.Text <> "" Then

                dS.Tables("Support").Rows(cboSupport.SelectedIndex).Item(3) = cboSupport.Text
            End If
        Else                                                'We are ADDING data
            indx = MsgBox("Add new entry: " & cboSupport.Text, _
              vbYesNo + vbQuestion, "Adding")
            dsNewRow = dS.Tables("Support").NewRow()
            dsNewRow.Item(1) = cboEditRig.Text
            dsNewRow.Item(2) = cboSupport.Text
            dS.Tables("Support").Rows.Add(dsNewRow)

            dA.Update(dS, "Support")
            MsgBox("New Record added to Database")

        End If

End Sub
 
Those two pieces of code don't seemed to be linked to each other. Which one is causing your problem?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
The first section is just to indicate how I am loading dataset.

My problem is within the save routine - loading the combobox works fine. I can add to the items in the combo list or edit the items in the list.

If I edit I cannot work out how to save the edited item

Regards
 
You need to run an update query on your database to save the changes. Doesn't look like your updating anything, just saving a new row.
 
When I try to change the text in the combo box the index goes to -1 I cannot even save the editted text to the dataset.
 
I would really do it the other way around. Send the change to the database then totally repopulate the ComboBox from the database. While you can edit a ComboBox that wasn't really the idea behind it. It was meant as away to give users either a prompt at selections or to force from a select list of choices. It is just much easier to Clear the items and totally repopulate it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks. I am trying to change from VB6 to .NET and find things very different. My application does require the user to pick from a list of choices, but there is also an option to edit the items in the list.

I thought using a combo and allowing the user to edit and then save would be easy.

What other way would make it easy.
 
As you already have it setup like I said I would find the item they edit in the database change it there and then just repopulate the combox box.

What would be my first thought on allowing to edit a list? If I wanted to give a user a list of Items they could edit directly I would use a ListView, ListBox, or populate a column of a DataGrid. It all depends on what else I want them to do.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top