B
BruceM
I have a form with a subform, based on one-to-many tables. After some
struggle and a newsgroup search I came up with a way of hiding the subform
if there are no records, and at the same time making a command button and a
label visible. The following is in the main form's Current event:
Me.subformcontrol.Visible =
Me.subformcontrol.Form.RecordsetClone.RecordCount > 0
Me.cmdAdd.Visible = Not Me.subformcontrol.Form.RecordsetClone.RecordCount >
0
Me.lblAdd.Visible = Not Me.subformcontrol.Form.RecordsetClone.RecordCount >
0
It doesn't quite work as I would like in that if the record at which the
form opens (the last record, by design) shows all of the controls,
regardless of whether or not there is a record in the subform record source.
Once I start navigating through the records it works as intended, with
either the subform control visible or the other two controls (cmdAdd and
lblAdd) visible, but not both. I suppose I could try adding code to the
form's Open event or Activate event or something, but two questions: Would
that work, and is it necessary to have redundant code?
Another issue: the code generated Error #2455 "You entered an expression
that has an invalid reference to the property Form/Report" in the main
form's Current event (custom error handling revealed the information in a
message box). When I clicked OK the form opened and performed as expected,
other than the issue described above, so I trapped the error, but what's up
with that? I get a little nervous sometimes about trapping errors when I
don't understand why they appeared in the first place.
Some VBA questions: RecordSet and RecordSetClone both seem to work in the
above code. I used RecordSetClone because it was in the code I found in my
search, and I knew the person who provided the code to be a reputable
source. Can anybody clarify?
Also, I tried an If statement in the code:
If Me.subformcontrol.Form.RecordsetClone.RecordCount > 0 Then
Me.cmdAdd.Visible = False
End If
It didn't work, but I would have thought it would accomplish the same thing
as the code I posted near the beginning of this message. What am I not
understanding here?
struggle and a newsgroup search I came up with a way of hiding the subform
if there are no records, and at the same time making a command button and a
label visible. The following is in the main form's Current event:
Me.subformcontrol.Visible =
Me.subformcontrol.Form.RecordsetClone.RecordCount > 0
Me.cmdAdd.Visible = Not Me.subformcontrol.Form.RecordsetClone.RecordCount >
0
Me.lblAdd.Visible = Not Me.subformcontrol.Form.RecordsetClone.RecordCount >
0
It doesn't quite work as I would like in that if the record at which the
form opens (the last record, by design) shows all of the controls,
regardless of whether or not there is a record in the subform record source.
Once I start navigating through the records it works as intended, with
either the subform control visible or the other two controls (cmdAdd and
lblAdd) visible, but not both. I suppose I could try adding code to the
form's Open event or Activate event or something, but two questions: Would
that work, and is it necessary to have redundant code?
Another issue: the code generated Error #2455 "You entered an expression
that has an invalid reference to the property Form/Report" in the main
form's Current event (custom error handling revealed the information in a
message box). When I clicked OK the form opened and performed as expected,
other than the issue described above, so I trapped the error, but what's up
with that? I get a little nervous sometimes about trapping errors when I
don't understand why they appeared in the first place.
Some VBA questions: RecordSet and RecordSetClone both seem to work in the
above code. I used RecordSetClone because it was in the code I found in my
search, and I knew the person who provided the code to be a reputable
source. Can anybody clarify?
Also, I tried an If statement in the code:
If Me.subformcontrol.Form.RecordsetClone.RecordCount > 0 Then
Me.cmdAdd.Visible = False
End If
It didn't work, but I would have thought it would accomplish the same thing
as the code I posted near the beginning of this message. What am I not
understanding here?