简体   繁体   中英

Passing barcode scan from textbox into a multiline textbox

ok stuck on this a while and need advice - i have the form taking in the barcode scan into a textfield and using that textfield to query the database and it returns the PRICE from the products table. but when i scan the next item i get the error described below. its like its looking for two barcodes at the same time...but i have the textfield that holds the barcode set to clear after it has added the PRICE to the items textbox

my code:

    Private Sub txtTest_KeyDown(ByVal sender As Object, _

ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown

        If e.KeyCode = Keys.Enter Then



            Dim con As New OleDbConnection

            Dim databaseprovider As String

            Dim dblocation As String





            databaseprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"

            dblocation = "Data Source = C:\Users\fergus\desktop\Loft Hair Studio Till App\loft.accdb"

            con.ConnectionString = databaseprovider & dblocation



            Dim queryLoft As String = "SELECT Price from Services where Field1 =" & TextBox3.Text & ""



            Dim command As New OleDbCommand(queryLoft, con)



            con.Open()



            Dim myreader As OleDbDataReader = command.ExecuteReader()



            myreader.Read() 'Read the next line from the DataReader



            'ListBox1.Text = myreader("price").ToString

            TextBoxList.Text = myreader("price").ToString

            'TextBox1.Text = myreader("price").ToString



            TextBoxTest.Clear()


        End If

    End Sub

error:

oledbexception was unhandled

syntax error(missing operator) in query expression 'Field1 = 61542451 61524587'

its like its searching the database for 2 barcode numbers at the same time.

seems odd.

any help appreciated

I noticed your event handler was named txtTest_KeyDown but it is handling TextBox3. I'm guessing you'll want to clear TextBox3 out after handling the scan.

Private Sub TextBox3_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.Handled = True

        Using cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\users\fergus\desktop\Loft Hair Studio Till App\loft.accdb")
            cn.Open()

            Using cmd = New OleDbCommand("SELECT Price FROM Services WHERE Field1=?", cn)
                cmd.Parameters.AddWithValue("@scan", TextBox3.Text)

                Using reader = cmd.ExecuteReader()
                    If reader.Read Then
                        TextBoxList.Text = reader("price").ToString
                        TextBox3.Clear()
                    End If
                End Using
            End Using
        End Using
    End If
End Sub

Also, it's good practice to employ Using statements for disposable resources such as connections, commands, and readers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM