last day (15 days later) » 

5:57 PM
0
Q: Queries can't access controls on subforms

organic agaveI have a form, let's call it Form1, that contains a text field text1 and a button that runs a query Query1. Query1 just does SELECT Forms!Form1!text1. Now, if I were to create a navigation form and drag Form1 in it, then I have a subform and Query1 breaks. One way I can get it to work is to mod...

 
There is a syntax for referring to parent/child; it is somewhat complex. The master reference is access.mvps.org/access/forms/frm0031.htm
If further help needed, please post the code for your button. The relationship between code, form and query is not entirely clear.
 
@Smandoli the link you posted seems to be VBA syntax. For the trivial example in my question I could use VBA to build a SQL string and execute it. However, in reality I have complex queries in the Access database that I want to leverage and it's not feasible to redo everything in terms of VBA. Let me know if I'm misinterpreting.
 
Correct about the VBA context. But SELECT Forms!Form1!text1 AS Expr1 is a mash of SQL and Object/VBA syntax. It's hard to tell what you are doing!
 
I don't know what the idiomatic way to reference forms in Access SQL is but that query runs fine (no VBA involved.) It just pulls the value from Form1!text1. I guess what I want is something like Me.text1 in the query and Access can understand that Me is the subform object where the query is called.
 
5:57 PM
@Smandoli I briefly looked at that question. Is the idea to convert my Query1 into one that takes parameters (if that's possible) and pass the parameters I want from the text field in my subform to the query via VBA?
oh hey
 
Ha. Chat. We'll give it a try. Yes, a parameter query is the ready-to-go solution -- and it is intended to solve issues without using VBA. I think you can see this in the answers for the other post. (Which I admit are not conclusive.)
Have you used a param qry? Say, with no reference to a form
 
this is what I've got so far
my query is now
parameters foo char;
SELECT foo AS Expr1;
and my button macro now says
Private Sub Command3_Click()
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("Query1")
qdf.Parameters("Forms!Form1!foo").Value = Me!foo
qdf.Execute
Set qdf = Nothing
End Sub
 
Ah. You have used SQL in another RDBMS
 
so that's all good
and of course i get the error that i can't execute a select statement
but in my access database
i have other queries that depend on query1
 
So, with NO VBA ... you are used to the SQL view in Access query interface?
Enterin with SQL text?
 
6:00 PM
yeah i look at the sql view mostly as i'm very new to access and picking this job up from someone else
i'm an R programmer
 
So enter: SELECT [put entry here] AS Expr1;
I'm not sure it will work without a table base, but let's see
SELECT [put entry here] AS My_Variable, My_Key FROM MyTable;
 
hold on
what are we planning to do now
 
This is the syntax fo param query
It will ask you for the var ... but you can also make it linked to a form
 
oh are we trying to do what i did with VBA, but without VBA?
 
Yes. And BTW I think your VBA is not correct, will not function -- can address that if needed
 
6:04 PM
okay got it
 
BUT... I cannot at all claim this will give you the flexy you ultimately want. I do think you are going to be in for some VBA. But thsi will be a needed building block.
 
so i have that code in the query
mytable obviously doesn't exist, but continue
 
For all VBA, include Option Explicit at top of every module and then run Debug>Compile. This will help you with the syntax
@organicagave right -- pseudo code
 
yeah i'm somewhat familiar with VBA, and i was able to step through and watch the variables update with my code above
 
Hey that's great
Stepping from R -- an achievement
Anyway... I think the elegant and skilled solution uses QueryDef. My usual solution does... hmmm... it's been a while
So QueryDef is it :-)
In truth, I still can't visualize what you are doing... perhaps more important, why.
To top everything off, my lunch break is over
 
6:08 PM
okay let me give some more background
haha
all right
 
go on a bity
 
right now the flow looks like data |> query1 |> query 2|> export query (to excel)
the queries reference controls on a form
everything works fine
but i want to create a navigation form
 
k
 
and put my form in there
so i did that
but now the references in my queries are broken
 
How do you trigger the export to Excel?
 
6:10 PM
Excel is pulling from the access exported table
but that's not important to my problem
 
So you initiate from Excel, then. okay
 
i want the queries to be able to reference the subform they're called from
i can change the code in all my queries to do that, but then if i open the form outside of the navgiation form, it won't work
i'd like to be able to use the form as standalone and also as a subform within a navigation form
 
Well, Access doesn't do javascript-style drag-n-drop. This sounds like a pivot-chart, something very dynamic. I take it i's not a matter of (tediously) coding in the half-dozen versions you need
 
maybe that's not good practice and hence why i'm having so much trouble
 
Ah! Re-use the forms
Then, gloat gloat, you are stuck with VBA again for sure
I have been coerced into VBA -- sometimes quite ambitious -- many times FOR PUIRPOSE OF re-usnig a form a report -- so important, at times, to not Repeat Yourself
 
6:14 PM
is there a way for queries to reference the form object they're called from?
 
thinking
 
i think that would solve all my issues but i don't know if access supports that
 
Yes. You build a param query. Then you put the "qualified form reference" -- the control, precisely -- into the input slot. But to alter this, it's back to QueryDef.
I can think of a scheme...
meh
 
i think querydef makes a lot of sense, but the issue is that query2 depensd on query1... i'll keep thinking
 
Oh that is solvab;e
Both queries reference for form, eh?
Isn't that just two QueryDefs?
 
6:17 PM
and query2 references the result from query1
 
But they both draw from the form? If so, two QDs. Otherwise, 1 QD.
 
okay let's suppose query2 only references query1 but not the form
we set the parameters of query1 via QD
 
Remember, the QD doesn't change the name of the form. A dependent query doesn't know if the criteria for its predecessor has changed
 
what do we do next
 
You can test all this with no reference to forms...
Sim the whole thing... make a hand-entry at the right point, to satify the parameter
 
6:20 PM
the qd object for query1 is invisible to query2
 
true
Hey, it looks like my "Dupl questoin" was not correct, so I'll remove that, eh?
 
k and i'll update my OP to reflect what i've learned
 
deal
signing off -- have a great success
 

last day (15 days later) »