我是VB的新手,几年前来自Visual Foxpro,需要一些结构和语法方面的帮助。
我正在尝试创建一个系统,该系统可以接受员工的ID号,来自带键盘楔的条形码扫描器,并从Access数据库中提取他们的姓名和员工编号。
本节的目的是预先提取ID数据,并确保他们可以访问该区域,因此可以用于库存检入/退出系统。
目前,代码进入实际的Query“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
答案 0 :(得分:1)
我看到两个问题:
第一个问题是您的连接字符串使用逗号,需要使用分号。正确的语法是
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\rcassel\Documents\Visual Studio 2012\Projects\Inventory Control\Inventory Control\Inventory_Control2DataSet.xsd;
第二个问题是,一旦修复了连接字符串,就会发现Microsoft.Jet.OLEDB.4.0
不支持将.xsd
(XML架构文档)文件用作Data source
。它也不支持直接读取XML数据。
您需要将XML数据导入Access数据库,或者使用某些VB.NET魔法(可能是ADO.NET)直接对XML数据执行查找。