Replacing a Form Name with a Variable

AccessV2.0

New member
Local time
Today, 18:01
Joined
Jun 3, 2022
Messages
9
In the OnActivate function of my form I have the following statement:

Forms!IP_Log.IP_Log_SubForm.Visible = False

how do I replace IP_Log.IP_LogSubForm with a variable?

IP_Log is the Main Form
IP_LogSubForm is the SubForm Name
 
In the OnActivate function of my form I have the following statement:

Forms!IP_Log.IP_Log_SubForm.Visible = False

how do I replace IP_Log.IP_LogSubForm with a variable?

IP_Log is the Main Form
IP_LogSubForm is the SubForm Name
Perhaps something like:
Code:
Dim obj As Object

   Set obj = Me.NameOfSubFormControl

obj.Visible = False
 
dim f
f = "frmMyform
docmd.openform f

or if using a combo box on the form:
docmd.openform cboBox
 
You actually only need

IP_LogSubForm.visible = false
 
Forms!IP_Log.IP_Log_SubForm.Visible = False

how do I replace IP_Log.IP_LogSubForm with a variable?
I'm slightly surprised by the other replies here.

Anyways...
You cannot replace that with a single variable. You need at least two; one for the form name and one for the subform (control) name.

Code:
Dim FormName as String
Dim SubFormControlName as String

FormName = "IP_Log"
SubFormControlName = "IP_LogSubForm"

Forms(FormName).Controls(SubFormControlName).Visible = False
 
Because the most common and logical requirement for wanting to replace a hard-coded form/control name with a variable is when the same code should be able to handle multiple different forms/controls. - None of the previous answers addressed this requirement in any way.
 
@AccessV2.0 Kind of curious why you want to do this?
Surely it's not because you want to copy/paste this to numerous forms' Activate events, hiding them all, is it?
 
knowing the why would be helpful. It can't be to save code since you would still need to assign the object
 
Perhaps something like:
Code:
Dim obj As Object

   Set obj = Me.NameOfSubFormControl

obj.Visible = False
That worked! But how
knowing the why would be helpful. It can't be to save code since you would still need to assign the object
The reason for this is to create a function which can be used in many different forms to control access to a given form.subform; if I can use a variable to call out the form.subform then a cleaver naming convention can make this possible.
 
I'm slightly surprised by the other replies here.

Anyways...
You cannot replace that with a single variable. You need at least two; one for the form name and one for the subform (control) name.

Code:
Dim FormName as String
Dim SubFormControlName as String

FormName = "IP_Log"
SubFormControlName = "IP_LogSubForm"

Forms(FormName).Controls(SubFormControlName).Visible = False
This is the code I needed!
I send the variable to the function as:
MyFunction(Form.Name)

The function then creates the subform name:
SubFormName = Form.Name & "_SubForm"

then use:
Forms(FormName).Controls(SubFormControlName).Visible = False

to control whatever feature I want to control.

I can now use MyFunction(Form.Name) in any form as long as I maintain the proper naming convention of form to subform.
 
Glad you have a solution - just don’t see how that saves you time and/or effort
 
I would be more inclined to use the control Tag property and a public sub
No need to maintain any naming conventions.

Code:
Public Sub HideMe(frm As Form, StrTag As String)
    Dim ctl As Control

    For Each ctl In frm.Controls
        If InStr(1, ctl.Tag, StrTag) Then
            ctl.Visible = False
        End If
    Next

End Sub

simply call it in your form like below and it will hide any control with that tag value.

Code:
HideMe Me,"SomeString"

By using Instr() in the code you can have many strings in the tag property should you need to do more than one thing.

I would also use the onLoad event or onCurrent event rather than the activate event.
 
Last edited:
I can now control access to a subform with a simple OnActivate command:
Secure(Me.Name)

which executes:
Function Secure(AForm As String) 'This routine runs a procedure to set the Access Level for any form
Dim Var1 'Security Access Level
Dim SubForm As String
SubForm = AForm & "_SubForm" 'Create SubForm name
Var1 = AccessLevel(AForm) 'Get Security Level for the form

Select Case Var1
Case 0 'Security Level 0 Does not allow user to open a form
Forms(AForm).Controls(SubForm).Visible = False
Case 1 'Security Level 1 allows user READ-ONLY Access to a form
Forms(AForm).Controls(SubForm).Enabled = False
Case 2 'Security Level 2 allows user READWRITE Access to a form
Forms(AForm).Controls(SubForm).Enabled = True
Case Else 'Other Security Levels have no security designation presently, defalut to NO ACCESS
Forms(FormName).Controls(SubFormControlName).Visible = False
End Select
End Function
.
That's efficient enough to go forward with.
 
you could just use

Forms(AForm).Controls(SubForm).Enabled = AccessLevel(AForm)=2
 

Users who are viewing this thread

Back
Top Bottom