Hide Sub-Form, See Module & Code Below

D

Dave Elliott

I tried this code in a module and called it on the onclick event of a
command button on my form to open the Main form with my sub-from on it.
If the control NameB IsNull , then I want to sub-from to be invisible, else
visible.
It is not working. meaning it is not hiding the sub-form Time_Hours using
the criteria provided





Function EnableCtl() ' This is my Module
On Error GoTo EnableCtl_Error

If IsNull(Forms!TimeCards!TimeID) Then
Forms!TimeCards!Time_Hours.Visible = False

Else:
Forms!TimeCards!Time_Hours.Visible = True

End If

EnableCtl_Exit:
Exit Function
EnableCtl_Error:
MsgBox "Unexpected error"
Resume EnableCtl_Exit
End Function


Private Sub Command72_Click() ' This code is on a command button on the
MASTER form which if clicked opens the main form w/ sub-form
On Error GoTo Err_Command72_Click
Dte1 = Forms!frmAutoPayrollReport!StartDate
Dte2 = Forms!frmAutoPayrollReport!EndDat
Call EnableCtl
DoCmd.OpenForm "TimeCards"

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
MsgBox err.Description
Resume Exit_Command72_Click

End Sub
 
K

Ken Snell [MVP]

Not sure if the problem is because the TimeID value isn't actually Null, so
try this:


Function EnableCtl() ' This is my Module
On Error GoTo EnableCtl_Error

If Len(Forms!TimeCards!TimeID & "") = 0 Then
Forms!TimeCards!Time_Hours.Visible = False

Else:
Forms!TimeCards!Time_Hours.Visible = True

End If

EnableCtl_Exit:
Exit Function
EnableCtl_Error:
MsgBox "Unexpected error"
Resume EnableCtl_Exit
End Function
 
S

SteveS

Dave said:
I tried this code in a module and called it on the onclick event of a
command button on my form to open the Main form with my sub-from on it.
If the control NameB IsNull , then I want to sub-from to be invisible, else
visible.
It is not working. meaning it is not hiding the sub-form Time_Hours using
the criteria provided





Function EnableCtl() ' This is my Module
On Error GoTo EnableCtl_Error

If IsNull(Forms!TimeCards!TimeID) Then
Forms!TimeCards!Time_Hours.Visible = False

Else:
Forms!TimeCards!Time_Hours.Visible = True

End If

EnableCtl_Exit:
Exit Function
EnableCtl_Error:
MsgBox "Unexpected error"
Resume EnableCtl_Exit
End Function


Private Sub Command72_Click() ' This code is on a command button on the
MASTER form which if clicked opens the main form w/ sub-form
On Error GoTo Err_Command72_Click
Dte1 = Forms!frmAutoPayrollReport!StartDate
Dte2 = Forms!frmAutoPayrollReport!EndDat
Call EnableCtl
DoCmd.OpenForm "TimeCards"

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
MsgBox err.Description
Resume Exit_Command72_Click

End Sub

It appears that you are trying to check a control on a form that is not
open.

Also, you say "If the control NameB IsNull...", but I don't see any
reference to "NameB" in your code.

Tracing the code, it looks like the command button is on form
"frmAutoPayrollReport". The button OnClick event sets two variables,
Dte1 and Dte2, to the values in controls StartDate and EndDate (you are
missing the last "e" in EndDate). Then you call a function to check the
value in "TimeID" on form "TimeCards". When the function ends, you then
open form "TimeCards".

The form "TimeCards" has to be open before you can check a control on
the form or change a property of a control on that form.

I would change the button code to:

Private Sub Command72_Click() ' This code is on a command button on
'the MASTER form which if clicked opens the main form w/ sub-form
On Error GoTo Err_Command72_Click
Dte1 = Forms!frmAutoPayrollReport!StartDate
Dte2 = Forms!frmAutoPayrollReport!EndDat
DoCmd.OpenForm "TimeCards"

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
MsgBox err.Description
Resume Exit_Command72_Click

End Sub


'--------------

'For form "TimeCards" On Open event I would use the following code to
set the visible property of the sub-form: (I didn't test this)

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Ctl_Error

' next line of code suggested by Ken Snell (MVP)
If Len(Forms!TimeCards!TimeID & "") = 0 Then
Forms!TimeCards!Time_Hours.Visible = False
Else
Forms!TimeCards!Time_Hours.Visible = True
End If

Ctl_Exit:
Exit Sub
Ctl_Error:
MsgBox "Unexpected error"
Resume Ctl_Exit
End Sub


HTH
 

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