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.
*******************************************
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