Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query based on two tables.

Status
Not open for further replies.

daffelito

Technical User
Joined
Sep 18, 2008
Messages
7
Location
SE
Hi I am trying to get an value to an unbound textbox in a form, but something in my query just returns the "query statement" it selfe in the textbox.

it looks like this:

Code:
Me.Pumptyp.Value = "SELECT tblPumpsorter.Pumpnamn" & _
                   "FROM tblPumpsorter" & _
                   "WHERE tblPumpsorter.id =" & _
                   "(SELECT tblPumpgropar.Pumpsort FROM tblPumpgropar" & _
                   "WHERE tblPumpgropar.Pumpgrop = '" & Me.Pumpgrop.Value & "');"
So the value i get in the textbox is: "SELECT tblPump.... etc"
 
G'day fella,

A good effort but things don't work that way! The easiest solution based on your example is this as the control source of your control:

Code:
=dlookup("Pumpnamn", "tblPumpsorter", id =" & dlookup("Pumpsort", "tblPumpgropar", "Pumpgrop = '" & Me!Pumpgrop & "'"))

or, for tidyness and to help you see what's going on:

Code:
dim txtPumpsort as string

txtPumpsort=dlookup("Pumpsort", "tblPumpgropar", "Pumpgrop = '" & Me!Pumpgrop & "'")

Me!pumptyp=dlookup("Pumpnamn", "tblPumpsorter", id =" & txtPumpsort

Have a great weekend, good luck!

JB

 
Create a query named, say, qryPumptyp:
Code:
SELECT S.Pumpnamn, G.Pumpgrop
FROM tblPumpsorter AS S INNER JOIN tblPumpgropar AS G ON S.id = G.Pumpsort

Then, in your form:
Code:
Me!Pumptyp.Value = DLookUp("Pumpnamn", "qryPumptyp", "Pumpgrop='" & Me!Pumpgrop.Value & "'")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ahh, thank you mate!

That worked like a charm.
I´m quite a newbee to this so it's kind of hard to know when to use SQL or when to use VBA.
 
no drama, happy to help. As you're new to this, I'll just mention there are other domain aggregate functions you may find use for that work in the same way as DLookup: DSum, DCount spring immediately to mind, check the help file for more...

And if you wish to execute an action query (rather than a select as you were attempting previously) look at docmd.runSQL or db.execute methods. And docmd.setwarnings false turns of the potentially scary-to-user warning about deleting, appending, updating records.

Here endeth the 1 minute crash course of "SQL thingies in VBA" ;)


JB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top