Access 2016 Form auto-populate based on another field - ms-access

I've got two tables that contain the following.
Table Name: Enquiries
ID
Username
Date of Raising
Time of Escalation
Invoice
Repeat Contact
Query Type
Context of Query
DC
Table Name: PC
PCID
PCarea
Town
DCArea
DCName
I currently have a Form setup with the above fields all selected. I have PCArea setup as a drop down as the field has 2000+ records (Postcodes). What I want is when a person updates this field/drop down/combo box for Access to lookup the PCArea and find out what the DCName is relevant to this and then populate the DC field in "Enquiries" and show on the form but I can't fathom how to do it.
I know it's VBA related but I can't seem to get the format right?
Private Sub Combo472_AfterUpdate()
Me.PCArea = Me.Data.Column(9)
End Sub
Sure didn't work! Any help would be great.

Replace {PCAreaDropDown} with name of your dropdown box, and all you really need is the one field in there. It looks like you threw at least 10 columns (9+1) based on your example.
me!DC = dlookup("DCName","PC", "PCArea = '" & me!PcAreaDropDown & "'"

Related

Populate two rows of a combobox with a query Access 2007

I'm working on an Access 2007 database and I'm having a problem with a query.
I have a table named Vehicles, which contains data such as ID, license plate and fuel type for each vehicle in it.
I'm trying to make a query which will populate a combobox in a Form with each vehicle's fuel type, based on the license plate chosen by the user beforehand.
The thing is, we have some some cars that work with two types of fuel and I cant' find a way to display them separately in the combobox.
So far it kinda works, code follows:
CheckDiesel: IIf([Diesel]="Yes";"Diesel";IIf([Gasoline] AND [Ethanol]="Yes";"Gasoline"+ "Ethanol";IIf([Ethanol]="Yes";"Ethanol";IIf([Gasoline]="Yes";"Gasoline";""))))
If you look at the second IIf, I have a condition for a bi-fuel car. I want to display Gasoline and Ethanol separately, each one in a row.
I've tried using "&Chr(10) Chr(13)&" and "\r\n" but I had no success so far.
Can anyone help me?
Storing multiple pieces of data in a single field rarely ends well. A few options I see
A series of binary fields for gasoline type. So you'd have a True/false for gasoline, ethanol, and diesel. This is easy to show with checkboxes on the form.
If you know there will only be certain combinations, like if there will not be a 'diesel and ethanol' without gasoline, you can build it as a single value combobox.
You will probably need some VBA to do what you need to do. Try something like this:
Dim R as String
R=""
if (Me.Diesel) then R = R & "Diesel;"
if (Me.Gasoline) then R = R & "Gasoline;"
… {add any other types of fuel}
Me.MyComboBox.Rowsource = R
Me.MyComboBox.Requery
This can be added tot he form's OnCurrent event so it will update the combo box every time a new record is displayed.
If your form does not include the various fuel types as fields, either add them as hidden fields or use DLookup to read them off the table.

Microsoft Access 2010 filtering data based on tempvar

i have a web database and im trying to filter a datasheet, based on the contents of a tempvar. Im trying to use the record source property of the datasheet to do this.
I need to do this because, every employee that logs in should only be able to see a given subset of data in the products table. In the employee table, i have an extra column with a string value which is the data that particular employee should see.
I have a login form that on clicking login, adds this string to the tempvars collection.I can see the tempvar has been added in the immediate window as shown below:
?tempvars!tmpgrpdsc -> "IAMS"
i use the query builder option to complete the record source property as shown below.
The problem is, nothing is returned !
But when i enter the string "IAMS", i get records returned.
However, i have done this with another datasheet and it has worked, the tempvar here held a number ! See below:
What am i missing or is there a better way to filter records based on the login. Thanks
What you showed should work.
However, have you tried to change the criteria to ="""" & [Tempvars]![tmpGrdsc] & """"
Also, to make sure that your tempvar is actually containing the data during the query, you could show it as a field, just to check exactly what data is being returned during the query:
SELECT Orders.*,
[Tempvars]![tmpGrdsc] AS TmpGrdsc
FROM Orders

Access 2010 - combobox - unbound form - displaying data from an existing record

The problem I am having is displaying the correct information in a combobox on an unbound form when a users views/edits data.
The table that populates the combobox:
tblLocation
idLocation
Location
Location Description
In the tblPerson table, there is a FK field called idLocation. I have a form that allows a user to pick a person from a listbox and displays the information in textboxes and comboboxes.
The combobox is setup with these items:
idLocation <--- column width set to 0
Location
The problem I am having is having the data show up correctly in the comobox when I view/edit a new person.
When a person is selected from a ListBox, the information from tblPerson should display in textboxes and comboboxes. The textboxes work just fine. However, I'm struggling with the comboboxes. Keep in mind all of the fields
My research finds only two methods on solving this problem:
DLOOKUP
Manual check and set
If I use the DLOOKUP method:
cmbLocation = (DLookup("Location", "tblLocation", "idLocation=" & .Fields("idLocation")))
The problem is that msgBox cmbLocation will display the text and not the FK. If the user tries to edit the data, but makes no changes, it will try to save the text and not the FK.
I found a manual way that does work, but I'm not sure it is the best approach:
For i = 0 To (cmbLocation.ListCount - 1)
If Val(cmbLocation.Column(0, i)) = Val(.Fields("idLocation").Value) Then
cmbLocation = cmbLocation.ItemData(i)
Exit For
End If
Next
Again, this works - but I have to think that I'm doing something wrong - probably something obvious.
Is there a better way to do this?
you can dynamically change which data is displayed in a combobox. in your scenario, i suggest you use the OnClick event of the listbox (once the person is selected). Add the following code:
YourComboBoxName.RowSource = "SELECT * FROM tblLocation WHERE idLocation=" & FK
If after clicking on a person, the data doesn't change in the combobox, you may need a Me.Refresh
Base the form on a query that left joins in the location column.
Then that field when bound to a text box will will display automatic and without any code. And better is if the actual location value changes, then no update code etc. is required. In fact this is the whole beauty of a relational database!

Access 2010 VBA Form Pulling In Table Data

I have an "Invoices" table, which I run an "Invoices From off of to create invoices for my clients. All my client data; address, hourly rate, etc. is on my "Client Lists" table. The Tables are linked together by a common account/client ID.
I don't want to update general items like hourly rate, address, every time I create an invoice. I am looking to set up an after update event that will auto populate these general items from my "Client Lists" table when I enter the client ID on the "Invoices" form.
---Update---
Below is the code I am working with, and I keep getting an error "Expected: list separator or).
In normal speak; The service type field on my "Invoices" form, should look at my Service type field, on my Client Lists table, for the same Client ID as Account ID.
Private Sub Client_ID_Change()
Service_Type = DLookup("Service_Type", "Client Lists", "Account ID=" & Client ID)
End Sub
Have you tried anything?
The short answer is:
create a query to represent the data you want. In vba use a recordset to run the query's logic for the ID you've got, then display the returned data in the recordset on your form.
However there's no detail here, as you have little yourself
Syntax error on the dlookup function.
To specify a search criteria (last parameter of dlookup) based on a field that contains a blank space, you will have to enclose it in brackets. Also consider that control names can have blank spaces, but to reference them in vba you must replace write them using underscores.
"[Account ID]=" & Client_ID
I would strongly suggest you name table fields and form controls without blank spaces.

Filtering A Lookup Field Based On Another Field

I have a lookup field in my table based on another table. I'm having trouble filtering those values based on another field that is entered prior to the field.
Is it possible to filter a lookup field based on another field?
EDIT
Let me try and clarify my original question, sorry about that. Ok, so I have a table1 that has the following fields: ID, Name, Logo.
If a user enters a specific name in the Name field, when they click on the Logo field, it'll only display those values associated that are similar to the name entered. Does that make any sense? If it does make sense, would there be an easier suggesion on accomplishing this task?
If you're talking about inside a table, the answer is "No". You can create cascading combo boxes on a form, but you can't base a lookup value in a field of a table off of a different field in that table (or the field in any other table).
Here is an example of how to handle filtering a combo box based on the value selected in another combo box:
I have the following form:
The combo boxes are named cboIntPN and cboManPN.
The Row Source for cboIntPN is set to: SELECT uniq_key, part_no, revision FROM inventor. The Row Source for cboManPN isn't set to anything.
When the user selects a value for Internal PN the following AfterUpdate Event is triggered:
Private Sub cboInternalPN_AfterUpdate()
[cboManPN].RowSourceType = "Table/Query"
[cboManPN].RowSource = "SELECT uniqmfgrhd, mfgr_pt_no FROM invtmfhd " & _
"WHERE uniq_key = '" & cboIntPN.value & "'"
End Sub
It sounds like he is having the same issue as me. I also wanted to filter a field in a table for data entry on another field's input and my conclusion is "it is time I stopped entering data manually in tables and begin to create Data entry forms. I was putting this task off until later, but if I don't do it now, I might make worse trouble for myself later.
Btw, what an old thread.