In trying to remove black border from spin button onclick
(btw, I'm very interesting about the reasons for this ugly black border)
Private Sub spin01_SpinUp()
... //some code
ch01.SetFocus // ch01 is a textbox
First click - there are no borders
Next click - borders are there
Next click - there are no borders - and so on
The same is for SpinDown()
Very strange, isn't it?
Those black borders are to let you know the spin button currently has focus. If you click on the textbox ch01 on your userform, then the lines will go away. AFAIK there isn't any property to remove those Black Borders permanently.
What is happening in your case is the code transfers the focus to the textbox ch01 and that is when you do not see the borders but when the focus comes back to the Spin Button, those black borders are back.
Alternative:
Use Two command buttons and replicate the functionality of the Spin Button. You can also change the .Picture to show arrows instead of text on the command button. See Screenshot Below
FOLLOWUP
Private Sub spin01_SpinUp()
'
'~~> Rest of the code
'
wait 0.1
ch01.SetFocus
End Sub
Private Sub spin01_SpinDown()
'
'~~> Rest of the code
'
wait 0.1
ch01.SetFocus
End Sub
Public Sub wait(ByVal nsec As Double)
nsec = nsec + Timer
While nsec > Timer
DoEvents
Wend
End Sub
Related
In my subform (VBA in ms-access) there is a button called "Test" and this button has to be set invisible after it was clicked.
So I tried:
Private Sub Test_Click()
'Do some stuff
Me.Test.Visible = False
End Sub
But this does not work. It raises an error because it is not possible to make a button invisible, which has the focus. Unfortunately the button is the only button on the form (the other controls are lables). As the button is in a subform, I tried to set the focus to a button in the parent:
Private Sub Test_Click()
'Do some stuff
call me.parent.schclose.SetFocus
Me.Test.Visible = False
End Sub
But this did not work either. 'call me.parent.schclose.SetFocus' does not raise an error but 'Me.Test.Visible = False' does like before.
So, how to make the button invisible?
What you can do is expand your user form and add a button that is visible by default. Let's say currently your user form has height of 300 and a width of 300. Expand the form to be 500X500 and place the new button on the bottom right of that form (keep the visible property set to Yes). Next add this code to the form:
Private Sub UserForm_Initialize()
Me.Height = 300
Me.Width = 300
End Sub
Now when you click on your test button it will be invisible and the new button will still be hidden because it's not within the height you set above.
Inspired by the answer of VBA_SQL_Programmer I created a empty read-only edit field with a transperent boarder (which means it is basicly invisible) in my form to set the focus to.
So my code looks like this:
Private Sub Test_Click()
'Do some stuff
call me.invisibleTextBox.SetFocus
Me.Test.Visible = False
End Sub
This works without manipulating the size of the form.
But there is a problem too, because as the focus is set to the textBox, the user can see a blinking cursor.
I have an onscreen number pad 0-9, I'm trying to send a keystroke to a checkbox when a user clicks the button of that number (essentially a touchscreen keyboard). There are several textbox's and I want to send it to the one that currently has the caret in it. I've tried SendKeys.Send("1") but that doesn't send it. What is the best way of doing this?
The issue you are encountering is because when the user clicks on any of your buttons, the button takes focus and the textbox loses focus, and therefore any keystrokes are sent to the control with focus, i.e. the button.
One possible way around this might be to use a global variable to store a reference to the last textbox to have lost focus on the form (via the On Exit or On Lost Focus events of each textbox), and then populate the content of this stored textbox with an appropriate value as part of the On Click event of each button.
A very simple example of this might be something along the lines of:
Dim LastTextBox As TextBox
Private Sub TextBox1_Exit(Cancel As Integer)
Set LastTextBox = TextBox1
End Sub
Private Sub TextBox2_Exit(Cancel As Integer)
Set LastTextBox = TextBox2
End Sub
Private Sub Button1_Click()
If Not LastTextBox Is Nothing Then LastTextBox = "1"
End Sub
Private Sub Button2_Click()
If Not LastTextBox Is Nothing Then LastTextBox = "2"
End Sub
I want to apply the following click event for all ActiveX checkboxes on my sheet. It's really simple - it turns the background color yellow if checked and white if unchecked:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox1.BackColor = vbYellow
Else
CheckBox1.BackColor = vbWhite
End If
End Sub
How can I apply this click event to all checkboxes now and for future checkboxes that I create? I googled it and some people suggested classmodules and Private WithEvents, but I can't seem to figure it out!
Edit: I want to apply this 1 click event for the specific box being clicked, not turn all boxes yellow! I dont want to write 50 CheckBoxn_Click subs!
I am just learning VBA and have used some code from an older book (Excel 2010). It could be that Excel 2016 had some changes that make this code not work anymore.
I do not get a compile error for the class or the Subs. The behavior is that NOTHING happens. What is supposed to happen is that the BackColor of either a ComboBox or a TextBox should change color as if is in focus or leaves focus.
As I said, for some reason when I run the code nothing happens. No errors or warnings appear so it's as if the code is running and then just doing nothing.
Here is my code. The comments should make it clear. I am hoping someone can explain to me what is going on and why this code results in no color changes as the focus changes when I tab through the UserForm.
This first block of code is a stand alone Class Module called "clsCtlColor"
Public Event GetFocus()
Public Event LostFucus(ByVal strCtrl As String)
Private strPreCtr As String
'Base Class for chaging Backcolor of ComBoxes and TextBoxes when focus is changed.
Public Sub CheckActiveCtrl(objForm As MSForms.UserForm)
With objForm
If TypeName(.ActiveControl) = "ComboBox" Or _
TypeName(.ActiveControl) = "TextBox" Then
strPreCtr = .ActiveControl.Name
'On Error GoTo Terminate
Do
DoEvents
If .ActiveControl.Name <> strPreCtr Then
If TypeName(.ActiveControl) = "ComboBox" Or _
TypeName(.ActiveControl) = "TextBox" Then
RaiseEvent LostFucus(strPreCtr)
strPreCtr = .ActiveControl.Name
RaiseEvent GetFocus
End If
End If
Loop
End If
End With
Terminate:
Exit Sub
End Sub
The following Subs are in the UserForm Code
Option Explicit
Private WithEvents objForm As clsCtlColor
'*********************************************************************************************************************
'*Subs for managing the BackColor of comboxes and TextBoxes depending on focus.***************************************
'*********************************************************************************************************************
'initializes the Userform with the clsCtlColor class
Private Sub UserForm_Initialize()
Set objForm = New clsCtlColor
End Sub
'Changes the BackColor of the Active Control when the form is activated.
Private Sub UserForm_Activate()
If TypeName(ActiveControl) = "ComboBox" Or _
TypeName(ActiveControl) = "TextBox" Then
ActiveControl.BackColor = &H99FF33
End If
objForm.CheckActiveCtrl Me
End Sub
'Changes the BackColor of the Active Control when it gets the focus.
Private Sub objForm_GetFocus()
ActiveControl.BackColor = &H99FF33
End Sub
'Changes the BackColor back to white when the control loses focus.
Private Sub objForm_LostFocus(ByVal strCtrl As String)
Me.Controls(strCtrl).BackColor = &HFFFFFF
End Sub
'Clears the objForm when the form is closed.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Set objForm = Nothing
End Sub
In the Class Module the is an On Error Statement that terminates the Sub when an error occurs. However, I commented it out and still, I see no compile errors. So, I can only conclude it is a runtime issue.
Any help would be much appreciated.
UPDATE:
If I use these two subs on a TextBox I get the effect I'm looking for:
Private Sub TextBox1_Enter()
TextBox1.BackColor = RGB(153, 255, 51)
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1.BackColor = RGB(255, 255, 255)
End Sub
What I hate about this is that my UserForm has over one hundred TextBoxes and I would need to write these two subs for each TextBox - so like 200++ Subs!
I am still trying to get the above more general approach to work.
One thing I noticed is that if I change the RGB values in the two subs above to Hex values, they no longer work. I tried changing the hex color values in the more general approach to RGB but it made no difference.
Yet Another Update:
It was pointed out that I had a typo in the class LostFucus. I changed that in two places to LostFocus. However, the code still does not work. Then the question was whether or not my code is in the userform module. It is. Then I tried an experiment. I created a new Workbook and imported the code into a brand new class and userform. I added three textboxes. Abracadabra! It worked! However, it does not work in the form I want it to work in. I have scoured the properties for the form itself and the text boxes and I can see nothing different between my form and the dummy form.
This must be something very simple I am over looking!
After a great deal of head scratching and screaming at my poor monitor I finally found the solution but as of now, I am totally disappointed in Microsoft for the weirdness of working with UserForms. Here is what fixed the problem:
I had not yet set the tab order!
I realized the tab order had my form opening with the first tab stop being set for a TextBox in a MultiPage on my form. I set the tab order so that the first TextBox is active on the UserForm and everything works with the coloring on the main body of the form.
Here is where the weirdness begins, in my opinion.
When the last TextBox on the main body of the form is reached and tab is pressed, the multi-page itself is selected. Only after you hit tab a second time is the first TextBox within the MultiPage selected and then the colors are not applied as they are in the main body of the form at all. The same scenario holds true for Frames as well. Also, there does not appear to be a good way to simply tab from the end of page 1 to the beginning of page 2.
It's very disappointing to me because I would have thought that this is not the way it is. I ASSUMED I could set up 1000 TextBoxes, use the Frames and the Multipage to organize things (SO I COULD MAINTAIN THE WINDOW AT ONE SIZE AND NOT HAVE TO SCROLL THE FORM UP AND DOWN) and then set a tab order that would navigate ALL of the TextBoxes regardless of what organizing container they are in. I assumed it would be this way because it MAKES SENSE! I want to click into the first TextBox and simply never touch my mouse until the form is completely filled out. Otherwise, there really is no point in this effort of making a UserForm! I could point and click around in the spreadsheet without the hassle of designing a form and writing code!
What a bummer!
I suppose I can "make it so!" by writing a bunch of code to jump the selection from container to container...MICROSOFT - It should not be this wonky and stupid!
I want to hide the excel file behind the Userform.
Is it possible that when I move the Userform with cursor, then Excel application behind the UserForm also moves?
The application has properties you can define
Top
Left
Width
Height
used like:
Application.Top = 0
Play around with this in various events in the userform until you find what youre after.
I strongly suggest learning how to capture the userform's values and then assigning them to the application.
Extending the answer by #DougCoats - its important to set the Application.WindowState to xlNormal in order to manipulate Application properties such as Top and Left etc. See the code below - you need to include a Module and UserForm in your workbook:
Module1
Option Explicit
Sub TestForm()
UserForm1.Show
End Sub
UserForm1
Option Explicit
Private Sub UserForm_Initialize()
HideApplicationBehindUserForm
End Sub
Private Sub UserForm_Layout()
HideApplicationBehindUserForm
End Sub
Private Sub HideApplicationBehindUserForm()
With Application
.WindowState = xlNormal
.Top = Me.Top
.Left = Me.Left
.Height = Me.Height
.Width = Me.Width
End With
End Sub
The Initialize event will hide the Excel application behind the UserForm when you open it. The Layout event will move the application behind the UserForm when you move the UserForm around with your mouse. The code is the same in both circumstances - HideApplicationBehindUserForm.
I see a little problem with my Excel where the application is slightly off to the border of the UserForm: