As with the article about a TreeView exchange with standard continous forms (which you can find here
) this is again a possibility to use slider bars, spin buttons and scrollbars using standard Access controls that are bundled together using a VBA class module.
All the details can be found in the documentation below that is also included in the attached Zip file.
The demo database is in the A2007 ACCDB format, but there are no specialities except the use of transparent buttons (in the demo only, not necessary for the class) so this class can also be used in other versions of Access. Also included are some demo graphics for the graphic slider variants which you can use like you want.
Have fun in experimenting with the new kind of "control".
Table of Contents
1 About CCSlider
2 How to use
3 The demo forms
1 About CCSlider
This is another attempt to replace a control that is part of the Microsoft Common Controls with native Access controls. This time it is the slider control I wanted to replace with this little project.
The MS slider control can be used to enter a value in a range which the programmer can define. But the design of the slider cannot be changed, not very much at least.
The CCSlider “control” is made of a bunch of standard Access form controls, where you can choose different control types for each element of the slider. They are controlled by the class “clsCCSlider” that is all you need; anything else in the attached demo database is just to demonstrate some of the features.
Feel free to use the slider like you want, including changes in the code, but don’t sell this as your product and don’t remove or change the remarks in the code about versions and author etc.
- The class provides the possibility to bundle some standard controls to be used as either
- spin buttons (up/down buttons to change a value)
- slider (a bar control and a handle control where you can move the handle along the bar to change a value)
- both combined to build a scroll bar
- slider/scroll bar as 2D pane to change two values with one handle and/or the spin controls
- Possible controls for each element are:
- Spin Buttons: CommandButton or Image
- Slider Handle: CommandButton, TextBox, Label, Image or Rectangle
- Slider Bar: Rectangle, Label, TextBox or Image
- Tooltip: Label or TextBox
- The use of images as part of the slider control gives you the possibility to design the slider like you want with any kind of picture which you can create in your favourite paint program.
- A lot of events are fired by the class which gives you the possibility to change the behaviour of the slider at many points. The example forms show you some of these events.
- As the design of the element itself will not be changed by the class you can design each element control like you want. The class will only change the position of the handle control and the tooltip control and also captions if enabled.
- Definition of the maximum and minimum value, which will automatically be calculated to fit the size of the bar control. Optionally a percentage value can be shown calculated from these values.
- You can define a start value to be shown.
- Margin values can be defined to have a margin between the handle control and the slider bar.
- Clicking at the left or right side (lower or upper side) of the handle control on the bar control moves the handle one page value lower or higher – like with a scrollbar. The page size can be defined.
- The handle caption can display the chosen value if you want (and if it is a control which is able to display a caption).
- You can double click the handle button to switch between the maximum and minimum defined values.
- The tooltip position can be defined, also the distance. You can also switch off the automatic caption of the tooltip control so you can display your own tooltip information. The size of the tooltip control is of course also not limited as usual so you can even display long texts if you want. The tooltip can be configured to disappear after changing the value, or to stay on the screen.
- Using conditional formatting in case of a TextBox control adds additional possibilities in design for the handle, the slider bar or the tooltip control.
- The slider can also be used in continous forms, with some little constraints explained in the section about the continous demo forms below.
2 How to use
To implement a slider in your form you need either a slider box with a handle control or two spin button controls. If you want to use a label control anywhere please remember that this must not be bound to another input control like a TextBox etc. Access changes the behaviour of a bound label if you click on it (the focus is set to the bound control then).
Next you need very short VBA code to create the slider control with this. So import the clsCCSlider class into your database if you didn’t already have done this and compile your VBA project to make sure anything works with your project.
Then you need a module level object variable that holds the instance of the class code for your form – one for each slider control group you want to implement. For example:
Option Compare Database
Public WithEvents objSlider As clsCCSlider
The “WithEvents” clause makes it possible to consume the events which are fired from the slider class. If you want to use any of these, select the “objSlider” of this example variable in the upper left combobox of the VBA editor, then you’ll find all possible events in the right combobox. Select the wanted one(s), that will insert the event sub stub, like you already know if you insert an event sub for i.e. Form_Load.
Next is that you must tell the class which controls of your form should be used to build the slider control. That is done by calling the “Init” procedure of the class like this example:
.Init enmCCSliderType_Horizontal, 0, 1000, _
Me.cmdSlider, Me.boxSlider, Me.lblTooltip
This would group the three controls “cmdSlider” (the handle CommandButton control), “boxSlider” (the slider bar as Rectangle control) and “lblTooltip” (a label control to be used as tooltip) together as one slider without spin buttons.
Now you have an (unbound) slider control which can change a value by moving the slider handle control with the mouse. You can read out the value using the “Value” property (or “Value2” if you use a 2D variant). If you want to bind a slider directly to i.e. a TextBox control you can use the events of the class like this:
Private Sub objSlider_SliderChangeValue(Value As Double)
Me.txtValue = Value
That would show the chosen value also in the “txtValue“ TextBox control on your form while you are sliding the handle control. If this TextBox is also bound to a table field the value also changes directly. See the demo forms for further examples of using the slider.
3 The demo forms
This is a form that shows how to bundle different standard Access controls to a slider control. At the top is a horizontal slider where the value is copied to the TextBox at the right of it. A second spin button example is at the right of this TextBox that also changes the value of the horizontal slider using the events of the class. Be cautious when you try such things as changing a value on one object that triggers the change of the value of the other if you change the value in the event sub. This is caught here with this sub:
Private Sub objSliderX2_SliderChangeValue(Value As Double)
Static bolEventIsRunning As Boolean
Me.txtValue_H = Value
If Not bolEventIsRunning Then
bolEventIsRunning = True
Me.objSliderX.Value = Value
bolEventIsRunning = False
The static variable ensures that when the value of the objSliderX is changed her, the SliderChangeValue event of it is triggered but this would also call this procedure again, but as the variable is True in this case it will not try to change the value again.
In the middle you can see a 2D slider where you can change two values with one slider at once. This can be helpful if you want to set a coordinate in a map or something like this.
At the right side of it there is a vertical slider example. Here the tooltip is set to stay visible, and the tooltip is made of a TextBox which has a conditional formatting so that the tooltip looks different on different values (when you release the mouse button as CFs are only applied then).
As these controls are normal Access controls you can of course also apply standard event procedures to them. To show that, the tooltip TextBox has an AfterUpdate event which changes the value of the slider after entering a number into the TextBox.
The vertical slider at the right end of the form shows that you can also display a conditional formatted TextBox as slider bar control and the slider here is a label using a WingDings font to display the arrow symbol.
This demo form shows how to use images for the different elements of a slider. The sliders are similar to the ones from the demo form above; there is no big difference in the code.
The horizontal yellow slider with the blue arrows is made of one image. The arrows have two transparent CommandButton objects to make them usable, but as the class automatically uses the complete size of the image slider bar object the margin properties can be used to define the moveable limits the slider control can be moved on the bar.
The 2D pane shows that you can also display an image as the slider pane so this could also be a map graphic instead of this flower picture from my garden.. :-)
The vertical slider is made from four image controls to see how that works. Although the background is set to transparent it can happen that the image don’t display transparent parts. But you can simulate transparent graphics by pasting the slider graphic onto the slider bar graphic with a paint program and then cut the slider graphic together with the background graphic and save it to a new file. On this way it doesn’t matter if the background is not transparent, as you move the slider over the slider bar it seems to the user as it would be transparent. That works of course only if the background looks equal on the moveable part of the slider button.
The continous form 1 shows that you can of course insert a slider control into a continous form. But as with all unbound controls in a continous form all unbound controls are displayed equal in all rows, independent of the displayed value. To see the correct slider position you must click on each row, the Current event changes the slider position in this case – but on all rows the same. This is the only possibility to use a slider inside of a continous form in the rows itself. As the positions of a control cannot be bound to a table field this behaviour cannot be changed.
Moreover binding the value of the table field to the slider control causes a direct change, although the value is not really changed, the row is Dirty in this case. You should avoid changing the slider value in the Current event of the form in this case which causes this behaviour. See below for a solution for that.
As an alternative to the first example this form shows how you can use the slider with a continous form without the problem of the first example: Here the slider is in the header and it changes its value corresponding to the current row so that it looks better for the user.
Here an additional module level variable “prv_bolCurrent” is used to determine when the changing of a value of the slider should also change the table field, details you can see in the code. With the help of this the table field will not change when the Current event is triggered but only if the user moves the slider over the bar.
This is an example form to show what you can do with the sliders. It is a single form which shows one slider for each table field with a frequency setting, additionally a master slider which can move all sliders at the same time but is not bound itself to an underlying field value. So on every move of the record pointer it will reset to 0 (the middle position here as you can set the values from -24 to 24).
Unfortunately I couldn’t find a way to apply these values to the Windows equalizer to use that in Media Player etc., that would have been a nice additional effect and would make it possible to save different settings, but it should not be a sound demo but a slider demo…:-)
As an additional graphic effect I placed 24 colored rectangle controls over each slider that are switched to visible or invisible depending on the value of the slider. (Of course I wrote a little help procedure to create all these controls.)
At the bottom of each slider the values of the table fields are displayed, that could also be entered manually to change the slider value.
The equalizer has an own class module to make the management of the slider controls a little bit simpler, but this is only used for this form and is not needed for the slider itself.