sql help

buggy2

Board Regular
Joined
Feb 28, 2003
Messages
69
I am running the following sql statement in my code by I keep getting an error telling me that this is not a valid sql statement, however when I run a query using the statement it is fine.


DoCmd.RunSQL ("SELECT Count([Master].[Name]) FROM [Master] GROUP BY [Master].[PLACE], [Master].TDATE HAVING ((([Master].[PLACE])=[Forms]![board].[Form]![cmbo_place]) AND (([ABC Master].TDATE)=[Forms]![board].[Form]![cmbo_tdate]));")
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
RunSQL Method only executes ACTION queries (like UPDATE, INSERT ONLY etc..)

You have a SELECT query. You can use something like this:

Code:
Currentdb.OpenRecordSet ("SELECT Count([Master].[Name]) FROM [Master] GROUP BY [Master].[PLACE], [Master].TDATE HAVING ((([Master].[PLACE])=[Forms]![board].[Form]![cmbo_place]) AND (([ABC Master].TDATE)=[Forms]![board].[Form]![cmbo_tdate]));").Fields(0).value

or may be a stored query in database. Or anything else but not RunSQL method.

I hope it helps.
Suat
 
Upvote 0
sql

I have already tried your suggestion but it resulted in an 'invalid use of property' error relating to '.Fields.Value'.

Do you know any other way around this?.
 
Upvote 0
Already tried ?

So you already knew that it returns a value so you should use it like :

SOMEVAR = Currentdb.OpenRecordSet ("SELECT Count([Master].[Name]) FROM [Master] GROUP BY [Master].[PLACE], [Master].TDATE HAVING ((([Master].[PLACE])=[Forms]![board].[Form]![cmbo_place]) AND (([ABC Master].TDATE)=[Forms]![board].[Form]![cmbo_tdate]));").Fields(0).value

Good luck.
 
Upvote 0
You can try something like this:

Code:
Option Explicit

Sub CreateSelectQuery()
    Dim qdf As DAO.QueryDef
    Dim db As DAO.Database
    Dim strSQL As String
    
    Set db = CurrentDb
    
    strSQL = "SELECT Count([Master].[Name]) " & _
            "FROM [Master] " & _
            "GROUP BY [Master].[PLACE], [Master].TDATE " & _
            "HAVING ((([Master].[PLACE])=[Forms]![board].[Form]![cmbo_place]) " & _
            "AND (([ABC Master].TDATE)=[Forms]![board].[Form]![cmbo_tdate]));"
    
    Set qdf = db.CreateQueryDef("qryNew", strSQL)
    DoCmd.OpenQuery "qryNew"
    
    ' if you don't want to keep the query,
    ' you can do something like this:
    
    db.QueryDefs.Delete ("qryNew")
    
    Set qdf = Nothing
    Set db = Nothing
End Sub

Note that if you are using Access97 you don't need the "DAO."

You may need to set a reference to "Microsoft DAO X.x Object Library" in Tools-References.

HTH,

Russell
 
Upvote 0
SQL.

thats code work perfectly and i can build other queries around it. I knew '.Fields(Item).Value' returned a value but even if assign it to a variable you still cant use it in the context i need it, it is an invalid use of the .value property.

Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,357
Members
451,642
Latest member
mirofa

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