Solved Display certain fields in subform depending on condition (1 Viewer)

Kayleigh

Member
Local time
Today, 23:06
Joined
Sep 24, 2020
Messages
706
Hi,
I have a form which has several subforms (tab control). I would like to only display certain fields if a field is set to specific value. However the fields are in a subform so it is difficult to refer to it. Can anyone suggest how to do this?
(I was thinking of putting a public function into subform and then calling it on the current event?)
Cheers,
Krayna
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:06
Joined
Oct 29, 2018
Messages
21,358
Hi. Not sure what specifically you were having problems with, but the syntax to refer to a control on a subform goes something like this:

Forms!MainFormName.SubformControlName.Form!ControlName

Make sure to use the name of the subform control rather than the form assigned to it as the Source Object.

Hope that helps...
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:06
Joined
May 21, 2018
Messages
8,463
To hide a subform control

Me.NameOfSubFormControl.Form.NameOfControl.visible

Make sure not to forget the .form after the name of the subform control. Often the name of the subform control is the same as the form inside the subform control, but double check. This is not always the case.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:06
Joined
May 21, 2018
Messages
8,463
Beat by a couple seconds.
 

Kayleigh

Member
Local time
Today, 23:06
Joined
Sep 24, 2020
Messages
706
Thank you.

Would you mind to look at the component of DB working on now - there are several subforms which I would like to be able to ref in the onCurrent of the main form.
Would like the work placement and medical tab to only display if fields in that subform are not null. Can you help me with coding this?

Sorry file too large to send so this is link to drive folder containing DB: StudentsTest

Many thanks,
Krayna
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:06
Joined
May 21, 2018
Messages
8,463
Try this

hide/show subform
Code:
Me.sbfrmChildWork.Visible = (GetRecordCount(Me.sbfrmChildWork.Form.RecordsetClone) <> 0)

or hide/show the entire page
Code:
'Me.TabCtl18.Pages("Work Placement").Visible = (GetRecordCount(Me.sbfrmChildWork.Form.RecordsetClone) <> 0)


add line for each subfrm control to show hide

add this function just to make sure you get an accurate record count. May be overkill, but better safe

Code:
Public Function GetRecordCount(RstRecords As DAO.Recordset) As Integer
  If RstRecords.EOF Then
    GetRecordCount = 0
  Else
    RstRecords.MoveLast
    GetRecordCount = RstRecords.RecordCount
  End If
  'MsgBox GetRecordCount
End Function
 
Last edited:

Kayleigh

Member
Local time
Today, 23:06
Joined
Sep 24, 2020
Messages
706
That code worked fine.

Wanted to add a command button which can display tab page if no records in associated table. Button would be hidden if page is displayed. This is code used in onCurrent event:
Code:
If GetRecordCount(Me.sbfrmStudentWork.Form.RecordsetClone) <> 0 Then
Me.StudentTabs.Pages("Work Placement").Visible = True
Me.cmdDisplayWork.Visible = False
End If
If GetRecordCount(Me.sbfrmStudentExAgency.Form.RecordsetClone) <> 0 Then
Me.StudentTabs.Pages("External Agencies").Visible = True
Me.cmdDisplayAgencies.Visible = False
End If
But this seems repetition? (Tried doing both in IF function but the page is always visible)
This is code in onClick event of command buttons:
Code:
Private Sub cmdDisplayWork_Click()
Me.StudentTabs.Pages("Work Placement").Visible = True
'Me.cmdDisplayWork.Visible = False
End Sub
However an error occurs when running this - code to change visibility to false is flagging an error though not sure why?
 

Kayleigh

Member
Local time
Today, 23:06
Joined
Sep 24, 2020
Messages
706
Solved my problem by setting focus to another control and then had no problem changing visible to false!
 

Users who are viewing this thread

Top Bottom