list box afterupdate to make control visible

J

johnlute

I'm trying to make a combo box visible/invisible depending on whether
or not a list box has a count or not. I'm trying this:

Private Sub lstDensity_AfterUpdate()
If Me.lstDensity.ListCount > 0 Then
Me.cbDensity.Visible = True
ElseIf Me.lstDensity.ListCount = 0 Then
Me.cbDensity.Visible = False
End If

End Sub

I also have it in the form's On Current event. The code works but only
if a selection is made in lstDensity. Without a selection cbDensity
remains invisible.

Have I written the code wrong? Does anyone see what I need to change?

Thanks!!!
 
D

Dirk Goldgar

johnlute said:
I'm trying to make a combo box visible/invisible depending on whether
or not a list box has a count or not. I'm trying this:

Private Sub lstDensity_AfterUpdate()
If Me.lstDensity.ListCount > 0 Then
Me.cbDensity.Visible = True
ElseIf Me.lstDensity.ListCount = 0 Then
Me.cbDensity.Visible = False
End If

End Sub

I also have it in the form's On Current event. The code works but only
if a selection is made in lstDensity. Without a selection cbDensity
remains invisible.

Have I written the code wrong? Does anyone see what I need to change?


John -

Please post the exact code you have in the form's Current event. Choosing
something in the list box should, in itself, make no difference to its
ListCount. Therefore, the only reason I can think of for it working in the
list box's AfterUpdate event and not in the form's Current event is that
either you don't execute the exact same code , or the list box is in fact
empty at the point in the form's Current event when you do it. Do you maybe
requery the list box in the Current event, but run the combo-visibility code
before you do that?

You shouId be able to simplify your code to just this (in any appropriate
event):

Me.cbDensity.Visible = (Me.lstDensity.ListCount > 0)

But if your original code isn't working, the simplification won't work
either until you fix whatever is wrong.
 
J

johnlute

Hi, Dirk! You're the man who never sleeps! This is actually a bit more
complex but you asked for it!

Ultimately, I'm making a selection in:
cbTargetFillFacIDs > AfterUpdate to make cbTargetFillLineIDs visible
or invisible > AfterUpdate that populates lstDensity > AfterUpdate
event that makes cbDensity visible or invisible. It's rather busy!

Here's the control source for lstDensity:
SELECT qryPROTargetFillWeightsCalculated.Density,
qryPROTargetFillWeightsCalculated.FGID,
qryPROTargetFillWeightsCalculated.numLocationAddressID,
qryPROTargetFillWeightsCalculated.LineID FROM
qryPROTargetFillWeightsCalculated GROUP BY
qryPROTargetFillWeightsCalculated.Density,
qryPROTargetFillWeightsCalculated.FGID,
qryPROTargetFillWeightsCalculated.numLocationAddressID,
qryPROTargetFillWeightsCalculated.LineID HAVING
(((qryPROTargetFillWeightsCalculated.Density) Is Not Null) AND
((qryPROTargetFillWeightsCalculated.FGID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbNavigateProfiles]) AND
((qryPROTargetFillWeightsCalculated.numLocationAddressID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillFacIDs]) AND
((qryPROTargetFillWeightsCalculated.LineID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillLineIDs]));

Private Sub Form_Current()
[cbTargetFillFacIDs].Requery
[cbTargetFillLineIDs].Requery
[cbDensity].Requery
[cbSTDEV].Requery
[cbThermoformFacIDs].Requery
[cbProfilesAssocsFacIDs].Requery
[cbProCodeFacIDs].Requery
[lstProCodeFacID].Requery
[lstTargetFillFacID].Requery
[lstDensity].Requery
[lstSTDEV].Requery
[lstThermPars].Requery

[cbTargetFillFacIDs] = Null
[cbTargetFillLineIDs] = Null
[cbThermoformFacIDs] = Null
[cbProfilesAssocsFacIDs] = Null
[cbProCodeFacIDs] = Null
[cbDensity] = Null
[cbSTDEV] = Null
[cbThermoformFacIDs] = Null
[cbThermoformLineIDs] = Null
[cbProfilesAssocsFacIDs] = Null
[cbProfilesAssocsFacIDsLineIDs] = Null

If Me.lstProCodeFacID.ListCount > 0 Then
Me.cbProCodeFacIDs.Visible = True
ElseIf Me.lstProCodeFacID.ListCount = 0 Then
Me.cbProCodeFacIDs.Visible = False
End If

If Me.lstTargetFillFacID.ListCount > 0 Then
Me.cbTargetFillFacIDs.Visible = True
ElseIf Me.lstTargetFillFacID.ListCount = 0 Then
Me.cbTargetFillFacIDs.Visible = False
End If

If IsNull(Me.cbTargetFillFacIDs) = True Then
Me.cbTargetFillLineIDs.Visible = False
ElseIf IsNull(Me.cbTargetFillFacIDs) = False Then
Me.cbTargetFillLineIDs.Visible = True
End If

If Me.lstDensity.ListCount > 0 Then
Me.cbDensity.Visible = True
ElseIf Me.lstDensity.ListCount = 0 Then
Me.cbDensity.Visible = False
End If

If Me.lstSTDEV.ListCount > 0 Then
Me.cbSTDEV.Visible = True
ElseIf Me.lstSTDEV.ListCount = 0 Then
Me.cbSTDEV.Visible = False
End If

If Me.lstThermPars.ListCount > 0 Then
Me.cbThermoformFacIDs.Visible = True
ElseIf Me.lstThermPars.ListCount = 0 Then
Me.cbThermoformFacIDs.Visible = False
End If

If IsNull(Me.cbThermoformFacIDs) = True Then
Me.cbThermoformLineIDs.Visible = False
ElseIf IsNull(Me.cbThermoformFacIDs) = False Then
Me.cbThermoformLineIDs.Visible = True
End If

End Sub
 
J

johnlute

I just ran through and simplified the current event to your suggestion
- thanks! - and of course, the problem persists however everything
else is working just fine:

Private Sub Form_Current()
[cbTargetFillFacIDs].Requery
[cbTargetFillLineIDs].Requery
[cbDensity].Requery
[cbSTDEV].Requery
[cbThermoformFacIDs].Requery
[cbProfilesAssocsFacIDs].Requery
[cbProCodeFacIDs].Requery
[lstProCodeFacID].Requery
[lstTargetFillFacID].Requery
[lstDensity].Requery
[lstSTDEV].Requery
[lstThermPars].Requery
[lstASSIDs].Requery

[cbTargetFillFacIDs] = Null
[cbTargetFillLineIDs] = Null
[cbThermoformFacIDs] = Null
[cbProfilesAssocsFacIDs] = Null
[cbProCodeFacIDs] = Null
[cbDensity] = Null
[cbSTDEV] = Null
[cbThermoformFacIDs] = Null
[cbThermoformLineIDs] = Null
[cbProfilesAssocsFacIDs] = Null
[cbProfilesAssocsFacIDsLineIDs] = Null

If IsNull(Me.cbTargetFillFacIDs) = True Then
Me.cbTargetFillLineIDs.Visible = False
ElseIf IsNull(Me.cbTargetFillFacIDs) = False Then
Me.cbTargetFillLineIDs.Visible = True
End If

Me.cbProCodeFacIDs.Visible = (Me.lstProCodeFacID.ListCount > 0)
Me.cbTargetFillFacIDs.Visible = (Me.lstTargetFillFacID.ListCount >
0)
Me.cbDensity.Visible = (Me.lstDensity.ListCount > 0)
Me.cbSTDEV.Visible = (Me.lstSTDEV.ListCount > 0)
Me.cbThermoformFacIDs.Visible = (Me.lstThermPars.ListCount > 0)

If IsNull(Me.cbThermoformFacIDs) = True Then
Me.cbThermoformLineIDs.Visible = False
ElseIf IsNull(Me.cbThermoformFacIDs) = False Then
Me.cbThermoformLineIDs.Visible = True
End If

Me.cbProfilesAssocsFacIDs.Visible = (Me.lstASSIDs.ListCount > 0)

If IsNull(cbProfilesAssocsFacIDs) = True Then
Me.cbProfilesAssocsFacIDsLineIDs.Visible = False
ElseIf IsNull(Me.cbProfilesAssocsFacIDs) = False Then
Me.cbProfilesAssocsFacIDsLineIDs.Visible = True
End If

End Sub
 
D

Dirk Goldgar

johnlute said:
Hi, Dirk! You're the man who never sleeps!

I should be asleep, but since I'm awake ...
Ultimately, I'm making a selection in:
cbTargetFillFacIDs > AfterUpdate to make cbTargetFillLineIDs visible
or invisible > AfterUpdate that populates lstDensity > AfterUpdate
event that makes cbDensity visible or invisible. It's rather busy!
Ooookay.

Here's the control source for lstDensity:
SELECT qryPROTargetFillWeightsCalculated.Density,
qryPROTargetFillWeightsCalculated.FGID,
qryPROTargetFillWeightsCalculated.numLocationAddressID,
qryPROTargetFillWeightsCalculated.LineID FROM
qryPROTargetFillWeightsCalculated GROUP BY
qryPROTargetFillWeightsCalculated.Density,
qryPROTargetFillWeightsCalculated.FGID,
qryPROTargetFillWeightsCalculated.numLocationAddressID,
qryPROTargetFillWeightsCalculated.LineID HAVING
(((qryPROTargetFillWeightsCalculated.Density) Is Not Null) AND
((qryPROTargetFillWeightsCalculated.FGID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbNavigateProfiles]) AND
((qryPROTargetFillWeightsCalculated.numLocationAddressID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillFacIDs]) AND
((qryPROTargetFillWeightsCalculated.LineID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillLineIDs]));

I'm curious. In the version of your application that I have previously
looked at, cbDensity has the above rowsource. Is that still the case? If
soIf so, why do you have a list box and a combo box with the same rowsource?

In above query, are you using the GROUP BY clause to remove duplicates?
Otherwise, I don't see any point to using GROUP BY. But your query would be
more efficient if you used a WHERE clause instead of HAVING:

SELECT
Density,
FGID,
numLocationAddressID,
LineID
FROM qryPROTargetFillWeightsCalculated
WHERE (Density Is Not Null)
AND
(FGID=[Forms]![frmQueryFGProcessingFacIDsLineIDs]![cbNavigateProfiles])
AND
(numLocationAddressID=[Forms]![frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillFacIDs])
AND
(LineID=[Forms]![frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillLineIDs])
GROUP BY
Density,
FGID,
numLocationAddressID,
LineID;
Private Sub Form_Current()
[cbTargetFillFacIDs].Requery
[cbTargetFillLineIDs].Requery
[cbDensity].Requery
[cbSTDEV].Requery
[cbThermoformFacIDs].Requery
[cbProfilesAssocsFacIDs].Requery
[cbProCodeFacIDs].Requery
[lstProCodeFacID].Requery
[lstTargetFillFacID].Requery
[lstDensity].Requery
[lstSTDEV].Requery
[lstThermPars].Requery

[cbTargetFillFacIDs] = Null
[cbTargetFillLineIDs] = Null
[cbThermoformFacIDs] = Null
[cbProfilesAssocsFacIDs] = Null
[cbProCodeFacIDs] = Null
[cbDensity] = Null
[cbSTDEV] = Null
[cbThermoformFacIDs] = Null
[cbThermoformLineIDs] = Null
[cbProfilesAssocsFacIDs] = Null
[cbProfilesAssocsFacIDsLineIDs] = Null [...]
End Sub

I'm not sure exactly what's going on, but the one thing I notice immediately
is that you are requerying a bunch of combo boxes, then requerying the list
boxes, and then setting those same combo boxes to Null. Among the combo
boxes being processed this way are the combo boxes, cbTargetFillFacIDs and
cbTargetFillLineIDs, that your list box's RowSource is using as criteria.
Therefore, when you requery the list box, those combo boxes will still have
the values they had before you requeried them -- even if those values are
not in their lists any more. They haven't been set to Null yet. Is that
your intention? I would have expected that you would mean those combos to
be Null, and then requery the list box based on that. If that's so, you'd
need to move assignment of Null to each combo box to a point in the code
before you requery anything that is based on that combo box.

I can also imagine that your AfterUpdate code for these combo boxes may need
to cascade down, so that when one control's value is changed, every control
that depends on that combo also gets requeried, and then every control that
depends on *those* controls gets requeried, and so on.

However, I may just be misunderstanding what you're trying to do.
 
J

johnlute

Hi, Dirk! You're the man who never sleeps!

I should be asleep, but since I'm awake ...
Ultimately, I'm making a selection in:
cbTargetFillFacIDs > AfterUpdate to make cbTargetFillLineIDs visible
or invisible > AfterUpdate that populates lstDensity > AfterUpdate
event that makes cbDensity visible or invisible. It's rather busy!
Ooookay.





Here's the control source for lstDensity:
SELECT qryPROTargetFillWeightsCalculated.Density,
qryPROTargetFillWeightsCalculated.FGID,
qryPROTargetFillWeightsCalculated.numLocationAddressID,
qryPROTargetFillWeightsCalculated.LineID FROM
qryPROTargetFillWeightsCalculated GROUP BY
qryPROTargetFillWeightsCalculated.Density,
qryPROTargetFillWeightsCalculated.FGID,
qryPROTargetFillWeightsCalculated.numLocationAddressID,
qryPROTargetFillWeightsCalculated.LineID HAVING
(((qryPROTargetFillWeightsCalculated.Density) Is Not Null) AND
((qryPROTargetFillWeightsCalculated.FGID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbNavigateProfiles]) AND
((qryPROTargetFillWeightsCalculated.numLocationAddressID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillFacIDs]) AND
((qryPROTargetFillWeightsCalculated.LineID)=[Forms]!
[frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillLineIDs]));

I'm curious.  In the version of your application that I have previously
looked at, cbDensity has the above rowsource.  Is that still the case?
Yes.

If so, why do you have a list box and a combo box with the same rowsource?

It's the only way I could figure out how to manage visible/invisible.
But now that I've reviewed your questions and overall design logic I
realize that I've been doing things the hard and painful way! Had I
learned about "ListCount" sooner I'd be much further ahead! I didn't
realize it could be used for combo boxes as well as list boxes.

Here was my logic PRIOR to this revelation. The form is used to filter
information via these controls and in the listed order:

1. [cbTargetFillFacIDs] per [cbNavigateProfiles]
2. [cbTargetFillLineIDs] per [cbNavigateProfiles] &
[cbTargetFillFacIDs]
3. [cbDensity] per [cbNavigateProfiles] & [cbTargetFillFacIDs] &
[cbTargetFillLineIDs]
4. [cbSTDEV] per [cbNavigateProfiles] & [cbTargetFillLineIDs]

First, I wanted to make [cbTargetFillFacIDs] invisible if there aren't
any records for it. I created the invisible [lstTargetFillFacID] with
the same query as [cbTargetFillFacIDs] and wrote its AfterUpdate:
Private Sub lstTargetFillFacID_AfterUpdate()
Me.cbTargetFillFacIDs.Visible = (Me.lstTargetFillFacID.ListCount >
0)

End Sub

This automatically makes [cbTargetFillFacIDs] visible or invisible
depending on whether or not it has records. Otherwise, the user will
have to click [cbTargetFillFacIDs] in order to see if there's a record
or not.

Well, now that I know better I've now eliminated [lstTargetFillFacID]
and added this to [cbTargetFillFacIDs] AfterUpdate and the form's
current:

Me.cbTargetFillFacIDs.Visible = (Me.cbTargetFillFacIDs.ListCount > 0)

This works just fine. I'm going to revise them all now and see if I
have any problems. Good grief. Talk about wasted days and wasted
nights. Ultimately, a tough lesson learned and those are the ones that
stay with you.
In above query, are you using the GROUP BY clause to remove duplicates?
Yes.

Otherwise, I don't see any point to using GROUP BY.  But your query would be
more efficient if you used a WHERE clause instead of HAVING:

    SELECT
        Density,
        FGID,
        numLocationAddressID,
        LineID
    FROM qryPROTargetFillWeightsCalculated
    WHERE (Density Is Not Null)
         AND
(FGID=[Forms]![frmQueryFGProcessingFacIDsLineIDs]![cbNavigateProfiles])
         AND
(numLocationAddressID=[Forms]![frmQueryFGProcessingFacIDsLineIDs]![cbTarget­FillFacIDs])
         AND
(LineID=[Forms]![frmQueryFGProcessingFacIDsLineIDs]![cbTargetFillLineIDs])
    GROUP BY
        Density,
        FGID,
        numLocationAddressID,
        LineID;

I'll look into this next - thanks!
I'm not sure exactly what's going on,

Can I blame you? What a mess to try and figure out!
but the one thing I notice immediately
is that you are requerying a bunch of combo boxes, then requerying the list
boxes, and then setting those same combo boxes to Null.

Not any more!
However, I may just be misunderstanding what you're trying to do.

Amazingly, you were grasping this mess! I'm going to clean it up and
let you know.

Should I thank you or your insomnia?
 
D

Dirk Goldgar

johnlute said:
Well, now that I know better I've now eliminated [lstTargetFillFacID] and
added this to [cbTargetFillFacIDs] AfterUpdate and the form's current:

Me.cbTargetFillFacIDs.Visible = (Me.cbTargetFillFacIDs.ListCount > 0)

Note: if a combo or list box has its ColumnHeads property set to True ("Yes"
on the property sheet), the header row will be included in the ListCount.
So in that case, your code should be:

Me.cbTargetFillFacIDs.Visible = (Me.cbTargetFillFacIDs.ListCount > 1)

Or you could use this general-purpose version, which will work regardless of
the ColumnHeads setting:

With Me.cbTargetFillFacIDs
.Visible = (.ListCount > Abs(.ColumnHeads))
End With
 
J

johnlute

Hi, Dirk -

Oddly, I wasn't having any issues with the > 0 despite the controls
having ColumnHeads as true. I went ahead and changed them all to > 1
anyway. The following now works as desired - WHEW!!!

Private Sub Form_Current()
[cbProCodeFacIDs].Requery
[cbTargetFillFacIDs].Requery
[cbTargetFillLineIDs].Requery
[cbDensity].Requery
[cbSTDEV].Requery
[cbThermoformFacIDs].Requery
[cbThermoformLineIDs].Requery
[cbProfilesAssocsFacIDs].Requery
[cbProfilesAssocsFacIDsLineIDs].Requery

Me.cbProCodeFacIDs.Enabled = (Me.cbProCodeFacIDs.ListCount > 1)
Me.ProcessingCodesFrame.Enabled = (Me.cbProCodeFacIDs.ListCount >
1)
Me.cbTargetFillFacIDs.Enabled = (Me.cbTargetFillFacIDs.ListCount >
1)
Me.TargetFillWeightsFrame.Enabled =
(Me.cbTargetFillFacIDs.ListCount > 1)
Me.cbTargetFillLineIDs.Enabled = (Me.cbTargetFillLineIDs.ListCount
Me.cbDensity.Enabled = (Me.cbDensity.ListCount > 1)
Me.CalculatedFrame.Enabled = (Me.cbDensity.Enabled = True)
Me.cbSTDEV.Enabled = (Me.cbSTDEV.ListCount > 1)
Me.cbThermoformFacIDs.Enabled = (Me.cbThermoformFacIDs.ListCount >
1)
Me.ThermoformFrame.Enabled = (Me.cbThermoformFacIDs.ListCount > 1)
Me.cbThermoformLineIDs.Enabled = (Me.cbThermoformLineIDs.ListCount
Me.cbProfilesAssocsFacIDs.Enabled =
(Me.cbProfilesAssocsFacIDs.ListCount > 1)
Me.AssociationsLineIDsFrame.Enabled =
(Me.cbProfilesAssocsFacIDs.ListCount > 1)
Me.cbProfilesAssocsFacIDsLineIDs.Enabled =
(Me.cbProfilesAssocsFacIDsLineIDs.ListCount > 1)

[cbProCodeFacIDs] = Null
[cbTargetFillFacIDs] = Null
[cbTargetFillLineIDs] = Null
[cbDensity] = Null
[cbSTDEV] = Null
[cbThermoformFacIDs] = Null
[cbThermoformLineIDs] = Null
[cbProfilesAssocsFacIDs] = Null
[cbProfilesAssocsFacIDsLineIDs] = Null

End Sub

I don't know if this is any help to anyone else but as always -
THANKS!

Well, now that I know better I've now eliminated [lstTargetFillFacID] and
added this to [cbTargetFillFacIDs] AfterUpdate and the form's current:
Me.cbTargetFillFacIDs.Visible = (Me.cbTargetFillFacIDs.ListCount > 0)

Note: if a combo or list box has its ColumnHeads property set to True ("Yes"
on the property sheet), the header row will be included in the ListCount.
So in that case, your code should be:

     Me.cbTargetFillFacIDs.Visible = (Me.cbTargetFillFacIDs.ListCount > 1)

Or you could use this general-purpose version, which will work regardlessof
the ColumnHeads setting:

    With Me.cbTargetFillFacIDs
        .Visible = (.ListCount > Abs(.ColumnHeads))
    End With

--
Dirk Goldgar, MS Access MVP
Access tips:www.datagnostics.com/tips.html

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top