【问题标题】:Excel data to Access DB - Get: Operation must use an updateable query ErrorExcel 数据到 Access DB - 获取:操作必须使用可更新查询错误
【发布时间】:2017-06-19 15:31:35
【问题描述】:

我正在开发一个 Excel 应用程序,它允许用户通过用户表单输入工作时间,并且信息存储在 Access DB 中。我是 excel 和访问连接的新手。我能够连接到数据库,但由于 .Update 命令 处的运行时错误,未保存/创建记录。 运行时错误“-2147467259 (80004005)”:操作必须使用可更新查询。

我已经搜索和搜索,但找不到解决此问题的方法。我希望有人能够提供帮助。 (代码如下)

Sub Export_Data_Access_TI1()

Dim dbPath As String
Dim x As Long, i As Long
Dim nextrow As Long
Dim user As String
Dim NewSht As Worksheet
Dim strQuery As String
Dim recDate As String
Dim Week_Of As String

user = Sheet1.Range("A1").Text

On Error GoTo ErrHandler:

'Variables for file path and last row of data
dbPath = "H:\PROJECTS\CAI_DOT-Time Tracker\CAI_EMP_SignIn_Database.accdb"
nextrow = Cells(Rows.Count, 1).End(xlUp).Row

'Initialise the collection class variable
Set cnn = New ADODB.Connection

'Check for data
If Sheets(user).Range("A2").Value = "" Then
MsgBox " There is no data to send to MS Access"
Exit Sub
End If

cnn.Mode = adModeReadWrite
'cnn.Mode = adModeShareDenyNone
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath

Set rst = New ADODB.Recordset 'assign memory to the recordset

rst.CursorLocation = adUseClient
rst.Open Source:="DATA", ActiveConnection:=cnn, _
    CursorType:=adOpenKeyset, LockType:=adLockPessimistic, _
    Options:=adCmdTable
'rst.Supports (adAddNew)

x = 2  'the start row in the worksheet
Do While Len(Sheets(user).Range("A" & x).Formula) > 0

With rst

.AddNew 'create a new record

.Fields("Date") = ActiveWorkbook.Sheets(user).Range("A" & x).Value
.Fields("Week_Of") = Sheets(user).Range("B" & x).Value
.Fields("Month") = Sheets(user).Range("C" & x).Value
.Fields("Name") = Sheets(user).Range("D" & x).Value
.Fields("Time_In") = Sheets(user).Range("E" & x).Value
.Fields("Time_Out") = Sheets(user).Range("F" & x).Value
.Fields("Time_In2") = Sheets(user).Range("G" & x).Value
.Fields("Time_Out2") = Sheets(user).Range("H" & x).Value
.Fields("Group") = Sheets(user).Range("I" & x).Value
.Fields("UniqueID") = Sheets(user).Range("J" & x).Value
.Fields("Comments") = Sheets(user).Range("K" & x).Value

.Update 'stores the new record
End With

x = x + 1 'next row
Loop

rst.Close

cnn.Close

Set rst = Nothing
Set cnn = Nothing

'communicate with the user
MsgBox " The data has been successfully sent to the access database"

'Update the sheet
Application.ScreenUpdating = True

'Clear the data
'Sheets(user).Range("A1:K1000").ClearContents
On Error GoTo 0
Exit Sub
ErrHandler:

'clear memory
Set rst = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data"
End Sub

【问题讨论】:

  • 您是否在查询中包含主键?如果您希望它可更新,这是必需的。
  • 您好 braX,是的,包括主键(UniqueID)。感谢您的回复。

标签: excel ms-access vba


【解决方案1】:

据我了解,DATA 是远程 accdb 中的一个查询。如果是这样,它应该是可更新的。例如,请参阅:Why is my query not updateable? 了解标准。如果这是一个表,请检查您是否对 accdb 有读写权限,并且该文件没有只读属性。

【讨论】:

  • Sergey,感谢您的快速响应,它是一个表,但是 accdb 具有所有权限,并且文件没有只读属性。
猜你喜欢
  • 2010-10-03
  • 2011-06-29
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 2013-11-16
  • 1970-01-01
相关资源
最近更新 更多