Solved Code Error (1 Viewer)

Niroshana

New member
Local time
Today, 20:52
Joined
Jul 10, 2020
Messages
8
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:22
Joined
Sep 21, 2011
Messages
14,270
Walk through the code with F8 after setting a breakpoint on the If statement.

Where is variable btnNew declared/populated?
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:22
Joined
Sep 21, 2011
Messages
14,270
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
 

Micron

AWF VIP
Local time
Today, 11:22
Joined
Oct 20, 2018
Messages
3,478
I think all references passed when using .Controls( ) syntax require double quotes?
Forms("FrmName").Controls("btnNew").Enable = True
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:22
Joined
Sep 21, 2011
Messages
14,270
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' :(
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:22
Joined
May 7, 2009
Messages
19,237
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
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:22
Joined
Jan 20, 2009
Messages
12,852
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.
 

cheekybuddha

AWF VIP
Local time
Today, 16:22
Joined
Jul 21, 2014
Messages
2,276
Least code solution:
Code:
Public Sub USERACTV(Frm As Form, ANum As Integer)

   Frm.btnNew.Enabled = ANum = 1

End Sub
 

Micron

AWF VIP
Local time
Today, 11:22
Joined
Oct 20, 2018
Messages
3,478
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

Top Bottom