Hide a form button

  • Thread starter Thread starter espee2
  • Start date Start date
E

espee2

I have a form with a sub-form. The sub-form has a button on it, I want
to be able to hide the button only when it's in Form w/ sub-form
view. the button will not work when in form w/sub-form view, (as
intended) It will work when I view the sub-form by itself. so I want
to be able to see the button when I view just the sub-form alone. how
do I do that? (is that possible?) either bu code or arranging the
layout...
 
Here is a function that I use to do that.

Public Function TopParent(frmNow As Form) As Form
' Author : Terry Kreft
' Purpose : Returns the top parent form for a subform
' or the form itself if there is no parent
'Usage example:
' Dim frmParent As Form
'
' Set frmParent = TopParent(Me)
' 'Debug.Print frmParent.Name
' If frmParent.Name = Me.Name Then
' 'I'm not a subform
' End If
On Error GoTo Err_Handler

Dim frmParent As Form

Const ERR_NO_PARENT As Long = 2452 'Invalid reference to the Parent
property.

Do
Set frmParent = frmNow.Parent
'Debug.Print frmParent.Name
Set frmNow = frmParent
DoEvents
Loop

Exit_Handler:
Set TopParent = frmNow
Exit Function

Err_Handler:
Select Case Err
Case ERR_NO_PARENT
Case Else
Call fnFormErrHandler(pstrMdl, Err)
End Select
Resume Exit_Handler

End Function


Jeanette Cunningham MVP (Access) Pakenham Victoria Australia

I have a form with a sub-form. The sub-form has a button on it, I want
to be able to hide the button only when it's in Form w/ sub-form
view. the button will not work when in form w/sub-form view, (as
intended) It will work when I view the sub-form by itself. so I want
to be able to see the button when I view just the sub-form alone. how
do I do that? (is that possible?) either bu code or arranging the
layout...
 
Here is a function that I use to do that.

Public Function TopParent(frmNow As Form) As Form
' Author    : Terry Kreft
' Purpose   : Returns the top parent form for a subform
'             or the form itself if there is no parent
'Usage example:
'    Dim frmParent As Form
'
'    Set frmParent = TopParent(Me)
'    'Debug.Print frmParent.Name
'    If frmParent.Name = Me.Name Then
'        'I'm not a subform
'    End If
On Error GoTo Err_Handler

    Dim frmParent As Form

  Const ERR_NO_PARENT As Long = 2452 'Invalid reference to the Parent
property.

    Do
        Set frmParent = frmNow.Parent
        'Debug.Print frmParent.Name
        Set frmNow = frmParent
        DoEvents
    Loop

Exit_Handler:
    Set TopParent = frmNow
Exit Function

Err_Handler:
    Select Case Err
    Case ERR_NO_PARENT
    Case Else
      Call fnFormErrHandler(pstrMdl, Err)
    End Select
    Resume Exit_Handler

End Function

Jeanette Cunningham  MVP (Access) Pakenham Victoria Australia


I have a form with a sub-form. The sub-form has a button on it, I want
to be able to hide the button only when it's in Form w/ sub-form
view.  the button will not work when in form w/sub-form view, (as
intended) It will work when I view the sub-form by itself. so I want
to be able to see the button when I view just the sub-form alone. how
do I do that? (is that possible?) either bu code or arranging the
layout...

Where do I enter that (certainly not "On Click") which 'Event' do I
enter it in" also do I have to edit the code, like where it says
'Parent', change it to my form name?
 
I can't get it to work, I have no idea where to put the code... I've
tried in "Enter" seemed logical to me, tried changing Parent.Name to
Parent.My Form Name... Please elaborate, thanks
 
m:
I have a form with a sub-form. The sub-form has a button on it, I
want to be able to hide the button only when it's in Form w/
sub-form view. the button will not work when in form w/sub-form
view, (as intended) It will work when I view the sub-form by
itself. so I want to be able to see the button when I view just
the sub-form alone. how do I do that? (is that possible?) either
bu code or arranging the layout...

I have forms like this that get used as both a standalone form and
as a subform, and that have different behaviors in each context. I
wrote an IsSubform() function. It was actually quite difficult, as
my original approach (using Me.Parent and trapping the error if
there was none) didn't work very well (it caused problems during the
unloading of forms during app shutdown), so I ended up with the code
after my signature.

I think this function will work even if you have the form open both
as a standalone form and as subform, and it will also work if you
have multiple instances of the standalone form (since each has it's
own specific hWnd).

I'm not sure there's any need for an error handler here, since I
can't quite conceive of any error that could happen, but it's there
just in case (actually a leftover from the previous version which
trapped the Me.Parent error).

--
David W. Fenton http://www.dfenton.com/
contact via website only http://www.dfenton.com/DFA/

Public Function IsSubForm(frm As Form) As Boolean
On Error GoTo errHandler
Dim frmLoop As Form
Dim bolIsMainForm As Boolean

For Each frmLoop In Forms
If frmLoop.Name = frm.Name Then
If frmLoop.hWnd = frm.hWnd Then
bolIsMainForm = True
Exit For
End If
End If
Next frmLoop
Set frmLoop = Nothing
IsSubForm = Not bolIsMainForm

exitRoutine:
Exit Function

errHandler:
MsgBox err.Number & ": " & err.Description, vbExclamation, _
"Error in IsSubForm()"
Resume exitRoutine
End Function
 
I have forms like this that get used as both a standalone form and
as a subform, and that have different behaviors in each context. I
wrote an IsSubform() function. It was actually quite difficult, as
my original approach (using Me.Parent and trapping the error if
there was none) didn't work very well (it caused problems during the
unloading of forms during app shutdown), so I ended up with the code
after my signature.

I think this function will work even if you have the form open both
as a standalone form and as subform, and it will also work if you
have multiple instances of the standalone form (since each has it's
own specific hWnd).

I'm not sure there's any need for an error handler here, since I
can't quite conceive of any error that could happen, but it's there
just in case (actually a leftover from the previous version which
trapped the Me.Parent error).

--
David W. Fenton                  http://www.dfenton.com/
contact via website only    http://www.dfenton.com/DFA/

Public Function IsSubForm(frm As Form) As Boolean
On Error GoTo errHandler
  Dim frmLoop As Form
  Dim bolIsMainForm As Boolean

  For Each frmLoop In Forms
    If frmLoop.Name = frm.Name Then
       If frmLoop.hWnd = frm.hWnd Then
          bolIsMainForm = True
          Exit For
       End If
    End If
  Next frmLoop
  Set frmLoop = Nothing
  IsSubForm = Not bolIsMainForm

exitRoutine:
  Exit Function

errHandler:
  MsgBox err.Number & ": " & err.Description, vbExclamation, _
      "Error in IsSubForm()"
  Resume exitRoutine
End Function

I still don't know where to put the code, I'm assuming... the button I
want to hide>properties>Event> but where from there, and do I need to
change any of the code to reflect my form (if so what part?)?
 
The code called Public Function TopParent(frmNow As Form) As Form, goes in a
standard module, you can name the module as modUtility.
You can use the code in the Load event of the subform, or if that doesn't
work, use it in the Current event of the subform.


Private Sub Form_Load()
On Error Go To SubErr
Dim frmParent As Form

Set frmParent = TopParent(Me)
'Debug.Print frmParent.Name
If frmParent.Name = Me.Name Then
'I'm not a subform, code here to make the button visible
Else
'code here to hide the button
End If

SubExit:
On Error Resume Next
Set frmParent = Nothing
Exit Sub

SubErr:
Select Case Err.Number
Case 438, 2467
Resume Next
Case Else
MsgBox Err.Number & " " & Err.Description
Resume SubErr
End Select
End Sub


Jeanette Cunningham MVP (Access) Pakenham Victoria Australia


I can't get it to work, I have no idea where to put the code... I've
tried in "Enter" seemed logical to me, tried changing Parent.Name to
Parent.My Form Name... Please elaborate, thanks
 
The code called Public Function TopParent(frmNow As Form) As Form, goes in a
standard module, you can name the module as modUtility.
You can use the code in the Load event of the subform, or if that doesn't
work, use it in the Current event of the subform.

Private Sub Form_Load()
On Error Go To SubErr
Dim frmParent As Form

    Set frmParent = TopParent(Me)
    'Debug.Print frmParent.Name
    If frmParent.Name = Me.Name Then
        'I'm not a subform, code here to make the button visible
    Else
        'code here to hide the button
    End If

SubExit:
    On Error Resume Next
    Set frmParent = Nothing
    Exit Sub

SubErr:
   Select Case Err.Number
    Case 438, 2467
        Resume Next
    Case Else
        MsgBox Err.Number & " " & Err.Description
        Resume SubErr
    End Select
End Sub

Jeanette Cunningham  MVP (Access) Pakenham Victoria Australia


I can't get it to work, I have no idea where to put the code... I've
tried in "Enter" seemed logical to me, tried changing Parent.Name to
Parent.My Form Name... Please elaborate, thanks

Please bear with me, I am only a novice and am guessing at what you
mean by moduals and such... I opened my said Sub-form, opened VB,
clicked a new module, entered the original code in your first post,
named it modUtility. now when I enter either the first code you sent
me or the second code you sent me into Form> On Load I get an error it
expects an ending to sub and the yellow arrow points at the first line
of code (that was there when the window opened before I added any
code)

You said ..."You can use the code in the Load event of the
subform"... then you gave me a new code. did you mean ..."use this
code"... (the second code) because I'm confused now, no matter what
code I use, no matter where I put it, I get a Syntax error, yellow hi-
lite with arrow first line of code, and the ...On Error Go To
SubErr... line is hi-litted in red letters... Am I not putting the
right thing in Load or Current?
 
I scoured the Google and found this... it works great and was easy for
an idiot like me to understand...

....quote] "try doing it in the
subform's Open event procedure. This should show only those controls
with a
Tag property of 'sfrm' when the form is used as a subform, and all
controls
when it is opened independently:

Dim frm As Form
Dim ctrl As Control

On Error Resume Next
Set frm = Forms(Me.Name)
If Err.Number <> 0 Then
On Error GoTo 0
For Each ctrl In Me.Controls
ctrl.Visible = (ctrl.Tag = "sfrm")
Next ctrl
End If

It works by trying to return a reference to the form as a member of
the Forms
collection, which is not the case when a form is used as a subform of
course,
so an error occurs and the code to hide/show the controls is
executed. The
error handling in the above is simplified of course, but should work.
Ideally it should test for the specific error.

I have tried it with a subform in continuous forms view and it appears
to work
fine. I have not tried it in datasheet view.

Ken Sheridan
Stafford, England


--
Message posted via AccessMonster.com
"....[end
quote
 
m:
I still don't know where to put the code, I'm assuming... the
button I want to hide>properties>Event> but where from there, and
do I need to change any of the code to reflect my form (if so what
part?)?

You'd put the code to hide the button in the form's OnLoad event.
Using my function, it would be something like this in OnLoad event:

Me!cmdMyCommandButton.Visible = Not IsSubForm(Me)

I may have reversed the logic you need, but the code above will show
the button when the form is open standalone, and hide it when the
form is open as a subform of another form.
 
If frmParent.Name = Me.Name Then

This approach is dangerous, as I discovered in trying to use it. The
problem is that if the form where this code is called is open when
Access exits, you get a non-trappable error. I tried to fix this
over the course of several months, trying everything to close the
form in an orderly method and all that, but could not get rid of the
error. The only way to get rid of it was to use some other method of
figuring out if the form is a subform or not (I posted the code in
another message yesterday).
 
You'd put the code to hide the button in the form's OnLoad event.
Using my function, it would be something like this in OnLoad event:

  Me!cmdMyCommandButton.Visible = Not IsSubForm(Me)

I may have reversed the logic you need, but the code above will show
the button when the form is open standalone, and hide it when the
form is open as a subform of another form.

The above code renders an Error:..."Compile error, Sub or Function not
defined" then IsSubForm is hi-lited...
 
That's interesting to know.
I do use the function TopParent(frmNow As Form) extensively in my databases,
however I don't usually use If frmParent.Name = Me.Name Then.
I sometimes use If frmParentName = "NameOfForm" Then.
I must be lucky to have avoided errors so far.

Jeanette Cunningham

If frmParent.Name = Me.Name Then

This approach is dangerous, as I discovered in trying to use it. The
problem is that if the form where this code is called is open when
Access exits, you get a non-trappable error. I tried to fix this
over the course of several months, trying everything to close the
form in an orderly method and all that, but could not get rid of the
error. The only way to get rid of it was to use some other method of
figuring out if the form is a subform or not (I posted the code in
another message yesterday).
 
Glad you found something you could understand and coule get it to work.


Jeanette Cunningham MVP (Access) Pakenham Victoria Australia
I scoured the Google and found this... it works great and was easy for
an idiot like me to understand...

....quote] "try doing it in the
subform's Open event procedure. This should show only those controls
with a
Tag property of 'sfrm' when the form is used as a subform, and all
controls
when it is opened independently:

Dim frm As Form
Dim ctrl As Control

On Error Resume Next
Set frm = Forms(Me.Name)
If Err.Number <> 0 Then
On Error GoTo 0
For Each ctrl In Me.Controls
ctrl.Visible = (ctrl.Tag = "sfrm")
Next ctrl
End If

It works by trying to return a reference to the form as a member of
the Forms
collection, which is not the case when a form is used as a subform of
course,
so an error occurs and the code to hide/show the controls is
executed. The
error handling in the above is simplified of course, but should work.
Ideally it should test for the specific error.

I have tried it with a subform in continuous forms view and it appears
to work
fine. I have not tried it in datasheet view.

Ken Sheridan
Stafford, England


--
Message posted via AccessMonster.com
"....[end
quote
 
:
The above code renders an Error:..."Compile error, Sub or Function
not defined" then IsSubForm is hi-lited...

Well, did you copy the IsSubForm code into a public module, or into
the module of your subform? If not, how did you expect it to be
used?
 
Well, did you copy the IsSubForm code into a public module, or into
the module of your subform? If not, how did you expect it to be
used?


I put it in the Forms OnLoad event just as prescribed...

...."You'd put the code to hide the button in the form's OnLoad event.
Using my function, it would be something like this in OnLoad event:
Me!cmdMyCommandButton.Visible = Not IsSubForm(Me) "...

I surely appreciate the help!!.. but not everybody has a PHD in this
subject and it's obvious after a few posts I might need a little more
guidance as to the where and how to put codes. It get frustrating when
you put it exactly where you were told to, get an error and then are
told you did it wrong... The remedy I posted above in post #8 from
searching Google, that guy explained in one post... where to put the
code, a code that works, and how it works... I learn a lot from these
forums and newsgroups and run into these situations all the time where
the "Experts" take it for granted that everybody is on the same level.
My advice for the MVP's is when it's obvious someone is a novice,
"throw 'em a frickin' bone" (Please) Thank you...
 
m:
I put it in the Forms OnLoad event just as prescribed...

..."You'd put the code to hide the button in the form's OnLoad
event. Using my function, it would be something like this in
OnLoad event:
Me!cmdMyCommandButton.Visible = Not IsSubForm(Me) "...

But I was assuming you undestood that you can't call a function that
you've not created in your application. That is, the code for the
IsSubForm() function has to be in your app.
I surely appreciate the help!!.. but not everybody has a PHD in
this subject

You don't have to have a PhD to understand things this basic.
and it's obvious after a few posts I might need a little more
guidance as to the where and how to put codes. It get frustrating
when you put it exactly where you were told to, get an error and
then are told you did it wrong...

It's not my fault that you skipped the first part of the
instructions.
The remedy I posted above in post #8 from
searching Google, that guy explained in one post... where to put
the code, a code that works, and how it works... I learn a lot
from these forums and newsgroups and run into these situations all
the time where the "Experts" take it for granted that everybody is
on the same level. My advice for the MVP's is when it's obvious
someone is a novice, "throw 'em a frickin' bone" (Please) Thank
you...

My advice to you is to hire a professional and stop trying to do it
yourself.

And quit whining.
 
Back
Top