Question Custom SQL query

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
Hello everyone. I am using VS/VB 08 together with access 07 for my project. I am trying to fill my datagridview using a custom SQL query where the "where" criteria is altered to accept the column name from the selection of the user. When a user clicks on a column in the datagrid I want that column to be represented in the query. This is what I have but is not working. Is this possible by the way?

VB.NET:
    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Serverjde\jde\Database0.accdb;Persist Security Info=False;")

        Dim search1 As String = Me.TextBox3.Text
        Dim search2 As String = Me.TextBox1.Text
Try
                
conn.Open()

                Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from AllServices where 'search1' = '" & search2 & "'", conn)

                Dim ds As Database2DataSet = New Database2DataSet
                ' fill dataset 
                da.Fill(ds, "AllServices")

                ' Attach DataSet to DataGrid 
                AllServicesDataGridView.DataSource = ds.Tables("AllServices")


            Catch ex As Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            End Try

U can see that 'search1' get its data from a textbox which represents the current selection of column the user has made.
 
First off look into using parameterized queries. If you have a quotation mark in search2 your query gets messed up.

Your issue is here
VB.NET:
"Select * from AllServices where 'search1' = '" & search2 & "'"

Right now if you run that query you will get all records from AllServices where the string 'search1' is equal to the value of search2. So if you typed 'search1' in textbox1 you would get all data in AllServices.

Also you said its not working, can you give us more details? Is it giving an error? Is it giving you unexpected data?...
 

Latest posts

Back
Top