SQL in VBA help

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
114
HI, I am having issues with my current SQL Statement.

my immediate window is throwing the current error: "-2147217900:Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'."

I am trying to determine the number of records in the Dates table based on two fields.


VBA Code:
Private Sub cmdSelectBySQL_Click()
    On Error GoTo Catch

    Dim strSql As String
    Dim cn As ADODB.Connection
    Dim objRst As ADODB.Recordset
    Dim lngCount As Long
    Dim project As String
    Dim report As String
    
    report = Sheets("Master").Range("f5")
    project = Sheets("Master").Range("f6")

    
    '' Get all categories
    strSql = "SELECT Count(*) FROM Dates HAVING (((([Dates].[Project_Number]))='" & project & "' And (([Dates].[Report_Name]))='" & report & "'));"
    
    Set objRst = New ADODB.Recordset
    objRst.Open strSql, cn.Connection, adOpenForwardOnly
    lngCount = objRst!cnt
    
    Debug.Print "Record count: " & lngCount
        
    '' Clean up
    objRst.Close
    Set objRst = Nothing
        
    Exit Sub
    
Catch:
    MsgBox "cmdSelectBySQL_Click()" & vbCrLf & vbCrLf & _
           "Error#:  " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub

Thank you,
Patrick
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
In SQL, you do not use "HAVING" clauses, unless you are creating Aggregate Queries (which would have a "GROUP BY" clause).
Try changing "HAVING" to "WHERE".
 
Upvote 0
In SQL, you do not use "HAVING" clauses, unless you are creating Aggregate Queries (which would have a "GROUP BY" clause).
Try changing "HAVING" to "WHERE".

Updated and i received this pop-up error. Not sure which object is referenced incorrectly.
 

Attachments

  • Capture.JPG
    Capture.JPG
    20 KB · Views: 11
Upvote 0
Try putting a break point near the top of your code, and test again.
When it puts you inside of the code, use the F8 to go through the code line-by-line, and see what line of code it is erroring out on.
 
Upvote 0
Try putting a break point near the top of your code, and test again.
When it puts you inside of the code, use the F8 to go through the code line-by-line, and see what line of code it is erroring out on.

Failing on this line, I don't believe I am properly reference my database:

VBA Code:
    objRst.Open strSql, cn.Connection, adOpenForwardOnly



I forgot to include my database connection at the top.
VBA Code:
Public Const con1 As String = "PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=R:\Development Team Database\Development Team.accdb;PERSIST SECURITY INFO=FALSE;Jet "
 
Upvote 0

Forum statistics

Threads
1,215,891
Messages
6,127,602
Members
449,388
Latest member
macca_18380

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top