2017-06-19 22 views
0

ユーザーがユーザーフォームを使用して作業し、情報がAccessデータベースに格納されるExcelアプリケーションを作成しています。私は接続にアクセスしてアクセスするために新しいです。データベースに接続できましたが、実行時エラーのためにレコードが保存/作成されませんでした。.Updateコマンド。 実行時エラー '-2147467259(80004005)':操作で更新可能なクエリを使用する必要があります。Access DBへのExcelデータ - 取得:操作で更新可能なクエリを使用する必要があります。エラー

私は検索して検索しましたが、この問題の解決策は見つかりませんでした。誰かが助けてくれることを願っています。 (下のコード)

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 
+0

クエリにプライマリキーを含めましたか?これは、更新可能にしたい場合に必要です。 – braX

+0

こんにちはbraX、はい主キーが含まれています(UniqueID)。お返事をありがとうございます。 – Andrea

答えて

0

私が理解しているように、DATAはリモートaccdbのクエリです。もしそうなら、それは更新可能でなければならない。たとえば、基準:Why is my query not updateable?を参照してください。これがテーブルの場合は、accdbに対する読み取り/書き込み権限があるかどうかをチェックし、ファイルに読み取り専用属性がないかどうかを確認します。

+0

Sergey、クイックレスポンスありがとう、それはテーブルですが、accdbにはすべての権限があり、ファイルには読み取り専用の属性はありません。 – Andrea

関連する問題