The proper ways of showing and hiding forms and reports. (3 Viewers)

MarlonVisser0408

New member
Local time
Tomorrow, 00:54
Joined
Mar 4, 2025
Messages
28
I've been playing around with VBA and the button macro to create a more application like experience. What I'm trying to do is make the right side respond to the actions in the menu. For example, if I want to add someone new to the list, I want it to close or hide the current open form and to open or unhide the form that adds someone to the list.

What I'm curious to, is what the proper ways are to make this happen? I was hoping that the macro would allow it through the IF -> SetProperty function and playing with the visible property. However, it suddenly started breaking on me telling me: "Object or class does not support the set of events". Considering that I've ran into this on my own and started using it to solve my problems, I've started wondering if there are not better or more proper ways of handling this.

*Note* I'm still playing around with the gray real estate. But no matter if I put the form inside another form that holds it own menu; it shouldn't really matter.

Please, if you reply with ways of doing this; don't be afraid to give a little information. I'm pretty new to the coding/macro side of Access and only been tipping my toes for a little while.
1747986188853.png
 
*Note* I'm still playing around with the gray real estate. But no matter if I put the form inside another form that holds it own menu; it shouldn't really matter.
But it does. As you nest objects, you change the way you have to refer to them. But, for your purpose, you are not referring to the form itself, you are referring to the form control that holds the form.

Welcome to AWF. When you post questions, especially about code, it is imperative that you post your current code so we can see what you did.

If you are trying to do this with macros, let me suggest that you step away from macros and switch to using VBA. It is far easier than you think.

How you refer to an object depends on where your code is running. If the buttons are sitting directly on the main form and the gray boxes are subform controls also sitting on the main form then the button code would be:

Me.sfrmControlName1.Visible = False --- to hide
Me.sfrmControlName3.Visible = True --- to unhide

to reverse

Me.sfrmControlName3.Visible = False --- to hide
Me.sfrmControlName1.Visible = True --- to unhide
 
I would not alter the visibility of the subform control. Rather I would programmatically change the subform currently hosted by the subform control by changing the subform control's .SourceObject property.
PHP:
Private Sub cmdContact_Click()
    Me.sfm.SourceObject = "fContactSFM"
End Sub
 
I would not alter the visibility of the subform control
It depends on your objective. If you don't need to go back to the place where you were, then both methods work. If you need to retain the status of the subform, then you need to hide it because eliminating it will lose its current status.
 
I would not alter the visibility of the subform control. Rather I would programmatically change the subform currently hosted by the subform control by changing the subform control's .SourceObject property.
PHP:
Private Sub cmdContact_Click()
    Me.sfm.SourceObject = "fContactSFM"
End Sub
Oef, that sounds really sexy. I basically got 2 'sizes' of subforms, of which the big majority is the same size.


But it does. As you nest objects, you change the way you have to refer to them. But, for your purpose, you are not referring to the form itself, you are referring to the form control that holds the form.

Welcome to AWF. When you post questions, especially about code, it is imperative that you post your current code so we can see what you did.

If you are trying to do this with macros, let me suggest that you step away from macros and switch to using VBA. It is far easier than you think.

How you refer to an object depends on where your code is running. If the buttons are sitting directly on the main form and the gray boxes are subform controls also sitting on the main form then the button code would be:

Me.sfrmControlName1.Visible = False --- to hide
Me.sfrmControlName3.Visible = True --- to unhide

to reverse

Me.sfrmControlName3.Visible = False --- to hide
Me.sfrmControlName1.Visible = True --- to unhide
Yeah, I figured at first to try and macro it. The way it's shown really resonates with how I look at organizing, so it's hard to get lost. Doing this in VBA would add a lot more functionality though.

Let me see if I can get the control to change, that would also mean that I don't have 20 pages over each other, I recon that this will hurt the database a lot more then doing it through swapping out the sourcecode.
 
2 'sizes' of subforms, of which the big majority is the same size.
Then you are only worrying about showing and hiding the two subform controls and setting the source object.

I would make a single VBA function called ShowSubform and pass in the name of the subform from the buttons and tell it to show the big or small subformcontrol. So each button calls the procedure

Code:
Private SomeButton_Click()
  showSubForm "YourFormName", "Big"
end sub


Public Sub ShowSubForm(SubFormName as string, BigOrSmall as string)
  if BigOrSmall = "Big" then
    me.subFrmBig.visible = true
    me.subfrmSmall.visible = false
   me.subfrmBig.sourceObject = subformname
 elseif BigOrSmall = "Small"
    me.subFrmBig.visible = false
    me.subfrmSmall.visible = true
   me.subfrmSmall.sourceobject = subformname
 end if
end sub
 
Last edited:
Or do smaller chunks, because HideUnload() might be generally useful, and if the button click handler knows the control, pass the control directly.
Code:
Private cmdContact_Click()
  ShowSubForm "fContactSFM", Me.sfmBig
End Sub

Public Sub ShowSubForm(src As String, sfm As Access.Subform)
    HideUnload
    sfm.SourceObject = src
    sfm.Visible = True
End Sub

Private Sub HideUnload()
    Dim sfm As Access.Subform
    For Each sfm In Array(Me.sfmBig, Me.sfmSmall)
        sfm.Visible = False
        sfm.SourceObject = ""
    Next
End Sub
 

Users who are viewing this thread

Back
Top Bottom