Home > Blockchain >  Can I add an extra row, at the top of a combo box that is being populated from an Access DB
Can I add an extra row, at the top of a combo box that is being populated from an Access DB

Time:11-15

I have a vb combo box whose values are being taken from a SELECT DISTINCT SQL statement run against an Access DB. The value selected will be used as a parameter in a subsequent SELECT statement. My concern is that I want the user to be able to avoid selecting a value so that either the parameter will be dropped from the next SELECT statement, or will use an * as a wildcard. I don’t want to have to add a dummy row in the table to hold a null value or asterisk, so I need to find some way to add a row, in the top spot, showing that. I’m not sure how/where to put the INSERT statement so that it will show up at the top of the combo box but not in the DB. Here’s the code I’m using. I’m pretty new to VB and selected this method to populate the combo box as it seemed reasonable easy to understand. I’d appreciate any help. Thanks.

    myConnToAccess = New OleDbConnection(myVariables.myStrConnectionString)
    myConnToAccess.Open()
    ds = New DataSet
    tables = ds.Tables
    da = New OleDbDataAdapter("SELECT DISTINCT ProcUserName FROM Processes ORDER BY ProcUserName", myConnToAccess)
    da.Fill(ds, "Processes")
    Dim view1 As New DataView(tables(0))
    With cboUserID
        .DataSource = ds.Tables("Processes")
        .DisplayMember = "ProcUserName"
        .ValueMember = "ProcUserName"
        .SelectedIndex = 0
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems
    End With
    myConnToAccess.Close()

CodePudding user response:

You could add the dummy row in the DataTable after you extract the data from the db setting its position with DataTable.Rows.InsertAt

....
da.Fill(ds, "Processes")
Dim dt As DataTable = ds.Tables("Processes")
Dim row As DataRow = dt.NewRow()
row("ProcUserName") = "(All)"
dt.Rows.InsertAt(row, 0)
  • Related