dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Filling combo box with with a filter from a different combo box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2018
    Posts
    24

    Resolved [RESOLVED] Filling combo box with with a filter from a different combo box

    Hello,
    I use Visual studio 2015, and Access 2016 and I am trying to fill a combo box with a select statement with a filter from another combo box.

    Such as if combo box 1 contains State(AK) then In combo box 2 I want to show counties that are in that state (4)from the complete list of counties (3077). Don't want to show the Number I want to show the name of county. Yes the database is there and the table does have state(by 2 letter abrev. and the county by name) Here is the code for the State that works, and the County that doesn't :

    Public Sub GetHState()

    'Gets the State's to show in comboBox his state

    Dim mcall = lblCall.Text
    Dim ds As New DataSet
    Dim dt As New DataTable
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter

    '------------ FROM HERE THE NEXT LINES SET UP THE CONNECTION STRING FOR USER'S DATABASE------------------

    Dim strFilePrefix = "NetControl"
    Dim strFileSuffix = ".accdb"
    Dim databaseFile As String = "C:\RRLogger Data\" & strFilePrefix & strFileSuffix
    Dim conString = "Provider = Microsoft.Ace.OLEDB.12.0; Data Source= " & databaseFile

    '--------------------------------------------------------------------------------------------------------------
    con.ConnectionString = conString

    con.Open()

    da = New OleDbDataAdapter("Select State From State1", con) '

    da.Fill(dt)

    con.Close()


    'CLEar combobox
    cmbHState.Items.Clear()

    'FILL COMBOBOX
    For Each R As DataRow In dt.Rows
    cmbHState.Items.Add(R("State"))

    Next

    'DISPLAY FIRST RECORD
    cmbHState.Text = CType(dt.Rows(0).Item(0), String)


    End Sub

    Private Sub GetMCounty()

    ' Dim Access As New DbControl
    'Dim mcall = lblCall.Text
    Dim ds As New DataSet
    Dim dt As New DataTable
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter

    '------------ FROM HERE THE NEXT LINES SET UP THE CONNECTION STRING FOR USER'S DATABASE------------------

    Dim strFilePrefix = "NetControl"
    Dim strFileSuffix = ".accdb"
    Dim databaseFile As String = "C:\RRLogger Data\" & strFilePrefix & strFileSuffix
    Dim conString = "Provider = Microsoft.Ace.OLEDB.12.0; Data Source= " & databaseFile

    '--------------------------------------------------------------------------------------------------------------
    con.ConnectionString = conString

    con.Open()

    da = New OleDbDataAdapter("SELECT County FROM County WHERE State = cmbhState.Text", con)


    da.Fill(dt)

    con.Close()
    'CLEar combobox
    cmbMCounty.Items.Clear()

    For Each R As DataRow In dt.Rows
    cmbMCounty.Items.Add(R("County"))
    Next

    'DISPLAY FIRST RECORD



    End Sub

    When I run it I get the error...... Message "No value given for one or more required parameters." String

    Please help?????
    Last edited by MikeDelke; Mar 4th, 2018 at 11:10 AM.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    462

    Re: Filling combo box with with a filter from a different combo box

    Quote Originally Posted by MikeDelke View Post
    da = New OleDbDataAdapter("SELECT County FROM County WHERE State = cmbhState.Text", con)
    There may be more issues than this, but the above line is problematic for two reasons that I see.

    The first is that if you want to use the value in cmbhState.Text rather than the literal string "cmbhState.Text", then cmbhState.Text needs to be outside of the quotes. The second is that, assuming State is not a numeric value, then you need to surround the search for value in quotes.

    Addressing those issues should look something like this:

    Code:
            da = New OleDbDataAdapter("SELECT County FROM County WHERE State = '" & cmbhState.Text & "'", con)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2018
    Posts
    24

    Re: [RESOLVED] Filling combo box with with a filter from a different combo box

    Thank You for the Help.

    That fixed the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width