MS ACCESS Multiple Record Visibility If Statement - ms-access

I am building a form in Access 2013 and I will print out 4 fields for each record.
So Let's say the form processes 2 records it will print out:
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Now I want to have it when it displays Other to print out the description of the other from a table. To do this I want the field named DefectType to go invisible and then the Other field to become visible. I start with Other being invisible and DefectType Visible and place them on top of each other. Here is the VBA:
If DefectType <> "Other" Then
DefectType.Visible = True
Other.Visible = False
Else
DefectType.Visible = False
Other.Visible = True
End If
It works fine if they are all other because what it does it takes the first record value of Other and applies it to all the other records for that field.
So if the first record that is displayed has a field 3 and it has a value of Other it will then look for the value in the table that is under the "other" field and display the content. SO lets say the "other" content was lamination. Instead of showing Other it will make that DefectType field invisible and show the "Other" field which will show Lamination.
The problem is it then does this for the rest of the records. It won't test to see if the field 3 is "Other" or not, it will just assume and then put it's "Other" field contents for the subsequent records. This means that since some actually already have values in them, like Corrosion, and nothing in the "other" field it will display a blank box.
I want it to test each field 3 as it is displayed so it can tell if "other" is in the field or not.

I'm unsure how well I understand your question, but it sounds to me that your form includes 2 text boxes named DefectType and Other. When the value of DefectType is "Other", you want to display the value from the Other text box. But when the value of DefectType is anything else, you want to display the value of DefectType.
If that is correct, you can create a new text box named txtSummary and use this as its Control Source property.
=IIf([DefectType] = "Other", [Other], [DefectType])
The txtSummary text box would not be editable. To change its value, you would change the values in DefectType and/or Other.
That approach will ensure txtSummary is always updated based on the current values of DefectType and Other in the same record. And it will do that for each record in the form, including when the form is in Continuous Form or DataSheet View.
Your original approach would do what I think you want only when the form is in Single Form view.

Related

Clear out dropdown value once another drodown value is changed in vba 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.

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))

MS Access: Why is my combo box displaying the wrong value?

I have 2 columns that populate my combo box. They come from a table where I manually wrote in an ID and a clean_value field.
The goal:
Display both columns on a combo box on a form. The ID values should be on the left of the clean_value values when looking at the drop down list. When selecting something in the list, the combo box should display the clean_value, not the ID. When saving the form, the form should write the ID value to a different table.
What I've tried:
I set up the table to get the values from. I set up the combo box to show the 2 values in the correct orientation. The ID value is the value that is being written to the table.
The problem: When the user selects one of the values in the list, their selection shows the ID. I want to display the clean_value upon selection instead. How do I do this?
I found a way to do this. I grabbed the clean_value field, the ID field, then the clean_value field again in the row source, then I just set the width of the first clean_value field to 0.021".

Referencing list boxes in Access

I'm new to Access so this might be a simple question.
I have a form in Access 2013. There is a subform displaying a table from an SQL server, like so.
Company Product
-----------------
CompanyA Product1
CompanyA Product2
CompanyB Product1
CompanyB Product2
Using ListIndex in a list box, I can display the index of any row I click on. For example if I click on the second row (CompanyA, Product2) the list box shows a ListIndex of 1. If I click on the third row the ListIndex is 2.
How do I get a list box to display the value of a column instead of the ListIndex?
What I am trying to do is that, when I click on a row in the subform, I'd like to display each column value for that row in its own list box.
However, I cannot seem to use ListIndex as a variable in a larger function. I've attempted the following:
Typing only the column name into the list box. Does not update the value if I click on a different row.
Column property does not update the value if I click on a different row.
Controls property gives an error.
Value property displays the correct row but only works for the first column.
Combo boxes circle back to the problem that I need to use ListIndex as a variable.
Is there a different property I should be using? Am I missing something in the properties I tried?
There seems to be a little confusion with terminology. Your List3 is a list box, not a subform.
The fields Company and Product look like text boxes, but if the first one has the control source =[List3].[ListIndex], and shows a text and not a number, it seems to be a listbox with height = one line.
I suggest using text boxes for Company and Product, with these control sources:
=[List3]
for the bound column. Alternatively, for consistency: =[List3].Column(0)
=[List3].Column(1)
for the second column.
These text boxes update themselves automatically when you click on an item in the listbox.
To get the value of a listbox, and if you allow multi-select, use the following example:
For Each varItem In Me.lstHierarchy.ItemsSelected ' Loop through the items selected (if multi-select allowed)
strPrint = ""
For i = 0 To iCols ' Loop thru each column in a row
strPrint = strPrint & "|" & Me.lstHierarchy.Column(i, varItem)
Next i
Debug.Print "Selected Value: " & strPrint ' Display the row; columns are delimited with '|'
Next

Access 2007 reports, hiding fields?

I have a report in Access 2007 thats populated with data from SQL SERVER (running on a vb6 application). This report has two subreports that display data. The first Subreport has a Label, "CHILDREN" and the subreport next to it displays Names of children. The 2nd subreport has a Label "PETS" and the subreport next to it displays PetName, and TypeOfPet. In most cases there are pets in every family, however, for some clients, there are no pets. What I'm trying to do is make the label PETS invisible if there are no pets, so the Label is not on the report by itself. How would I go about doing that?? Is this something I have to code?
The following link shows how to hide the label if no data: https://forums.techguy.org/threads/solved-access-2003-hide-field-labels-on-reports-when-value-is-null.660825/
Below are the steps required:
1. Delete the label from the text box.
2. Add new text box in place of the old label.
3. Format the new text box same as other label.
4. Set it's "Can Shrink" property to "Yes".
5.Bind the 'new' label to an expression that will solve to "" if the [Pet] field is blank or to the text string "Pets:" if the [Pet] field is not blank.
6. Change text box Control Source field on the Data tab of the Properties window. In it put:
iif(isnull([Pet]),"","Pets:")
This will put a zero-length string into the text box when the [Pet] field is null and the text "Pet:" when it is not null
7. If the field is blank, it could be null or a zero-length string (""), or could have any number of blank spaces in it. Rather than use "IsNull", use a combination of functions that will solve. i.e.:
Iif(trim(nz([Pet],""))="","","Pets:")