0

次のコードを実行しようとするとエラーがスローされます。実際にデータベースに接続できたと思いますが、セルが選択されているため、何が欠けているのかわかりません。

エラー:

クエリ式 'PopID =' に構文エラー (演算子がありません)。

理想的には、マクロが実行されるたびに、アクセスの追加で 4 つの列に入る 4 つのセルをリストできるようにしたいと考えています。

Const TARGET_DB = "testdb.accdb"

Sub AlterOneRecord() 'not working yet

   Dim cnn As ADODB.Connection
   Dim rst As ADODB.Recordset
   Dim fld As ADODB.Field
   Dim MyConn
   Dim lngRow As Long
   Dim lngID As String
   Dim j As Long
   Dim sSQL As String

                   'determine the ID of the current record and define the SQL statement
                   lngRow = ActiveCell.Row
                   lngID = Cells(lngRow, 1).Value

   sSQL = "SELECT * FROM tblPopulation WHERE PopID = " & lngID

   Set cnn = New ADODB.Connection
   MyConn = ThisWorkbook.path & Application.PathSeparator & TARGET_DB

   With cnn
     .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"
     .Open MyConn
   End With

   Set rst = New ADODB.Recordset
   rst.CursorLocation = adUseServer
   rst.Open Source:=sSQL, _
            ActiveConnection:=cnn, _
            CursorType:=adOpenKeyset, _
            LockType:=adLockOptimistic

   'Load contents of modified record from Excel to Access.
   'do not load the ID again.
   For j = 2 To 7
      rst(Cells(1, j).Value) = Cells(lngRow, j).Value
   Next j
   rst.Update

   ' Close the connection
   rst.Close
   cnn.Close
   Set rst = Nothing
   Set cnn = Nothing
End Sub

両方とも M$ 製品であり、これが十分に文書化されていないか、実際に実行するのが本当に簡単ではないのは奇妙です。多分私は間違った方法でそれについて行っています。

たとえば、セル A1 と B2 を含めるにはどうすればよいでしょうか。

4

1 に答える 1