snakehips2000
Programmer
I need a way to allow a user to click on a commandbar button (listing a series of 'favourite' forms) and for Access VBA to delete that button programmatically.
I quickly realised that the problem was probably due me trying to delete the button from the function contained within the "OnAction" macro.
Then, I came across the following code thinking that this might get around it but I still can't get it to work properly.
When I click on the CommandBar button, I get "Method 'Delete' of object '_CommandBarButton' failed". Strangely though, if you create several instances of the same button, the code works OK. It's only when you get down to the last remaining button that the error occurs.
Any thoughts? Thanks, Brian
******************************************
Sub CreateMenu()
With CommandBars("InformmenuHOF").Controls(2).CommandBar.Controls(4)
With .Controls.Add(msoControlButton)
.Caption = "Clicktodelete"
.OnAction = "DeleteMyself"
.Visible = True
End With
End With
End Sub
Sub DeleteMyself()
With CommandBars("InformmenuHOF").Controls(2).CommandBar.Controls(4)
.Controls("Clicktodelete").Tag = "DELME"
End With
DeleteLater
End Sub
Sub DeleteLater()
Dim Cbc As CommandBarButton
Set Cbc = CommandBars.FindControl(, , "DELME")
If Not Cbc Is Nothing Then
Cbc.Delete
End If
End Sub
I quickly realised that the problem was probably due me trying to delete the button from the function contained within the "OnAction" macro.
Then, I came across the following code thinking that this might get around it but I still can't get it to work properly.
When I click on the CommandBar button, I get "Method 'Delete' of object '_CommandBarButton' failed". Strangely though, if you create several instances of the same button, the code works OK. It's only when you get down to the last remaining button that the error occurs.
Any thoughts? Thanks, Brian
******************************************
Sub CreateMenu()
With CommandBars("InformmenuHOF").Controls(2).CommandBar.Controls(4)
With .Controls.Add(msoControlButton)
.Caption = "Clicktodelete"
.OnAction = "DeleteMyself"
.Visible = True
End With
End With
End Sub
Sub DeleteMyself()
With CommandBars("InformmenuHOF").Controls(2).CommandBar.Controls(4)
.Controls("Clicktodelete").Tag = "DELME"
End With
DeleteLater
End Sub
Sub DeleteLater()
Dim Cbc As CommandBarButton
Set Cbc = CommandBars.FindControl(, , "DELME")
If Not Cbc Is Nothing Then
Cbc.Delete
End If
End Sub