Accessing a SQLite Database in VBA in Excel

后端 未结 3 777
有刺的猬
有刺的猬 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 22:58

    After failing to adapt this or this to my needs,I finally
    succeeded using Marcus Mangelsdorf's refinements from here.
    Not showing his code - it's in the link. I just put it in it's own module
    called "WSHreturn", changed it (along with the function Name/Args) to return the shell object and moved/added other code to mine. This is easy to set up quickly and will be the basis for a LINQ? type functionality.

    Sub VBALimposterQ()
        'With >1 field, SQLite default delim is Pipe "|"
        Const sqlInit As String = "c:\users\user\sqlite3.exe :memory:"
        Const sqlCreat As String = "CREATE Table Nums (n1 INTEGER NOT NULL, n2 INTEGER NOT NULL);"
        Const sqlIns0   As String = "INSERT INTO Nums VALUES (33,99);"
        Const sqlIns1   As String = "INSERT INTO Nums VALUES (11,22);"
        Const sqlIns2   As String = "INSERT INTO Nums VALUES (44,55);"
        Const sqlQry   As String = "SELECT RowId, n1, n2 from Nums;"
        Dim Ax, Axi, i, S
        Dim sqlShell As Object  'REF: Windows Script Host Object Model
        Set sqlShell = WSHreturn.getWShell(sqlInit) 'Qualifying Mssr. Mangelsdorf's code
        With sqlShell                   'with module name I gave it. 
            .StdIn.Write sqlCreat       'Thx Mathieu Guindon!
            .StdIn.Write sqlIns0
            .StdIn.Write sqlIns1
            .StdIn.Write sqlIns2
            .StdIn.Write sqlQry
            
            .StdIn.Close
            S = .StdOut.ReadAll
            Ax = Split(S, vbCrLf, , vbTextCompare)
            .Terminate
        End With
        
        For i = 0 To UBound(Ax)
            Axi = Ax(i)
            Debug.Print Axi
        Next i
    End Sub
    

提交回复
热议问题