560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Advanced controls in userforms with VBA macros Part six of a nine-part series of blogs |
---|
The two previous parts of this mini-blog have shown how to draw forms and how to write code to handle form events. This final part shows how to add some of the more exotic controls to user forms, like combo boxes, list boxes, multipage controls, spinners and option buttons.
This blog is part of our Excel VBA tutorial. Wise Owl's main business is running Excel, VBA and other courses for businesses.
|
SpinButtons at first glance look clunky and unnecessary:
The SpinButton allows the user to change the number of sugars ordered.
However, the reason that they are so useful is that they allow you to prevent users typing in numbers directly:
The textbox for the number of sugars has the Enabled property set to False - this means that a user can't change the value directly.
If a user can't type in a number directly, you don't have to validate its data type or values. This is a big advantage!
To create a SpinButton, just click on the tool and then click where you want it to go:
Don't confuse the SpinButton tool with the scroll bar tool ...
A SpinButton usually sits next to the textbox whose value it will affect:
Here the SpinButton will control the value in the txtSugar textbox.
That's the easy part. When you double-click on the SpinButton to write code, you get an odd default event:
Private Sub spnSugar_Change()
End Sub
It's more likely that you would attach code to the SpinDown and SpinUp events.
It's good programming practice to write a single procedure to handle both possible spin directions. For our example, this could be:
Sub AddSugar(NumberAdd As Integer)
Dim CurrentSugars As Integer
'find out how many sugars currently
CurrentSugars = CInt(Me.txtSugar.Text)
CurrentSugars = CurrentSugars + NumberAdd
'don't allow negative sugars
txtSugar.Text = IIf(CurrentSugars > 0, CurrentSugars, 0)
End Sub
Here we'll pass in the number of sugars to add (this can be negative). The routine makes sure that you don't set the number of sugars to be less than 0.
If you've written a generic procedure as above, you just need to call it twice - once when you spin up, and once when you spin down:
Private Sub spnSugar_SpinDown()
'add a sugar
AddSugar -1
End Sub
Private Sub spnSugar_SpinUp()
'remove a sugar, if any left to remove
AddSugar 1
End Sub
When you click on the down arrow of the SpinButton, for example, it will call the AddSugar routine to add -1 sugars.
Parts of this blog |
---|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.