Insert Into Access - Cannot Update. Database or object is read-only

倾然丶 夕夏残阳落幕 提交于 2020-07-10 17:24:34

问题


I'm trying to upload a table of values from Excel 2013 into Access 2013 database via the VBA code below. However, I keep getting an error. The error occurs on the cn.execute SQL line.

Run-time error '-2147217911 (80040e09)':

Cannot update. Database or object is read-only.

I have checked permissions on the file and Access database and and it is read and write access. I've even tried to create a new microsoft Access database in a different directory but get the same error.

Another thing that has me stumped is that I've re-used this code before without any problems in a different Excel sheet, but using it with this workbook/database does not work.

Sub uploadToDB()

Dim SQL As String

dbPath = "C:\Users\john.smith\Desktop\database.accdb"
wbName = "C:\Macros\" & ActiveWorkbook.Name & ".xlsm"
wsname = "Staging"
TableName = "dataTable"

Set cn = CreateObject("ADODB.Connection")
scn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & dbPath

cn.Open scn

'find number of data points to add
row = Sheets(wsname).Range("A1").End(xlDown).row

SQL = "INSERT INTO " & TableName & " ([field1],[field2],[field3],[field4],[field5],[field6],[field7],[field8]) "
SQL = SQL & "SELECT * FROM [Excel 12.0 Macro;HDR=YES;DATABASE=" & wbName & "].[" & wsname & "$A1:H" & row & "]" '" & wsName

cn.Execute SQL

cn.Close
Set cn = Nothing

End Sub

回答1:


I would try to use a hard-coded SQL statement first, i.e.

SQL = "INSERT INTO " & TableName & " ([field1],[field2],[field3],[field4],[field5],[field6],[field7],[field8]) "
SQL = SQL & "SELECT 'val1', 'val2', etc.

This differentiates the problem between an issue with Access or one with Excel. If the hard-coded statement works, you may need to use IMEX=0 in the reference to Excel. i.e.

Excel 12.0;HDR=Yes;IMEX=0;Readonly=False



回答2:


Please condsider these. One of them might cause this issue:

  • You used the OpenDatabase method and opened the database for read-only access.

  • In Microsoft Visual Basic, you are using the Data control, and you set the ReadOnly property to True.

  • The database file is defined as read-only in the operating system or by your network.

  • The database file is stored on read-only media.

  • In a network environment, you do not have write privileges for the database file.

  • When working with a secured database, the database or one of its objects (such as a field or table) may be set to read-only. You may not have permission to access this data with your user name and password.

Source here.



来源:https://stackoverflow.com/questions/42654245/insert-into-access-cannot-update-database-or-object-is-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!