Clear out dropdown value once another drodown value is changed in vba access? - ms-access

I have three combo box controls(MFG, Code, and GrpID) in my VBA access form. Once the user selects an option from the first combo box (MFG), rest of combo boxes give me available options. But I need to do some validation i.e. what if the user decided to change the value of first combo box? The values of rest combo box should be cleared. All I need to do is once the first combo box is changed the second and third combo box need to be cleared out or at least set to focus on them so that users will realize that they can't use old values as first value is cleared in the first combo box. I added a code block 'AfterUpdate for first combo box as shown below:
Private Sub MFG_AfterUpdate()
Code.Value = " "
GrpID.Value = 0
End Sub
The problem after writing above code is: they don't get empty until they(Code and GrpID) get clicked. In other words, I need to click them to empty them every time I change the value of MFG. Can anyone direct me how do I clear them or at least focus them?

Set the combo to null to wipe any displayed value in them;
Me.Code = Null
Me.GrpID = Null
This assumes your combo controls are called the same as your field names.
Edit for clarity: Including the Me. makes sure your are changing the form control values. If your field names are the same as the controls then Access will change the underlying field values, and as you have discovered on your form these aren't reflected until you click in that fields bound control.

Related

MS Access - Identify a specific textbox from the result of a combobox selection

I have a combobox on a form for selecting a specific shelf location. I also have a number of textboxes imitating a shelf location. I would like to use the result of my combo box selection to change the color of a matching textbox label, for example combobox selection is F14. Textbox label name is LabelF14. How do I code that for event afterupdate to select LabelF14 from the combobox result, F14?
Build control name by concatenating literal text and variable, like:
Me("Label" & Me.comboboxname)
If need to set multiple controls' BackColor at same time, can use a For Next loop. If there are say 15 controls with similar names like LabelF1, LabelF2, ... LabelF15:
For x = 1 to 15
Me("LabelF" & x).BackColor = IIf(Me("LabelF" & x).Name = "Label" & Me.comboboxname, vbGreen, vbWhite)
Next
Make sure BackStyle property is set to Normal, not Transparent.
Keep in mind that programmatically setting properties of a control will affect all instances of control.
If you colored textboxes instead labels, could probably use Conditional Formatting instead of VBA.

Paste a value in a textbox on the second tab of a navigation form access vba

I'm quite new to VBA and I've been looking around but cannot seem to find a solution to my problem.
I have made a navigation form (frmNavigation) with 3 buttons, each referring to a different form, let's call them frm1, frm2 and frm3. In the navigationform the control buttons to switch between tabs are all named differently (btn1, btn2, btn3), but the subform that shows either frm1, frm2, or frm3 has the same name: “NavigationSubform” (this shows a different form depending on which tab is clicked on, based on the 'navagation target name' referring to frm1, frm2 and frm3).
When I want to refer to a textbox (txtBox1) on form 1 (first tab) and insert a value i can do this by:
Forms!frmNavigation!NavigationSubform.Form!txtBox1.Value = "insert awesome text"
But how would I refer to txtbox10 on the second tab (frm2)? Just using the following does not work:
Forms!frmNavigation!NavigationSubform.Form!txtBox10.Value
You then get the error 2465 (can't find the field).
I’ve been trying many different things, but can’t seem to get it right. So how do I refer to a textbox on a different tab than the first one?
Help us much appreciated!
Only one subform can be loaded at once. So you've just got to break this process into two steps.
Store the value from txtBox1 somewhere outside of the NavigationSubforms (a textbox on the parent form with visible = no, a global variable or a table works).
In frm2's On Load event, set txtbox10 to be the value you stored.
Just note, that you will need to add conditions in the On Load event if you want to avoid that textbox being set to an empty string or a wrong value if you have a setup where your filter is changing.

Autofilling my form in Access with the use of a Combo Box

I'm having a problem when I want to autofill my form in Microsoft Access. The idea is that I use a combo box to select a name. Then the onChange code of my Combobox automaticlly inserts all the other data in the proper field. I use this code on the Combo Box.
Private Sub cmbName_Change()
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(2)
Me.tbBirthday = Me.cmbName.Column(3)
End Sub
This methode works fine for the personalnumber and the emailadress. But it doesn't work for the birthday date, it returns a null value but when I check my table there is is a date in the proper field.
Am I missing something? I tried everything but it wont work.
I was thinking that the problem is related to the birthday column being the last in the table. Or having the date type.
Thank you in advance for your time and efford!
Edit; The .Column(1) is missing because this is the name that is already inserted with the ComboBox.
There is some confusion caused by the wording of the question, I'll try to state back how I've interpreted and if I have it right it may lead you to an answer.
You have combo box called cmdName that is pre-populated with data from a table. The content of the combo box could look as below (you may have set column widths to zero to hide the data)
0001|Gary Evans|gary#email.com|01/Jan/1970
0002|J Rommers |JR#email.com |02/Jan/1970
When the user selects J Rommers Me.tbPersonalNumber is populated with Me.cmbName.Column(0) (0002) and Me.tbEmailadress is populated with Me.cmbName.Column(2) (JR#email.com) but Me.tbBirthday is not being populated with Me.cmbName.Column(3) (02/Jan/1970).
Assuming Me.tbBirthday is a text box with no code that might clear it out, I suspect the issue is within the combo box. Not being sure how your combo box is set up, I would suggets the following checks:-
In the combo box properties, does the Column Count equal 4?
In debug, with a breakpoint on Me.tbBirthday = Me.cmbName.Column(3), does it show you the date you are after?
If it is not there does the query that populates the combo box have it in?
Edit based on comments to help further: -
Change the query to SELECT Personel.PersonalNumber, Personel.Emailadress, Personel.Birthday, Personel.Name FROM Personel ORDER BY Personel.Name; this puts all the fields you want hidden at the front.
Change the column widths property of cmbName to 0,0,0, this first the ones you want hidden and leave the last one to fill the width of the combo box.
Ensure the column count property is still 4 as per the answer
Change your code as per below and Gustav's answer
Replacement code:-
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(1)
Me.tbBirthday = DateValue(Me.cmbName.Column(2))
This accounts for the fields moving in the query and ensure the date shows as a date like you wanted.
Comboboxes (and Listboxes) always return a string, so convert that to a Date value:
Me!tbBirthday.Value = DateValue(Me!cmbName.Column(3))

Opening a combo box from another object

I would like to know if it is possible to open a combo box and see it's elements listed from another object on the form. I do not like the 'down arrow' on the combo box and would like to create something more 'stylish' if it is possible. I am wondering if there is a routine one could write and put on another object so that upon the 'on click' event, the user will see all the elements listed within the combo box and have the opportunity to select one of them.
For that you can use the Dropdown method of Combobox object.
Example:
Dim cmb1 As ComboBox: Set cmb1 = Me.Combo1
cmb1.SetFocus ' necessary
cmb1.Dropdown
Note: The control needs to have focus so you have to set it programmatically first.

MS Access Multi-select Combo Box Select All/None

I have a combo box on a form that is linked to a SharePoint field, the combo box populates correctly however I am having difficulty trying to add VBA code to select all of the options or to unselect all options.
With a standard combo box you can use:
cmbBox1.value = ""
and that will reset the field. The same thing can be done with a list box that has multi-select enabled however this tosses an error, "This control is read-only and cannot be modified", with the combo box that has multi-select because of the lookup.
I have done some searching however no one seems to have a real answer other than to use a listbox instead and that isn't a solution here.
Has anyone worked with one of these fields and know how to select all of the options using VBA?
Here is a link describing this type of field but it does not discuss how to interact with it using VBA - http://office.microsoft.com/en-us/access-help/use-a-list-that-stores-multiple-values-HA010031117.aspx.
UPDATE:
There has been some confusion about the type of field I was describing so I have added some screen captures to show the difference between a combo box that allows multiselect, a list box that allows multiple options and a combo box with the option added.
First the field I was describing:
Second the list box:
Lastly the combobox:
These images visualize the issue that was described. As you can see there are multiple check boxes that need to be selected or unselected. Normally I would not create a field like this but as described above this is how Access interprets a combobox from SharePoint that allows for multiple selections.
After a ton of searching and trial and error I figured it out.
To unselect all of the check boxes it is
cmbBox1.Value = Array()
So with this information I figured that to select items they have to be in an array. Creating an array with all of the items that are in the combo box and then setting the combo box equal to the array will select all of the items.
I used a basic loop to set each element of the array
Dim SelVals(), i
ReDim SelVals(0 to cmbBox1.ListCount - 1)
For i = 0 to cmbBox1.ListCount - 1
SelVals(i) = cmbBox1.Column(1,i)
Next i
cmbBox1.Value = SelVals
Obviously then you aren't limited to only using the entire contents - you could assign any array and those would be the values selected.
http://msdn.microsoft.com/en-us/library/office/aa140084(v=office.10).aspx
I beleive this covers what you are asking