Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, what i am trying to do is, if i select values from the first two combo box and click search it should display the result in third text box. I am using Ms Access as my database.


coding in VB to link to DB

VB
Imports System.Data.OleDb
Public Class Form1
    Dim v_HardnessDbconn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\Users\Desktop\Hardness.accdb")
    Dim v_datareader As OleDbDataReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        v_HardnessDbconn.Open()
        v_HardnessDbconn.Close()
    End Sub


coding on the search box
VB
    Private Sub searchtext_Click(sender As Object, e As EventArgs) Handles searchtext.Click
        v_HardnessDbconn.Open()
        TextBox1.Clear()
        Dim Str As String

        Str = "SELECT * From Items Where (Material = '" & ComboBox1.Text & "', Hardness = '" & ComboBox2.Text & "')"
        Dim v_dbCommand As OleDbCommand

        v_dbCommand = New OleDbCommand(Str, v_HardnessDbconn)
        Dim v_dataReader As OleDbDataReader = v_dbCommand.ExecuteReader()
        If v_dataReader.HasRows Then
            While v_dataReader.Read
                TextBox1.Text = v_dataReader("Material Code").ToString

            End While
            v_HardnessDbconn.Close()
        End If


    End Sub
End Class


error getting after running it
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error (comma) in query expression '(Material = 'Plain Carbon Steel ( AISI 1010- AISI 1030)', Hardness = '150-180')'.


Can someone please help.
Is there some mistake i made in this expression, where i am choosing values from two different combo boxes and then reading those values in the database to display my result :

SQL
Str = "SELECT * From Items Where (Material = '" & ComboBox1.Text & "', Hardness = '" & ComboBox2.Text & "')"


or is something wrong with my database?
Posted
Updated 9-Sep-15 1:09am
v3
Comments
Michael_Davies 9-Sep-15 8:18am    
Hi, Be careful, you only close the database if the reader has rows, move the database close outside of the if otherwise you will get an exception the next time you open the database as it is already open.

Hello,

You need a logical relationship between your condition, OR/AND etc.

VB
Str = "SELECT * From Items Where Material='" & ComboBox1.Text & "' AND Hardness='" & ComboBox2.Text & "'"


Will work...
 
Share this answer
 
v2
Comments
Maciej Los 9-Sep-15 11:20am    
5ed!
Member 11963188 10-Sep-15 6:15am    
hello,
i made the changes as suggested, but now another error is coming which says " The Microsoft Office Access database engine cannot find the input table or query 'item'. Make sure it exists and that its name is spelled correctly.

i have made the connection to my database as shown above. Although i didnt add any reference in visual studio ( not sure whether i have to do that or not).

can you please have a look and tell me what's wrong i am doing.

I am new at all these stuff.
sorry for bothering again
thanks
Michael_Davies 10-Sep-15 10:12am    
Which is odd; according to your select the table is called Items not Item. However as Maciej has said in reply there are reserved words that you ought not to use.

Read the link provided by Maceij and either rename the table to a non-reserved name or put the name between [] in your SQL.
In addition to solution 1...

Please, do not use command like this:
VB
Str = "SELECT * From Items Where Material='" & ComboBox1.Text & "' AND Hardness='" & ComboBox2.Text & "'"


Rather than it, use parameterized query:
VB
Str = "SELECT * From Items Where Material=? AND Hardness=?"


How to use it?
Please, see this: OleDbParameterCollection.Add Method (OleDbParameter)[^]
 
Share this answer
 
Comments
Member 11963188 10-Sep-15 6:16am    
hello,
I am getting another error coming which says " The Microsoft Office Access database engine cannot find the input table or query 'item'. Make sure it exists and that its name is spelled correctly.

i have made the connection to my database as shown above. Although i didnt add any reference in visual studio ( not sure whether i have to do that or not).

can you please have a look and tell me what's wrong i am doing.

I am new at all these stuff.
sorry for bothering again
thanks
Maciej Los 10-Sep-15 6:56am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900