How to return results of SQL query to Excel sheet with VBA function

2

1

I'm trying to create a VBA function in Excel to affect dynamic value from Access to Excel sheet. This is my code.

Public Function SQL(DB As String, Query As String)
    Dim conn As ADODB.Connection
    Set conn = New ADODB.Connection
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DB
    SQL = conn.Execute Query
End Function

Then I should write on my cell

=SQL("file.accdb", "SELECT Count(*) FROM Table")

The value returned is #VALUE!, but if I change the code to SQL = 1 + 2, then the value is 3. I believe this means the issue is with conn.Execute Query.

Matt

Posted 2015-04-15T19:33:39.917

Reputation: 21

Question was closed 2015-04-19T04:50:44.247

No answers