VBA with simple SQL query issue

Soundje

Registered User.
Local time
Today, 06:45
Joined
Aug 14, 2014
Messages
27
Hello There,

I am looking for some help or advice with the following use:


Code:
 Dim strQuery As String
strQuery = "SELECT DISTINCTROW tbl_data_leverancier.ContractID, Sum(tbl_data_leverancier.[Hoeveelheid Bruto]) AS [Som Van Hoeveelheid Bruto] FROM tbl_data_leverancier GROUP BY tbl_data_leverancier.ContractID HAVING (((tbl_data_leverancier.ContractID)=[Forms]![frml_contract_lev]![ContractID2]));"
    
 DoCmd.RunSQL strQuery
 Me.som_nettogewicht.Value = strQuery

Can anybody have a look to this I am not able to show the Query result

Many thanks in advanced !!

Kindly Regards,
Koen
 
RunSQL is for Action queries like INSERT, DELETE... etc. It's not for a SELECT query.

You can use a DLookup() function for what you want to do.
 
DoCmd.RunSQL is for action queries Insert, Update , Delete only.

Google this msaccess docmd.runsql

Also see this for SQL aggregate functions


Geeeez: vbaInet is there again.... faster than a speeding bullet....
 
Geeeez: vbaInet is there again.... faster than a speeding bullet....
I'm playing catchup. I've not been answering questions all day so I'm trying to get my brain in gear! :)
 
I'm playing catchup. I've not been answering questions all day so I'm trying to get my brain in gear!

I've been out all day as well. First post after supper here and bingo --there you are--- quicker than ever.
 
Thanks a lot vbaInet, I will try this, probarly stuppid question: since I need to calculate the sum of all selected values of 1 colum, is it possible to do this with Dlookup()? if yes could give me a very short example?

Thanks a lot,
Koen
 
I've been out all day as well. First post after supper here and bingo --there you are--- quicker than ever.
I did wait a few minutes though so you could have taken advantage :D

By the way, welcome to the forum Soundje!
 
I can see that the sum is already being calculated in the query so the OP can use just the DLookup to fetch [Som Van Hoeveelheid Bruto].
 
Ciao vbaInet,
Once more me sorry for this..

I tried the following code:


Private Sub TEST_Click()
str = DLoopup("SUM[Prijs]", "tbl_data_leverancier", "ContractID = 20141")
MsgBox str

End Sub

But I got a error saying:
Sub of the function is not defined .

Any ideas?

Thanks for all efforts,
Koen
 
Did you save the SQL statement you had in your first post as a query?
 
Yes I have the nam of the query is
que_som_data_leverancier

I am getting a littlebit confused sorry.. probarly due to the time :-)

Thanks
Koen
 
Time to sleep in Belgium and here too ;)

What's the name of the query? And the SQL statement in your first post is the same as what's in the query, nothing has changed?
 
I found it thanks to a cup of coffee :-)

str = DLookup("[Som Van Hoeveelheid Bruto]", "que_som_data_leverancier", "ContractID = 20141")

right? :-)
 
Yes, good job! But you know the criteria should be coming from your form right?
 
Thanks a lot for all help vbaInet, i couldn't make it without your triggers and help !!!
Really appriciated it !!!!

Have a nice evening/night,
Koen
 

Users who are viewing this thread

Back
Top Bottom