Click here to Skip to main content
15,742,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Private Sub TextBox12_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox12.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter

                Dim sqlQRY As String = "SELECT * FROM Items_&_MultiBarcode WHERE Barcode = '" & TextBox12.Text & "'"
                Dim cmd As OleDbCommand = New OleDbCommand(sqlQRY, conn)
                Dim rdr As OleDbDataReader = cmd.ExecuteReader

                If rdr.HasRows Then

                    Dim Barcode As String = rdr("Barcode").ToString
                    Dim newRow As POS_DataSet6.Sales1Row = POS_DataSet6.Sales1.NewSales1Row
                    newRow.Barcode = Barcode
                    Dim Description As String = rdr("Description")
                    newRow.Description = Description
                    Dim Count As String = rdr("Count")
                    newRow.Count = Count
                    newRow.Count = +1
                    Dim Price As Decimal = rdr("Price")
                    newRow.Price = Price
                    newRow.Subtotal = newRow.Count * newRow.Price
                    newRow.Tax = newRow.Subtotal * 1,2
                    newRow.Total = (newRow.Subtotal + newRow.Tax)
                    TextBox12.Text = ("")
                End If

        End Select
    End Sub

What I have tried:

Hi! This " Items_&_MultiBarcode " is Query from Table access Items and MultiBarcode.
I want to add in datagridview one item with more than one barcodes. For this I have a relationship with access: Items and MultiBarcode. When I change code from:
"SELECT * FROM Items WHERE Barcode = '"
"SELECT * FROM Items_&_MultiBarcode WHERE Barcode = '"
I show one msgbox for Dim rdr As OleDbDataReader = cmd.ExecuteReader :

Syntax error in FROM clause.

Is any solution this problem so to connect this code with Query ?
Please Help :) Thanks!
Updated 10-May-16 3:39am
Member 12003400 10-May-16 9:40am    
what is this Items_&_MultiBarcode? Is it a single table or view?

1 solution

'&' is a special character and needs special handling for use in names.
SELECT * FROM [Items_&_MultiBarcode] WHERE Barcode = ...

SELECT * FROM `Items_&_MultiBarcode` WHERE Barcode = ...

But...Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
Share this answer
ionMEMBER 10-May-16 13:04pm    
OriginalGriff 10-May-16 13:58pm    
You're welcome!

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