Accessing a SQLite Database in VBA in Excel

后端 未结 3 772
有刺的猬
有刺的猬 2020-12-09 22:14

I have been adding an MS Access database to VBA in order to conduct some analysis of ships. However the database has now changed to SQlite, which I have no idea how to acces

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 23:07

    Great solution, thanks Parfait!

    Just one small quick correction, you actually need to make:

    rst.Open strSQL, conn, 1, 1
    

    This way, the complete solution would be:

    Dim conn As Object, rst As Object
    
    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")
    
    ' OPEN CONNECTION
    conn.Open "DRIVER=SQLite3 ODBC Driver;Database=C:\Path\To\SQLite\Database.db;"
    
    strSQL = "SELECT Vessels.vsl_name, Vessels.dwt FROM Vessels GROUP BY Vessels.vsl_name, Vessels.dwt ORDER BY Vessels.vsl_name ;"
    
    ' OPEN RECORDSET
    rst.Open strSQL, conn, 1, 1
    
    ' OUTPUT TO WORKSHEET
    Worksheets("results").Range("A1").CopyFromRecordset rst
    rst.Close
    
    ' FREE RESOURCES
    Set rst = Nothing: Set conn = Nothing
    

    This will make rst contain the entire table you got from the query.

提交回复
热议问题