How can I disable a button on a worksheet? - excel

I am trying to disable a button on a worksheet.
I could make it invisible but I'd rather have it on screen but not clickable and greyed out so it is obviously not clickable.
Here is my script for the button:
Sub LinkFile()
Call Module1.setup
Dim btn1 As Button
Dim t As Range
Set t = Range(Cells(3, "G"), Cells(4, "I"))
Set btn1 = ACImport.Buttons.Add(Left:=t.Left, Top:=t.Top, Width:=t.Width, Height:=t.Height)
btn1.Name = "Button1"
btn1.OnAction = "FileSearch" ' MySub is executed when btn is clicked
btn1.Caption = "GO!"
End Sub

For UserForms and ActiveX buttons, this will work:
btn1.Enabled = False
For the other worksheet button, you can try:
btn1.ControlFormat.Ebabled = False
or
ACImport.Shapes("Button1").ControlFormat.Enabled = False
If those do not work then you then you will have to make a judgement call. You can switch to an ActiveX button. Remove the button from the worksheet and place it on a UserForm. Give up and let the button remain enabled. Or creatively push boundaries by employing different strategies to create the effect you want.
For example, if your set the OnAction property to vbNullString. This not only neuters the button, it also changes the button behavior during run time so that a single left click puts it into the caption editing mode. It could be used for a novel, and highly unorthodox, quasi-clipboard.
Or you could go more mainstream, perhaps with #Storax's solution.
Or maybe add some flavor to it by briefly changing the backcolor when clicked. The simple animation adds a nice touch, in fact, I prefer for that over an unclickable button.
You could jump the shark and turn all that clickyness into a mood improving anger reduction tool. Or a cruel joke on the office intern. Or a hilarious joke on your boss.
Or maybe you don't like the click so youou throw a locked transparent text box over it and tweek the sheet protections to give the user sufficient freedom while prohibiting interaction with that text box.
But in the end, if the button isn't animated then it's just as useful as a static image. So maybe you just want to use a picture instead of a button.
With so many choices, it's like the world is your oyster...
...So go find some pearls!

Related

VBA Enable only worksheet scrolling while UserForm is active

Is there a way to enable only the worksheet scrolling while userform is active?
.show (vbmodeless)
Doesn't work for my needs as it enables all editing in worksheet, but I need to let the end user to only scroll the worksheet.
Could someone please share an answer to my question, please?
I suggest you put a vertical scroll bar on the form itself and use changes in the value of the scroll bar to scroll the worksheet using vba code. The form can then be modal and you retain control of the worksheet.
For example, I put a scrollbar on a modal form and ran this code:
Option Explicit
Private Sub ScrollBar1_Change()
Debug.Print ScrollBar1.Value
Sheet1.Rows(ScrollBar1.Value + 1).Select
End Sub
Which worked fine. You will need to adjust the settings of the scroll bar to be sensible in relation to the size of the worksheet and the way you want the user to interact, but if scrolling is the only function you want to allow, then perhaps this could work.

VBA Form Control Button Needs to Disappear After Another Button is Clicked

So I have a Form Control Button NOT and ActiveX Command Button that I want to make disappear when another button is clicked. Normally if it was a ActiveX Command Button i would use:
ButtonName.Visible = False
in the code that was triggered by the other button. However I dont know how to get that work with a Form Control Button as they dont have name like a ActiveX Command Button does so far as I know?
I would just change the button I want to disappear to a ActiveX Command Button but when I do that I have issues with the buttons format changing, text sizes up when they are clicked, buttons change size when clicked, etc.
You may also try
Sub Button2_Click()
Sheets("Sheet1").Buttons("Button 1").Visible = False
End Sub
.Shapes("button 1").Visible = False

Close userform with Esc without control_KeyPress() command for each control

In this answer, it's shown that Excel userforms can be closed with Esc by setting up a control_KeyPress() sub for each control that can take focus - Close userform with escape button
I've gotten this to work, but I have several menus, each with a number of controls. I tried putting this routine on just one button, but it's not always quick to tab/nav back to a specific button.
I'm looking for a way to make it so that I either
don't have to create control_KeyPress() subs for every control that can take focus
or, can achieve the same goal (closing the userform with esc) in a different way
Thanks!
My idea would be to create a command button on the form, set the cancel property to true AND set the width and height to 0. Add the following code to the not visible button.
Private Sub CommandButton1_Click()
Hide
End Sub
Leave the visible property on true
Create a small button, e.g. 6 x 6 so that it can be visible but unobtrusively positioned in one of the corners of the form. Making it this small stops the caption from being displayed and just leaves a neat, small square.
Create a button, (e.g. BTXX), and set:
Cancel = True so that pressing the escape key "presses" this button
TabStop = False so that tabbing through the form does not stop on the button
Caption = "Esc" as a reminder of what the button is for
In the onclick event use:
Private Sub BTXX_Click()
Unload Me
End Sub
In this example the escape button is "hidden" top-left on the form by setting Top=0 and Left=0. (See images below).
Userform
Properties
If you already have a button for closing the userform then you can simply set its cancel property to true. It is then triggered by pressing escape, remains clickable, keeps its accelerator and stays in the tab order. You can only have 1 button with Cancel=True and the property editor takes care of this automatically.

Move focus to a button on a worksheet

I have a worksheet with form control buttons. At the end of a macro called by one button I would like to set the focus to another button, so that if I see that the first button did the job, I could just hit Enter.
I searched the site and got this
ActiveSheet.Shapes("CommandButton1").Select
This selects the shape but Enter does not run it.
Methods of CommandButton do not work.
ActiveSheet.Shapes("CommandButton1").SetFocus
returns the "method not supported" error.
I got this other answer:
Me.CommandButton1.SetFocus
I get "Object needed" error.
This leads me to believe that either CommandButton# object does not exist in Excel VBA or that it refers to a form but my buttons are placed directly on the spreadsheet.
Tried
ActiveSheet.CommandButton1.SetFocus
ActiveSheet.Buttons("Button 1").SetFocus
I found an old sample code with something like this:
CommandButton2.Caption = "CONTINUE"
CommandButton1.Enabled = False
CommandButton3.Enabled = False
This works in that sample, but in that sample the buttons behave differently. I suspect they are ActiveX or something else instead of form control buttons. In that sample right-clicking on them does not bring up the context menu.
I tried
ActiveSheet.Buttons("Button 1").ControlFormat
But do not know what to use next. SetFocus does not work.
With an ActiveX control button named "CommandButton1", you can call
Me.CommandButton1.Activate
from the same worksheet.
On a form control button named "CommandButton1", you can call
UserForm1.CommandButton1.SetFocus
This will make sure the focus is on the CommandButton1.

Access VBA - Trigger combobox change event

I have been playing around with form designs and now I have constructed a form, which is almost complete, just one problem left. Let me explain first:
Form is bound to a "Join Table", which has only 2 fields - ID from "Table1" and ID from "Table2". Based on those two fields I have added fields from "Table1" & "Table2", on same form. Then I have added 2 option buttons, 1 Combobox and 2 Subforms.
This allows me to watch records from two different tables that are joined, from each point of view - "Table1" or "Table2" view. I am selecting this views with Option buttons, which changes Combobox rowsource, so you can navigate to records from Combobox.
Here is code :
Private Sub OptButton0_Click()
If Me.OptButtonO.Value = True Then
Me.OptButton1.Value = False
Me.Cmbox.RowSource = "SELECT [Table1].[Field1], [Table1].[Field2], [Table1].[Field3] FROM Table1 ORDER BY [Field1];"
Me.Cmbox.SetFocus
Me.Cmbox = Me.Cmbox.ItemData(0)
End If
End Sub
Private Sub Cmbox_AfterUpdate()
If Me.OptButton0.Value = True Then
If IsNull(Me!Cmbox) Then Exit Sub
With Me.RecordsetClone
.FindFirst "[Field1] = " & Me!Cmbox
If Not .NoMatch Then
If Me.Dirty Then Me.Dirty = False
Me.MySubform.Width = 8280
Me.MySubform.SourceObject = "MySubform"
Me.Bookmark = .Bookmark
Else
Me.MySubform.Width = 8000
Me.MySubform.SourceObject = ""
End If
End With
Me.Cmbox.SetFocus
DoCmd.Requery
End If
End Sub
This posted code is only for one Option button, second one is same, just opposite. Now what is problem ?
Problem is that when I navigate through record via Combobox, click second Option button for another view AND THEN RETURN to same view, my subform results stays same as they were when I clicked another Option button, although Combobox listIndex is 0. If I select combobox Listindex from Combobox, code works again.
SO BASICALLY - I NEED CODE THAT WILL TRIGGER COMBOBOX CHANGE WHEN OPTION BUTTONS ARE CLICKED. It works when you're clicking in Combobox, but not when clicking in Option button.
I know It's complicated to understand, please take a look at code, and ask anything. Any help appreciated.
Another option: I had a similar issue: with an unbound combobox. In the '*_Change' event in a combobox, if I pick a value from the dropdown, the value is there; but if I delete the existing value, the prior value still shows up.
...
If Me.series_filter > "" Then
lstrMetric = lstrMetric & "and X.series_name = '" & Me.series_filter & "' "
End If
...
Me.Filter = Mid(lstrMetric, 5)
...
I have a dropdown for a filter on the form: picking a value is meant to set (or clear) the filter. It worked when I picked a value, but would not clear if I delete it.
I added a line to the start of the code to commit updates:
Me.dirty = false
The code now recognizes the null value when the combobox is cleared. This works in my case - of course it would be a problem if you didn't want any updated fields written to the database.
call the the combobox afterupdate event in the option button click event:
private sub optbutton0_click()
...
cmbox_afterupdate()
end sub
PS: Rather than having events for option buttons directly, you should put them in a frame, (even if you then have to make the frame transparent to stop it from appearing) and use the afterupdate or click events of the frame, whereby you can get the selected option button by option value:
private sub frame0_click()
select case frame0
case 0 'option button 0 is selected
...
case 1
...
end select
end sub
In Access, controls can be funny. I mean, when you change the option, you see it change, it changes on the screen... but the control doesn't always change its .value right away... meaning it doesn't Update. For a simpler illustration of this principle, create a textbox, type some stuff into it, and look at the textbox.value in the immediate window. Then, with the cursor still in the textbox, backspace some of it. Check the value again. Do that a few more times. Start including textbox.text in your tests. There are multiple ways around this, but it is most important to understand when controls actually update, so you can understand better when and which work arounds to use.
Now, in this situation, you typed 'click' every time when you referenced selecting an option. I think I will hold you to that. Personally, I use Tab and the arrow keys sometimes, which would be a slightly more complicated answer, but one you will be better equipped to solve yourself, after understanding the textbox example above. But, if you change AfterUpdate above to Click, you should be good.
That said, there are events besides mouse clicks that can activate the Click event. Usually not a problem, but since you are changing the look of your form, specifically the width, you may want to be aware that the subform width may flash back and forth sometimes while using your tool. Not often, probably won't be too much of an annoyance, but I might rethink why I needed to change width at all, or if there might be better triggers for it.

Resources