Solved Code Error

Niroshana

New member
Local time
Today, 18:12
Joined
Jul 10, 2020
Messages
10
Hi

The following SUB is not working. Hence please help me to correct the Code.


Public Sub USERACTV(Frm As Form, ANum As Integer)

If ANum = 1 Then

Forms(Frm.Name).Controls(btnNew).Enable = True

Else

Forms(Frm.Name).Controls(btnNew).Enable = False

End If

End Sub
 
Walk through the code with F8 after setting a breakpoint on the If statement.

Where is variable btnNew declared/populated?
 
This would work
Code:
Public Sub USERACTV(Frm As Form, ANum As Integer)
    If ANum = 1 Then
        Forms(Frm.Name).Controls("btnNew").Enabled = True
    Else
        Forms(Frm.Name).Controls("btnNew").Enabled = False
    End If

End Sub

however as you are passing the form in I'd probably use
Code:
Public Sub USERACTV(Frm As Form, ANum As Integer)

    If ANum = 1 Then
        Frm.btnNew.Enabled = True
    Else
        Frm.btnNew.Enabled = False
    End If

End Sub
 
I think all references passed when using .Controls( ) syntax require double quotes?
Forms("FrmName").Controls("btnNew").Enable = True
 
Sorry, I should have said 'Does work' as I tested it on one of my forms.
That was when I spotted you had 'enable' and not 'enabled' :(
 
you may also try:
Code:
Public Sub USERACTV(Byref Frm As Form, ByVal btnNew As String, Byval ANum As Integer)
    Frm.Controls(btnNew).Enabled = (ANum = 1)
End Sub

you then call the Sub:

USEERACTV Me, "theCommandButtonNameHere", 1
 
I think all references passed when using .Controls( ) syntax require double quotes?
Forms("FrmName").Controls("btnNew").Enable = True

You missed the dot in frm.Name

Double quotes are literal string delimiters. A string variable or reference to a string property such as frm.Name inside the parenthesis is the same as putting a literal string there.

btnNew is a literal string so would need delimiters.
 
Least code solution:
Code:
Public Sub USERACTV(Frm As Form, ANum As Integer)

   Frm.btnNew.Enabled = ANum = 1

End Sub
 
You missed the dot in frm.Name
Actually, I omitted it, not missed it, because I overlooked the fact that frm was a variable and I was thinking along the lines of Me.Controls("frmName") where frnName is just a pseudonym for the name of the form (like "FormNameHere"). So yeah, dumb of me.
 

Users who are viewing this thread

Back
Top Bottom