Number Picker in Access / VBA - vba

I am trying to put a number picker in a form in MS Access 2007. Here's an example of what I am trying to make:
I cannot find this in the default form controls, and have tried to make one myself using a listbox. Listboxes can be modified to look just like the number picker above, however the arrows only change the view, of the list, and not the actual selection (that is the value). For example, with the list box, if I have it range from 1 to 3, and default at 1 - when I change it to 2 via the arrows, the value of the listbox does not change, and is still one.
Does anyone know how to get a number picker in Access?

So you want to create a list of numbers and allow users to change the value displayed (AND stored as the control's value) using up and down arrows, such that they select the next or previous in the list.
I would suggest creating a text box and two buttons. Populate an array with the list of values. When a button is clicked it would:
A. Find the position in the array of any value already entered into the text box (eg loaded from a database)
B. Get the next or previous item from the array.
The array is populated as required (probably when the form is opened).
If you just need to allow the user to enter a whole integer number (ie a number spinner) you would do as follows:
Create one using a (locked) textbox and two buttons. Just add a textbox (name it something like txtValue) and two buttons (btnUp and btnDown), then add code like this to the Click event of those buttons:
Private Sub btnUp_Click()
Me.txtValue = Nz(Me.txtValue, 0) + 1
End Sub
Private Sub btnDown_Click()
Me.txtValue = Nz(Me.txtValue, 0) - 1
End Sub
You could add if statements to limit the data being entered
Or you can use a 3rd party control.
http://www.fmsinc.com/microsoftaccess/controls/components/spin-button/index.html
There are probably more, but be aware that using these sorts of controls in Access is unsupported, and there is no guarantee moving forward that they will work in Access. You're far better off using the native methods described earlier.

Related

Passing a checkbox control to a function

I am using Access 365 and VisualBasic. I have a number of checkboxes on a form that I want to display just a subset of these at a time, so I want to reposition the top of the checkboxes so that no matter which are currently displayed, I can make them line up nicely.
So, I have declared an array of checkboxes that I want to dynamically add those checkboxes to display:
' Array holding the checkboxes that will be displayed, depending upon species, sex, etc.
Dim arrCheckBoxes(cNumCheckboxes) As CheckBox
Then as I determine that a specific check box should be shown, I want to add it to the array at the next available position using this function (which will also update the next available position):
Private Sub AddCheckbox(ByRef arrCheckBoxes() As CheckBox, ByRef chkNew As CheckBox, ByRef intCurrentCheckbox)
intCurrentCheckbox = intCurrentCheckbox + 1
arrCheckBoxes(intCurrentCheckbox) = chkNew
End Sub
My problem is that in the code that calls the AddCheckbox function, I cannot figure out how to pass the check box. I am using the following code (CatNeuter is the checkbox from my form).
Call AddCheckbox(arrCheckBoxes, Me.CatNeuter, intCurrentCheckbox)
However, Me.CatNeuter is always 0, so I think I am getting just the value, and not the checkbox control itself. I've tried numerous different methods such as:
Me.Controls!CatNeuter
Me.Controls("CatNeuter")
But I just cannot figure out how to pass the actual checkbox so I can then go through the array and change the Top property for each checkbox.
Regards,
Lise
You need to use Set for objects:
Set arrCheckBoxes(intCurrentCheckbox) = chkNew

Excel VBA mulitiple checkboxes

is it possible to create listbox with multiple checkboxes in one row (Excel VBA)?
Thanks
Kamil
I'm not sure I understood your question fully, but I'll elaborate on ListBoxes as much as I can.
First things first: Checkboxes and ListBoxes are different objects in Excel Userforms. The first is the little box that returns a "true/false". The second is a list of items which can be chosen. Clicking in a Checkbox will make the tick mark appear/disappear (or fade if tristate is enabled), while clicking a Listbox row will turn the listbox row "blue"/"white" (or whatever color is being used for the selected rows). In both elements, clicking is a way to toggle between True and False.
While a checkbox only allows for a single information to be marked as True or False, a Listbox allows you to select entries out of a list. That list may be inserted through code (.AddItem method) or passed from a range (.RowSource property)
ListBox objects allow for multiple columns of data to be attributed to one row element, but each row is an entire element (which means you cannot pick the element on row 3, column 2 - only all of row 3). The number of columns is established using the ColumnCount property.
By changing the value of the MultiSelect property, you'll allow the user to select multiple or single row elements simultaneously on your Listbox. Using the Selected( RowIndex ) property, you can check whether or not an item is currently selected (returns True/False). Remember that row indexes start at 0.
Finally, if you're using the MultiSelect property set to fmMultiSelectSingle and have a single column (as far as I know), the Text property can be used to return the selected item's value.
An easy example of a listbox is in Excel can be found at File > Options > Customize Ribbon (or something like that). There are two listboxes, one (on the left) with the visible items and another with the available items. A pair of command buttons is used to move items between boxes. That's a simple application you can likely find already setup online.
Am I on track to answer your question?

Multiple Text Box and Lookups

I am currently trying to improve on an Access Database VBA that I have inherited from my predecessor at work. I have come unstuck on a particular form.
I have a form that at the moment is just a large form containing 32 individual textbox, with the same code behind each but it is the same code repeated for each textbox with just the references to the text box changing in each.
Private Sub Cand_No2_AfterUpdate()
Cand_Name2 = DLookup("[Name]", "[qryExamAbsences]", "[Cand_No] = Cand_No2")
End Sub
Then once the button is pressed
If Not IsNull([Cand_Name1]) Then
Rope = Rope & " Or Cand_No = " & [Cand_No1]
End If
(The If statement is contained in the button mousedown event.)
Occurs for each text box which then filters a report that is printed for office use. There are many problems with this but the major one I am trying to solve is that there is an upper limit to the number of entries, if I need to filter more than 32 I would need to delete the text and start again as it were.
Is there a way of combining all this into a single section of code which will create text boxes when needed?
EDIT.
I have found a way to give the impression to the user that the text boxes are being created after each entry which has improved the form from a user standpoint (no longer having 32 textboxes or having to scroll down to the Print Button.) however this still hasn't solved the issue of messy code as I have had to repeat the extra code for each box again, it also leaves me with the maximum of 32 entries still.
The new code is as follows:
If Not IsNull(Cand_Name1.value) Then
Cand_No2.Visible = True
Cand_Name2.Visible = True
cmdPrint.Top = 2500
cmdPrint.Left = 2500
DoCmd.MoveSize 1440, 2201, , 4000
Else
Cand_No2.Visible = False
Cand_Name2.Visible = False
cmdPrint.Top = 2000
DoCmd.MoveSize 1440, 2201, , 3500
End If
Essentially makes the next text box visible and moves the print button down to make room for the new text boxes. It also expands the window.
Could you not just have 2 text boxes, one for CAND_NO and another for CAND_NAME and then beside those two boxes place an ADD CAND_NO button.
Create a list box that would list every CAND_NO / CAND_NAME after they press the add button so they can see what they've added so far. Then loop through your list box to build your rope string or have your rope string either a global variable on the form and build it as they add numbers or stored in a hidden text box storing the value as they add numbers if you don't like global.

Gray out a form row's (detail's) button conditionally with VBA code

I have a standard form in MS-Access which lists a bunch of orders, and each row contains order no, customer, etc fields + a button to view notes and attached document files.
On request from our customer we should gray out the button btnAnm (or check or uncheck a checkbox) depending on a calculation from two queries to two other tables (a SELECT COUNT WHERE and a check if a text field is empty).
I've tried btnAnm_BeforeUpdate(...) and btnAnm_BeforeRender(...) and put breakpoints in the subs, but none of them trigger. The same if I use the control Ordernr instead of btnAnm.
I'd like a function in the Detail VBA code to be triggered for each "Me." (row) so to speak, and set the row's control's properties in that sub.
What do I do? I've looked at the help file and searched here.
*Edit: So I want to do something that "isn't made to work that way"? Ie. events are not triggered in Details.
As an alternative, could I base the value of a checkbox on each line on a query based on the 'Ordernr' field of the current row and the result of a SELECT COUNT from another table and empty field check?
Do I do this in the query the list is based on, or can I bind the extra checkbox field to a query?
A description of how to do this (combine a COUNT and a WHERE "not empty" to yes/no checkbox value) would be perfectly acceptable, I think! :)*
You cannot do much with an unbound control in a continuous form, anything you do will only apply to the current record. You can use a bound control with a click event so that it acts like a button.
Presumably the related documents have a reference to the order number that appears on your form, which means that you can create a control, let us call it CountOrders, with a ControlSource like so:
=DCount("OrderID","QueryName","OrderID=" & [OrderID])
The control can be hidden, or you can set it up to return true or False for use with a textbox, you can also use it for Conditional Formatting, but sadly, not for command buttons.
Expression Is [CountOrders]>0
You can also hide the contents and add a click event so that is acts in place of the command button. Conditional Formatting will allow you to enable or disable a textbox.
As I understand your question, you have a continuous form with as command button that appears on each row - and you'd like to enable/disable the button conditionally depending on the contents of the row.
Unfortunately you can't do that. It seems that you can't reference the individual command buttons separately.
Having wanted to do something similar in the past I came up with two alternate ways of setting up my interface.
Put a trap into the onClick code for the Button. Which is icky, because it is counter intuitive to the user. But it gets you that functionality now.
Move the command button (and editable fields) up into the form header, and make the rows read only. Your users then interact with the record only in the header, and select the record they want work with in the list below. As I recall this is known a Master-Detail interface.

VB in Access: Combo Box Values are not visible in form view but are visible through Debug.Print

Code in Form onLoad:
country_combo.RowSourceType = "Value List"
Code in a reset function:
Dim lListIndex As Long
With Me.country_combo
For lListIndex = .ListCount - 1 To 0 Step -1
.RemoveItem (lListIndex)
Next lListIndex<br/>
End With
Code to populate country combo:
*For n = 1 To numCountries*
*countryCombo.AddItem (countryRS.Fields("countryName"))*
*countryRS.MoveNext*
*Next n*
I'm having a problem that occurs AFTER the code to populate the country combobox runs. The values are there as I can run Debug.Print(countryCombo.Value) and it prints out the name of the selected country, but I can't see the values in the combobox at all. They're invisible, and as far as I know there is no visiblity property for specific items, unless I'm completely mistaken.
comboBoxError.png http://img110.imageshack.us/my.php?image=comboboxerror.png
I think you should probably use Access's GUI tools to do what you're looking for. In design mode, click on the field you are trying to populate, then click the "lookup" tab. You can then specify a table to populate the field with and your forms should automaticly update as well.
I've also seen what you describe here - as far as I can tell, it's a bug within Access (I was using 2007) that only occurs when you programatically mess with the contents of a combo box. It does not happen every time. The issue corrects itself if you highlight the text that is in the combo box.
I am experiencing a similar issue with Access 2003. Based on the selection of one combo box, the row source of a listbox is set to an SQL string Basically a SELECT DISTINCT [MyField_Selected] FROM MyTable. For some fields the values are visible in the list box and others it is not. The values are there however as I can access them via code. To make it more interesting it works fine in Access 2007.
Just found the resolution on another forum. Check the format property of the field(s) in question on the table. In my case, when Access 2007 created the table, it put an # format in there. I removed that and all works great!