LoginSignup
2
1

More than 5 years have passed since last update.

Windows 10 Pro x64 + Excel2016 x86 VBA から ODBC経由で SQLite3 にアクセスしてみる

Last updated at Posted at 2019-01-29

目的

SQLite3をインストールしたので、ExcelVBAからのアクセスを試してみる
ODBCを使用してアクセスする
32bit/64bit 版ODBCドライバー共に正常に使用できる仕様なんですね・・・

サンプルコード

'=================================================================
'
'   Excel2016 VBA -> SQLite3 からの読込サンプル
'   32bit/64bit 版ODBCドライバー共に正常に読み込めるのはなぜ?
'
'=================================================================
Private Sub ReadSqlite3()
On Error GoTo ERR_PROC

    Dim adoCon      As ADODB.Connection
    Dim adoRst      As New ADODB.Recordset
    Dim adoCmd      As New ADODB.Command
    Dim host        As String
    Dim driver      As String
    Dim dbname      As String
    Dim user        As String
    Dim pass        As String
    Dim conn        As String
'
    driver = "SQLite3 ODBC Driver"
    dbname = "C:\DEV\DB\sqlite3\demo.sqlite3"
'    host = "SQLITE3DEMO64"     ' sqliteodbc_w64.exe
    host = "SQLITE3DEMO32"      ' sqliteodbc.exe
    user = ""                   ' 未設定
    pass = ""                   ' 未設定
    conn = "DSN=" & host & ";UID=" & user & ";PWD=" & pass
    conn = "DRIVER={" & driver & "};DATABASE=" & dbname & ";UID=" & user & ";PWD=" & pass
'
    Set adoCon = New ADODB.Connection
    adoCon.ConnectionString = conn
    adoCon.CursorLocation = adUseClient
    Call adoCon.Open
'
    adoCmd.ActiveConnection = adoCon
    adoCmd.CommandText = "SELECT * FROM TEST_TABLE"
    adoCmd.CommandType = adCmdText
'
    Set adoRst = adoCmd.Execute
    While Not adoRst.EOF
        Debug.Print adoRst.Fields("ID") & " " & adoRst.Fields("VALUE")
        adoRst.MoveNext
    Wend
'
    adoCon.Close
    Set adoCon = Nothing
    Set adoRst = Nothing
    Set adoCmd = Nothing
    Exit Sub

ERR_PROC:
    adoCon.Close
    Set adoCon = Nothing
    Set adoRst = Nothing
    Set adoCmd = Nothing
End Sub

参考にしたのは以下のサイト

簡単なコマンドの作成と実行
対象テーブルは以下で作成したもの
Windows 10 Pro x64 で SQLite3を試してみる

2
1
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
2
1