Passing form name

P

Pastor Del

I found this code (EnableFormControls) in the forum to enable/disable
controls on a form but I'm having trouble setting the variable 'frmAny' and
passing it to the sub EnableFormControls. What am I doing wrong?

Sub EnableFormControls(frmAny As Form, _
strControlSkip As String, _
Optional tfEnable As Boolean = True)
Dim ctlAny As Control
On Error GoTo ERROR_Handler
frmAny(strControlSkip).SetFocus
For Each ctlAny In frmAny.Controls
If ctlAny.Name <> strControlSkip Then
Select Case ctlAny.ControlType
Case acCheckBox, acComboBox, acCommandButton _
, acListBox, acOptionGroup, acSubform _
, acTextBox, acToggleButton
ctlAny.Enabled = tfEnable
End Select
End If
Next ctlAny
Exit Sub

ERROR_Handler:
If Err.Number = 2164 Then
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description, , _
"Error in EnableFormControls"
End If
End Sub

Private Sub Command1_Click()
Dim frmAny As Form
Dim strControlSkip As String
Dim tfEnable As Boolean


frmAny = Form2
strControlSkip = "Text2"
tfEnable = False

EnableFormControls frmAny, strControlSkip, tfEnable
End Sub
 
S

Stuart McCall

Pastor Del said:
I found this code (EnableFormControls) in the forum to enable/disable
controls on a form but I'm having trouble setting the variable 'frmAny'
and
passing it to the sub EnableFormControls. What am I doing wrong?

Sub EnableFormControls(frmAny As Form, _
strControlSkip As String, _
Optional tfEnable As Boolean = True)
Dim ctlAny As Control
On Error GoTo ERROR_Handler
frmAny(strControlSkip).SetFocus
For Each ctlAny In frmAny.Controls
If ctlAny.Name <> strControlSkip Then
Select Case ctlAny.ControlType
Case acCheckBox, acComboBox, acCommandButton _
, acListBox, acOptionGroup, acSubform _
, acTextBox, acToggleButton
ctlAny.Enabled = tfEnable
End Select
End If
Next ctlAny
Exit Sub

ERROR_Handler:
If Err.Number = 2164 Then
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description, , _
"Error in EnableFormControls"
End If
End Sub

Private Sub Command1_Click()
Dim frmAny As Form
Dim strControlSkip As String
Dim tfEnable As Boolean


frmAny = Form2
strControlSkip = "Text2"
tfEnable = False

EnableFormControls frmAny, strControlSkip, tfEnable
End Sub

First off, the form must be open for this code to work. Also you need to
reference the form via the Forms collection, like this:

frmAny = Forms!Form2
 
D

DrGUI

See if this helps. Instead of passing it as a form, I suggest passing the
form name as string:

Sub EnableFormControls(strFrmAny As string, _
strControlSkip As String, _
Optional tfEnable As Boolean = True)

Dim ctlAny As Control
On Error GoTo ERROR_Handler

Forms(strFrmAny).(strControlSkip).SetFocus

For Each ctlAny In Forms(strFrmAny).Controls
If ctlAny.Name <> strControlSkip Then
Select Case ctlAny.ControlType
Case acCheckBox, acComboBox, acCommandButton, _
acListBox, acOptionGroup, acSubform, _
acTextBox, acToggleButton
ctlAny.Enabled = tfEnable
End Select
End If
Next ctlAny

Exit Sub

ERROR_Handler:
If Err.Number = 2164 Then
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description, , _
"Error in EnableFormControls"
End If
End Sub

Private Sub Command1_Click()
Dim strFrmAny As string
Dim strControlSkip As String
Dim tfEnable As Boolean


strFrmAny = "frmAny"
strControlSkip = "Text2"
tfEnable = False

EnableFormControls strFrmAny, strControlSkip, tfEnable

End Sub
 
P

Pastor Del

I tried 'frmAny = Me' without success. DrGUI suggests that I try passing the
name as a string. I'll probably go that way because I know I can pass a
string, but for the sake of greater understanding I'd like to know why I
can't pass it as a form. Any ideas?
 
P

Pastor Del

The form was open and I tried 'frmAny = Forms!Form2' without success. DrGUI
suggests that I try passing the name as a string. I'll probably go that way
because I know I can pass a string, but for the sake of greater understanding
I'd like to know why I can't pass it as a form. Any ideas?
 
P

Pastor Del

Thanks, I'm sure I can pass the string & I'll probably do it this way. But,
can you explain why I couldn't pass it as a form, just for a better
understanding of the beast?
 
S

Stuart McCall

Pastor Del said:
The form was open and I tried 'frmAny = Forms!Form2' without success.

My bad. Apologies. The line should read:

Set frmAny = Forms!Form2

When you're assigning an object, such as a form or report to a variable, you
need to use the Set keyword.
 
C

ChrisO

I tried 'frmAny = Me' without success.<< is not very helpful in determining
the problem. There may be other problems which are preventing it from
working. However: -

Sub EnableFormControls(frmAny As Form, _
strControlSkip As String, _
Optional tfEnable As Boolean = True)

is expecting a reference (pointer) to be passed to it and Me should refer to
the Form in which Private Sub Command1_Click() is running. Since Private Sub
Command1_Click() is running, and doesn’t close the Form, then the Form must
be open and Me must be valid.

I did not test Sub EnableFormControls at all but it does, as written,
require a reference to a Form, not its name. If you are trying to use it on a
sub Form from a parent Form then pass a reference to the sub Form with
Me.YourSubFormControlNameGoesHere.Form. That also is untested.

More information about the failure is required, please.
 
P

Pastor Del

Stuart pointed out my problem. When you're assigning an object, such as a
form or report to a variable, you need to use the Set keyword. The code I
initially posted works great when the Set keyword in used.

Thanks for you time and attention
 
P

Pastor Del

Thanks, it works great with the Set keyword. I'm self-taught and had not
come across this kind of situation before. Thanks again.
 
C

ChrisO

Yes, Stuart is correct and I should have tested the code.

As a matter of interest: -

Private Sub Command1_Click()
Dim frmAny As Form
Dim strControlSkip As String
Dim tfEnable As Boolean

Set frmAny = Me
strControlSkip = "Text2"
tfEnable = False

EnableFormControls frmAny, strControlSkip, tfEnable

End Sub

can be reduced to: -

Private Sub Command1_Click()

EnableFormControls Me, "Text2", False

End Sub

which removes the problem.
 

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