CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2014
    Posts
    2

    Question How to Select statement using the value from Combobox in the form of VB.net

    Hi
    i m a new to VB.net. I'm trying to show the different form of DataGrid View based on the value from Combobox. The problem is the data from Combobox and Datagrid view are from different tables.

    I have form1, form2 and form3
    Here is some of my code from form2
    I would like to write the select statement matching the value from Fr2ComBo in ea00 table and show the result in form3 with datagrid view.

    Could someone please help me???

    Thank you in advance.

    Public Class Form2

    Private Property Avail3 As BindingSource

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'AvailgrpDataSet.arctm' table. You can move, or remove it, as needed.
    Me.ArctmTableAdapter.Fill(Me.AvailgrpDataSet.arctm)

    Dim connetionString As String
    Dim connection As OleDbConnection


    Dim oledbAdapter As OleDbDataAdapter
    Dim ds As New DataSet
    Dim sql As String
    ''Dim i As Integer

    connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\visual basic\availgrp.mdb;"
    sql = "select reptrc + ' ' + desc as reptrcdesc From arctm "
    connection = New OleDbConnection(connetionString)

    Try
    connection.Open()

    oledbAdapter = New OleDbDataAdapter(sql, connection)
    oledbAdapter.Fill(ds) 'fill the datatable from the SQL and the dataSet
    oledbAdapter.Dispose()
    connection.Close()
    'TODO: This line of code loads data into the 'AvailgrpDataSet.arctm' table. You can move, or remove it, as needed.
    '' Me.ArctmTableAdapter.Fill(Me.AvailgrpDataSet.arctm)

    Fr2ComBo.DataSource = ds.Tables(0)
    Fr2ComBo.DisplayMember = "reptrcdesc"

    Catch ex As Exception
    MessageBox.Show("Can not open connection ! ")
    End Try
    End Sub

    *** that's where my problem start

    Private Sub ButCnt_Click(sender As System.Object, e As System.EventArgs) Handles ButCnt.Click

    Dim connetionStrings As String
    Dim connection As OleDbConnection

    Dim oledbAdapter As OleDbDataAdapter
    Dim ds As New DataSet
    Dim sql As String

    Avail3 = New BindingSource(ds, "ea00")

    ' ' how to filter and show the result in form3.????

    sql = " select * from ea00 where reptrc=Fr2ComBo.text"

    Form3.show()

    End Sub

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to Select statement using the value from Combobox in the form of VB.net

    Did you try they TODO's? Uncomment one just for fun!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to Select statement using the value from Combobox in the form of VB.net

    Code:
    sql = " select * from ea00 where reptrc=Fr2ComBo.text"
    Since that is all in quotes it would be looking for the literal text " Fr2ComBo.text"

    You need to build the string so that it gets the value of the combobox
    Code:
    sql = " select * from ea00 where reptrc=" & Fr2ComBo.text
    Note that if this is a text field then you also need to use single quotes around that value, if it is a date field then you may need to use single quotes around it or you may need to use the # symbol around it depending on the database in use
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jun 2014
    Posts
    2

    Re: How to Select statement using the value from Combobox in the form of VB.net

    Quote Originally Posted by DataMiser View Post
    Code:
    sql = " select * from ea00 where reptrc=Fr2ComBo.text"
    Since that is all in quotes it would be looking for the literal text " Fr2ComBo.text"

    You need to build the string so that it gets the value of the combobox
    Code:
    sql = " select * from ea00 where reptrc=" & Fr2ComBo.text
    Note that if this is a text field then you also need to use single quotes around that value, if it is a date field then you may need to use single quotes around it or you may need to use the # symbol around it depending on the database in use
    Thank you, this sql works well.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured