提问人:St3althPatchin 提问时间:9/8/2023 最后编辑:Joel CoehoornSt3althPatchin 更新时间:9/9/2023 访问量:130
为什么我的 Access 数据库连接 vb 显示身份验证失败
Why is my Access Database connection vb stating authentication failed
问:
我是第一次尝试使用 Microsoft Access。使用它创建一个数据库,用于存储我将从大量 Microsoft Excel 文件中提取的信息。不幸的是,当我尝试从Excel文件设置函数和与数据库的连接时,我遇到了这个错误,我不知道如何解决。我一直在使用在线资源来尝试第一次构建它,但我还没有看到任何人自己遇到具体问题。每次它通过我拥有的一个函数时,似乎都会发生此错误。我希望有人能够提供一些见解。似乎每次我运行它并去调试它时都会突出显示.打开时显示 GetKeyID 函数中的错误。但我以前从未见过这种情况。
Sub TestTransferDataToAccess()
Dim ConnObj As ADODB.Connection
Dim RecSet As ADODB.Recordset
Dim ConnCmd As ADODB.Command
Dim ColNames As ADODB.Fields
Dim DataSource As String
Dim intLoop As Integer
'Datasource
DataSource = "O:\Department\Engineering\Einstein\00 - Systems Engineering\04 - Databases\VerificationLogDB.accdb"
'Create a new connection object & a new command object
Set ConnObj = New ADODB.Connection
Set ConnCmd = New ADODB.Command
'Create a new connection
With ConnObj
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = DataSource
.Open
End With
'command object to use Active Connection shortcut method
ConnCmd.ActiveConnection = ConnObj
' Define Excel workbook and worksheet
Dim wb As Workbook
Dim ws As Worksheet
' Set Excel workbook and worksheet
Set wb = Workbooks.Open("O:\Swap\Chicago\GenoF\Reading-2021-03-22-10-14-48.xls")
' Set worksheet
Set ws = wb.Sheets("As Found-CW Data")
' Loop through Excel data and insert into Access database
Dim headerID As Long
Dim keyID As Long
Dim torqueValue As Double
Dim row As Long
For row = 16 To 25 ' Rows B16 to B25 for Channel 1 data
headerID = GetHeaderID(ws.Cells(row, 2).Value) ' Used for header lookup
keyID = GetKeyID("Channel 1") ' Used for key lookup
torqueValue = CDbl(ws.Cells(row, 3).Value) ' Used for torque data cell
' Insert data into Access database
strSQL = "INSERT INTO tblTorqueData (headerID, keyID, torqueValue) VALUES (" & headerID & ", " & keyID & ", " & torqueValue & ")"
ConnCmd.Execute strSQL
Next row
' Close the Recordset and Connection
RecSet.Close
ConnObj.Close
Set RecSet = Nothing
Set ConnCmd = Nothing
Set ConnObj = Nothing
MsgBox "Data transfer to Access completed."
End Sub
Function GetHeaderID(headerName As String) As Long
'variables for database connection
Dim conn As Object
Dim rs As Object
Dim RecSet As ADODB.Recordset
'Define the Datasource
DataSource = "O:\Department\Engineering\Einstein\00 - Systems Engineering\04 - Databases\VerificationLogDB.accdb"
'Create a new connection object & a new command object
Set ConnObj = New ADODB.Connection
Set ConnCmd = New ADODB.Command
'Create a new connection
With ConnObj
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = DataSource
.Open
End With
'Allow command object to use the Active Connection shortcut method
'ConnCmd.ActiveConnection = ConnObj
' Use SQL query to retrieve HeaderID based on headerName
Dim strSQL As String
strSQL = "SELECT HeaderID FROM tblHeaders WHERE HeaderName = '" & headerName & "'"
'Execute the Query & Get the Column Names.
'rs.Open strSQL, conn
Set rs = CreateObject("ADODB.Recordset")
' Check if record was found
'If Not rs.EOF Then
' GetHeaderID = rs.Fields(0).Value
'Else
' Handle the case where no record was found
' GetHeaderID = -1 ' Replace with default value
'End If
' Close the Connection
ConnObj.Close
' Release ADODB objects
Set rs = Nothing
Set conn = Nothing
End Function
Function GetKeyID(keyName As String) As Long
' variables for database connection
Dim conn As Object
Dim rs As Object
Dim RecSet As ADODB.Recordset
'path to Access database file
Dim dbPath As String
dbPath = "O:\Department\Engineering\Einstein\00 - Systems Engineering\04 - Databases\VerificationLogDB.laccdb"
'database and recordset
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
'Create a new connection
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = DataSource
.Open
End With
'Allow command object to use the Active Connection shortcut method
ConnCmd.ActiveConnection = ConnObj
' Use SQL query to retrieve KeyID based on keyName
Dim strSQL As String
strSQL = "SELECT KeyID FROM tblKeys WHERE KeyName = '" & keyName & "'"
' Execute query and get result
Set RecSet = ConnCmd.Execute
'rs.Open strSQL, conn
' Check if record was found
If Not rs.EOF Then
GetKeyID = rs.Fields(0).Value
Else
' Handle case where no record was found
GetKeyID = -1 ' Replace with appropriate default value
End If
' Close the Recordset and Connection
rs.Close
conn.Close
' Release ADODB objects
Set rs = Nothing
Set conn = Nothing
End Function
答:
仅创建一个连接并将其用于所有查询
Option Explicit
Sub TestTransferDataToAccess2()
Const db = "O:\Department\Engineering\Einstein\00 - Systems Engineering\04 - Databases\VerificationLogDB.accdb"
Const WBPATH = "O:\Swap\Chicago\GenoF\Reading-2021-03-22-10-14-48.xls"
Dim rs As ADODB.Recordset, cmd As ADODB.Command
Dim keyID As Long, keyName As String, hdr As String
Dim wb As Workbook, ws As Worksheet, ar, arHdrID
Dim r As Long, n As Long, num As Long
Dim t0 As Single: t0 = Timer
' copy data from sheet to an array
Set wb = Workbooks.Open(WBPATH)
Set ws = wb.Sheets("As Found-CW Data")
With ws
ar = .Range("B16:C25")
ReDim arHdrID(1 To UBound(ar))
End With
' get keyid for keyname
keyName = "Channel 1"
keyID = GetKeyID(keyName, db)
If keyID < 0 Then
MsgBox "No KeyID for " & keyName, vbExclamation
Exit Sub
End If
' get header IDs
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = dbConnect(db)
.CommandText = "SELECT HeaderID FROM tblHeaders WHERE HeaderName = ?"
.Parameters.Append .CreateParameter("p1", adVarChar, adParamInput, 10)
For r = 1 To UBound(ar)
hdr = ar(r, 1)
.Parameters(0) = hdr
Set rs = .Execute
If rs.EOF Then
MsgBox "No HeaderID for " & hdr, vbExclamation
Exit Sub
End If
arHdrID(r) = rs.Fields(0)
'Debug.Print r, arHdrID(r), rs.Fields(0)
Next
.ActiveConnection.Close
End With
' prepare and execute insert command
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = dbConnect(db)
.CommandText = "INSERT INTO tblTorqueData (headerID, keyID, torqueValue) VALUES (?,?,?)"
.Parameters.Append .CreateParameter("p1", adBigInt, adParamInput)
.Parameters.Append .CreateParameter("p2", adBigInt, adParamInput)
.Parameters.Append .CreateParameter("p3", adNumeric, adParamInput)
' execute inserts
For r = 1 To UBound(ar)
.Parameters(0) = arHdrID(r) ' headerID
.Parameters(1) = keyID
.Parameters(2) = CDbl(ar(r, 2))
.Execute num
n = n + num
Next
.ActiveConnection.Close
End With
MsgBox n & " records inserted into " & db, vbInformation, Format(Timer - t0, "0.0 secs")
End Sub
Function GetKeyID(keyName, db) As Long
Dim rs As ADODB.Recordset
With New ADODB.Command
.ActiveConnection = dbConnect(db)
.CommandText = "SELECT KeyID FROM tblKeys WHERE KeyName = ?"
.Parameters.Append .CreateParameter("p1", adVarChar, adParamInput, 20, keyName)
Set rs = .Execute
If rs.EOF Then
GetKeyID = -1
Else
GetKeyID = rs.Fields(0)
End If
.ActiveConnection.Close
End With
End Function
Function dbConnect(db) As ADODB.Connection
Set dbConnect = New ADODB.Connection
With dbConnect
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = db
.Open
End With
End Function
评论
连接字符串应更像这样,而不仅仅是文件路径:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=O:\Department\Engineering\Einstein\00 - Systems Engineering\04 - Databases\VerificationLogDB.accdb;
有关连接字符串的详细信息,请参阅:
我在这里看到的另一件事是它很可能是共享驱动器。值得一提的是,共享共享驱动器不是系统 (OS) 级别的构造,而是仅限于特定会话,因此一个用户的 O: 驱动器可能不适用于另一个用户,即使在同一台计算机上也是如此。O:
您告诉我们错误发生在函数的连接步骤中。因此,我建议您将注意力集中在该特定操作(打开 ADODB 连接)上。.Open
GetKeyID
这是在我的系统上运行没有错误的示例代码。将 的值 back 更改为 your 的值并对其进行测试。如果成功,它将在“即时”窗口中打印 1(用于 )。dbPath
State
Public Sub test_connection()
Dim conn As Object
Dim DataSource As String
Dim dbPath As String
'dbPath = "O:\Department\Engineering\Einstein\00 - Systems Engineering\04 - Databases\VerificationLogDB.laccdb"
dbPath = "C:\Users\hansu\AppData\Roaming\AccessApps\AppStarter.accdb"
DataSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
Set conn = CreateObject("ADODB.Connection")
With conn
.ConnectionString = DataSource
.Open
Debug.Print .State
End With
End Sub
注意,然后是行。您的代码也不包括。这就是为什么你发现“我的数据源在我的局部变量窗口中显示为空”的原因。如果没有,Access 将被视为未声明的变体,并且由于您从未为其分配值,因此它仍然是空变体。所以连接自然会失败。Dim DataSource As String
DataSource =
Dim DataSource
DataSource
Open
添加到代码模块的“声明”部分。然后,至少 Access 可以提醒您注意未声明的变量。Option Explicit
此外,您似乎正在尝试连接到锁定文件。请改为连接到数据库。我的猜测是你想连接到(accdb而不是laccdb)。VerificationLogDB.laccdb
VerificationLogDB.accdb
评论
rs.Open strSQL, ConnObj
Debug.Print strSQL
评论
WHERE HeaderName = '" & headerName & "'"
keyName