首先要做的是在构建命令文本并使用参数时删除字符串连接,然后为描述设置TextBox,而不是其文本属性。还删除了一些无用的代码
Private Sub txt_productcode_Leave(sender As Object, e As EventArgs) Handles txt_productcode.Leave
Dim dt As New DataTable
Dim connstring As String = "...."
Dim SqlString As String = "select [product group],[product description] " & _
"from [stock] where [stock code] = @stock"
Using conn As New OleDbConnection(connstring)
Using command As New OleDbCommand(SqlString, conn)
Using adapter As New OleDbDataAdapter(command)
conn.Open()
command.Parameters.AddWithValue("@stock", txt_productcode.Text)
adapter.Fill(dt)
End Using
For y = 0 To dt.Rows.Count - 1
Dim MyDataRow = dt.Rows(y)
txt_productgroup.Text = MyDataRow("product group").ToString()
txt_productdescription.Text = MyDataRow("product description").ToString()
Next
End Using
End Using
End Sub