1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Excel VBAでAccessにアクセス

Last updated at Posted at 2021-08-31

概要

Excelで作成したデータをAccessに反映させるときに作った雛形コードです。
参照設定は不要です。

Sub サンプル()
    
    Dim DBpath As String 'Accessファイルのフルパス
    Dim DBobject As Object 'Accessへ接続用のオブジェクト
    Dim strSQL As String 'SQL文
    
    '----------------------------------
    'DBに接続
    '----------------------------------
    Set DBobject = CreateObject("ADODB.Connection")
    DBpath = "Accessファイルへのフルパス"
    DBobject.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBpath & ";"
    
    '----------------------------------
    'ここにデータ更新部分を作成
    '----------------------------------
    
    '----------------------------------
    '後処理
    '----------------------------------
    DBobject.Close          'DBの切断
    Set DBobject = Nothing  'オブジェクトの開放
    
End Sub

使い方サンプル

UPDATEの場合

    '----------------------------------
    'ここにデータ更新部分を作成
    '----------------------------------
    Dim p As Long
    p = 2
    With ThisWorkbook.Worksheets("Sheet2")
    
    Do While .Cells(p, "A").Value <> ""
        'SQL文の作成
        strSQL = "UPDATE テーブル名 set 変更フィールド名='" & .Cells(p, "B").Value
        strSQL = strSQL & "' where 変更条件='" & .Cells(p, "C").Value & "'"
    
        'SQLの実行
        adoCn.Execute strSQL
    
        p = p + 1
    Loop
    
    End With

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?