我是 VB 的新手,几年前来自 Visual Foxpro,需要一些结构和语法方面的帮助。
我正在尝试创建一个系统,该系统可以从带有键盘楔的条形码扫描仪中接受员工的 ID 号,并使用他们的姓名和员工编号从 Access 数据库中提取它们。
本节的目的是预先提取 ID 数据,并确保他们可以访问该区域,以便可以将其用于库存签入/签出系统。
目前,代码进入实际的查询“OleEmp”部分,然后停止而不提取数据或填充任何内容。
我到底做错了什么?
谢谢!
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Public EmpID As Double
Public dbEmpID As Double
Public Barcode As Double
Public FirstName As String '= "Test"
Public LastName As String '= "Account"
Public OleDbConn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\rcassel\Documents\Visual Studio 2012\Projects\Inventory Control\Inventory Control\Inventory Control2.accdb;")
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
EmpID = (Val(TextBox1.Text))
'Checks to see if someone entered a Badge
If EmpID = Nothing Then
MsgBox("You must scan your Badge!", MsgBoxStyle.OkOnly)
TextBox1.Focus()
Else
'Fire Query into Database
Try
Dim OleEmp As New OleDbCommand("SELECT [First Name],[Last Name],[Employee ID] FROM Contacts WHERE [Employee ID]=" + EmpID + "", OleDbConn)
Dim r1 As OleDbDataReader = OleEmp.ExecuteReader()
While r1.Read()
FirstName = CStr(r1("First Name"))
LastName = CStr(r1("Last Name"))
dbEmpID = CInt(r1("Employee ID"))
End While
r1.Close()
Catch ex As Exception
MsgBox("Cannot Pull Data." & vbCrLf & ex.Message)
End Try
If dbEmpID = Nothing Then
MsgBox("You are not Authorised to use this device. This activity has been logged.", MsgBoxStyle.OkOnly)
Else
Me.ListBox1.Items.Add(FirstName)
Me.ListBox1.Items.Add(LastName)
Me.ListBox1.Items.Add(EmpID)
TextBox2.Focus()
End If
OleDbConn.Close()
End If
End Sub