Home > Back-end >  Enabling specific buttons in a form
Enabling specific buttons in a form

Time:01-24

I alredy have this question resolved, but, I don't know what I doing wrong.

In one Access the function works perfectly, in other the same code (cmdStructureOn) dosen't work, the same code retur an error:

Add a info, on the access that is work I have two codes, one for True and other for False, just de True works, the false, is the same code juste change True or false and doesn't work

Run-time error '424': Object required

Dosen't work

Public Function cmdStructureOn()
    
    Dim arr As Variant
        arr = Array("btnAnalyze", "btnVacation", "btnTicket", "btnBackup104", "btnParameterStr", "btnStrReturnMenu")
    Dim btn As Variant
        
    For Each btn In arr
        Form_FrmMenu.Controls(btn).Visible = True
        Form_FrmMenu.Controls(btn).Enabled = True
    Next btn
End Function

Works

Public Function cmdStructureOff()
        
        Dim arr As Variant
            arr = Array("btnAnalyze", "btnVacation", "btnTicket", "btnBackup104", "btnParameterStr", "btnStrReturnMenu")
        Dim btn As Variant
            
        For Each btn In arr
            Form_FrmMenu.Controls(btn).Visible = False
            Form_FrmMenu.Controls(btn).Enabled = False
        Next btn
    End Function

CodePudding user response:

This is not a solution to your problem.

As I said in my comment, I tried every possible scenario (including the one you posted with two methods) and I cannot replicate the issue. However, you can refactor your code to avoid the unnecessary duplication and maybe this could solve the problem.

There's no need to have two identical methods just to change a boolean value. Instead, have only one where you pass the boolean value as parameter.

Assuming the code is located behind the form Form_FrmMenu:

Public Sub cmdStructure(ByVal flag As Boolean)
    
    Dim arr As Variant
        arr = Array("btnAnalyze", "btnVacation", "btnTicket", "btnBackup104", "btnParameterStr", "btnStrReturnMenu")
    
    Dim btn As Variant
        
    For Each btn In arr
        With Me.Controls(btn)
            .Visible = flag 
            .Enabled = flag 
        End With
    Next btn
End Sub

Then just call the method passing the appropriate value:

cmdStructure True

'or 

cmdStructure False

Lastly, functions usually return a value. Since the method does not return a value, it's advisable to make it a Sub.

Hope this helps.

  • Related