Passing a variable into a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm sure this is very easy, but I can't figure it out.

I have a form that allows people to filter by selecting an entry in a combo.
In certain cases this form is opened from another form, where that selection
has already been made. So I did...

AcctID = Me!accountId
DoCmd.OpenForm "frmPrices", acNormal
DoCmd.Maximize
Forms!frmPrices("AccountCombo") = AcctID

But this does not work. Various other guesses at the syntax for the last
line also fail. Yes, "AccountCombo" is a real control on the form in question.

Maury
 
Maury Markowitz said:
I'm sure this is very easy, but I can't figure it out.

I have a form that allows people to filter by selecting an entry in a
combo. In certain cases this form is opened from another form, where
that selection has already been made. So I did...

AcctID = Me!accountId
DoCmd.OpenForm "frmPrices", acNormal
DoCmd.Maximize
Forms!frmPrices("AccountCombo") = AcctID

But this does not work. Various other guesses at the syntax for the
last
line also fail. Yes, "AccountCombo" is a real control on the form in
question.


"Does not work" in what sense? Error message? Doesn't set the combo?
Sets the combo but doesn't filter the form? If it's the last one, I
wouildn't be surprised, because the filtering action is probably done by
code in the combo box's AfterUpdate event, and that event won't fire if
the value is modified by code.

If the filtering process is complicated, you can make it a Public sub or
function on the form -- "Public" is the key -- and call it from your
code, qualifying it by a reference to the form like this:

Forms!frmPrices.FilterForAccount

On the other hand, if it's a simple filter, you can apply it when you
open the form by using the WhereCondition parameter of the OpenForm
method:

AcctID = Me!accountId
DoCmd.OpenForm "frmPrices", acNormal, _
WhereCondition:="AcctID=" & AcctID
DoCmd.Maximize
Forms!frmPrices("AccountCombo") = AcctID

(or something like that).

Or, as yet another alternative, you could pass the AcctID to the form
via the OpenArgs parameter of te OpenForm method, and then have code in
the form's Open or Load event to set the value of the combo box and
apply the filter (possibly by calling the combo's AfterUpdate event
procedure).

That's three different approaches, but I'll grant you they're all based
on my assumption that it's just the application of the filter that isn't
working.
 
Dirk Goldgar said:
"Does not work" in what sense? Error message? Doesn't set the combo?

The later. I assume from the way you worded the content that what I'm doing
should work, so maybe there's some sort of interaction going on.

The form in question as an OnOpen which sets the recordsource. I only do
this because I couldn't figure out a better way to do a where, but below you
suggest an answer...
AcctID = Me!accountId
DoCmd.OpenForm "frmPrices", acNormal, _
WhereCondition:="AcctID=" & AcctID

I assume in this case it's actually setting what the Properties panel calls
the "Filter"?

I'm trying this now, but the combo remains empty and the filter does not
seem to get applied. I'll keep poking.

Maury
 
Maury Markowitz said:
The later. I assume from the way you worded the content that what I'm
doing should work, so maybe there's some sort of interaction going on.

The form in question as an OnOpen which sets the recordsource. I only
do this because I couldn't figure out a better way to do a where, but
below you suggest an answer...


I assume in this case it's actually setting what the Properties panel
calls the "Filter"?
Yes.

I'm trying this now, but the combo remains empty and the filter does
not seem to get applied. I'll keep poking.

If you're setting the form's recordsource in the Open event, I don't
think any filter you apply via OpenForm -- or any time before you set
the recordsource -- is going to be retained.

Make sure that you use the correct field name in the WhereCondition.
Also, if that's a text field, use quotes around it, as in:

WhereCondition:="AcctID='" & AcctID & "'"

Using the WhereCondition isn't going to set the value of the combo box.
You'll have to do that with your code. I'm assuming, by the way, that
this combo box is unbound, since you're using it for filtering.
 
Ahhh, found it. The form was imported from an MBD, and hidden in one of the
combos was a DISTINCTROW. This caused an internal unreported error, which
stopped the Open from working properly.

I only noticed this because the combo in question was empty!

Maury
 
Maury Markowitz said:
Ahhh, found it. The form was imported from an MBD, and hidden in one
of the combos was a DISTINCTROW. This caused an internal unreported
error, which stopped the Open from working properly.

Unreported error? We don't like those.

Can I take it that things are working okay now?

BTW, I'd recommend mentioning up front, every time you start a new
thread, that you're working with an ADP. In some cases that could make
a difference. For example, some events have a different sequence in
ADPs than in MDBs.
 
Back
Top