Passing a value from a main form to a subform - ms-access

I have two tables, tblContacts and tblAddresses which are related by contactID as a foreign key on tblAddresses. Not every address is associated with a contact, as some of them are related to organizations but not people. Currently, the relationship between the tables is a one-to-many relationship between contacts and addresses, as sometimes a contact can have more than one address (office, mailing, etc.) I am not enforcing referential integrity on this relationship because of the above reason- not every address has a contact.
In my form frmContacts, which has tblContacts as record source, I have a subform fsubAddresses, which has tblAddresses as a record source, that needs to be linked to the contactID of the current record. However, the parent-child relationship between the form and the subform on contactID does not seem to be effective- when I set the control source to a text box in my subform to contactID I see a null value, rather than the expected contactID number matching the current contact.
I have created a somewhat patchy workaround and I'd like to see if there is a better solution or a different way for me to structure this relationship.
I created a text box txtContactID in the frmContacts main form with the control source set to contactID then created a locked combo box in the fsubAddresses subform with the default value set to txtContactID and this displays the correct contactID for the current record. I then set the control property of this combo box to contactID so it would pass that value into the tblAddresses field for contactID. This works, but I feel like I have over-complicated the problem. What am I missing?

You do not have to access the contactID of the main form and use it to fill the subform's contactID when adding a new address. Also, I think that you do not want to see the contactID, neither on the main form nor on the subform, and in fact you don't have to. Access uses the relationship defined by Link Child Fields and Link Master Fields to fill the contactID of the subform's recordsource when adding a new address in the subform. This will not happen before the address is saved, so you won't "see" that value on the subform while the record is still new and unsaved.

Related

Access add more than one record at a time using subform

Hello, so I need some help. The image attached is the form I'm working on. I got it working up until the point I can save that record and go back to my query where training records are and see it in there. The save button does not clear any info out of the form, but if I do change the id of 345 to say 346, it won't make a new line. It just rewrites over the 345 in the record. However, what I fully intend to do is have this store multiple records at once and the subform is where you can see what you're going to add once you hit save. This is my first big project, so speak in noob terms. Thanks!
I think I understand what you're trying to do and I'll use an example of a customer with multiple orders to explain how to use a subform to add multiple orders to the same person.
First, you want to have the Customer_ID and then a Product_ID. In the Product_ID table, make sure you have a field called "Customer_ID" which will act as the foreign key that will reference the Customer_ID field in the Customer table. When you add a subform to the Customer form, you set the "Master Field" and "Child Field" to the "Customer_ID". This will allow the subform to reference the Customer_ID field and maintain the relationship and a unique Product_ID (not Customer_ID) will be generated each time you add a record to the subform.
Select the subform and go to "Property Sheet>Data" and double-check the link in the master and child field.
Simple 1-to-many Employee-Task relationship
How the relationship should look like with the Subform
What the main form and subform should look like. You can do something similar with the Standard List Table based on the relationship you've established.

Subform gets records with foreign key, but new records don't get saved with it

I am new to access and trying to create a simple form with a subform. The database has the relationship set. When I select an ID from the combo box linked to the subform, the subform pulls the records with the correct foreign key. However, when I try and add new records, I can't seem to get the foreign key to set with the ID from the combo box. From what I understand it should just save the fk automatically, but nothing I try seems to get it to grab the ID. Here is the link to the simple database so far.
Any specific sections that are of interest I can add.
Thanks!
Don't know how you created the subform, but if you remove the subform and then drag-and-drop the query on the parent form, a new subform is created, and the wizard asks you by default:
Show Order Items for each record in Order by ID
Leave that selected, click next and continue, and your subform now works as expected.

MS Access - Resetting Combo Box within a Subform

I'm having an issue with a control I've built into a dashboard used by our medical credentialing staff.The control is a combobox in a contracting subform that is within a demographic form, the primary key in the demographic form is ProviderId, this is also the foreign key in the contracting table in this relationship.
The combobox is built to allow the association of a ProviderId (an identifier for medical staff) and TINLevelEntityId (an identifier for practices) where there is a documented association already established between the provider and the practice in a junction table that serves that purpose.
I'm able to restrict the values that populate in the combo box appropriately by using criteria in the underlying query that only returns values where the ProviderId in the controls row source is equivelant to the ProviderId in the enclosing form. My issue is that when I navigate to a new record the control does not reset and the values that appeared for the first provider continue to reappear for all other providers. I've tried using the requery command in an after update event but it doesn't seem to want to work. I've tried a few different approaches and am stumped. Would appreciate some help.

Suggestion and auto-complete in form with one-to-many relationship in Access

I am trying to make a form for a very typical scenario: a customer makes an order. The customer might be new or returning. I have created an Access database with two tables: Job and Customer (linked by the primary key 'customer no'). The form I wish to create looks like this:
The customer table data is highlighted.
When the customer's name is beginning to be typed in, I want to see an updating list suggesting possible customer matches. If the customer happens to be returning, I can click on one of the drop-down options and have all other customer fields auto-complete.
What is the best way to create such a form?
I tried to achieve this using a comboBox and some code. However, if I use this method, the comboBox does not allow me to enter a customer name which isn't already in the customer's table. So I can't enter new customer details.
Ideally, this auto-complete/suggestion should work for all customer fields, such as phone number (in case the phone number is the only know customer info).
You could use a combobox which has LimitToList set to false, allowing entry of data not already defined in the combobox's rowsource; With this approach you would define a not-in-list event handler for the combobox to open a popup form for adding a new customer, ideally pre-initialised with the customer name just entered, and when data is saved in that form, control returns to this form, requeries the combobox (as it's contained data is now out of date), re-locks the customer just added, and fires it's after update event to display all the relevant customer-related fields appearing on this form.
However whether this is appropriate or not depends to some extent on the quantity of data that you have. If you've more than say 10000 customers in the database, I'd recommend instead having an elipsis (...) button beside customer name which opens a popup search screen, and provides whatever search options seem best (name, number, city, etc) for the entity being searched, a textbox, and find + add buttons; With huge data quantities you might also want to provide a means to prevent searching for matches (especially by multiple users across a busy network) where there is less than say 4 characters in the textbox, to improve performance.
You'd then use the text_changed event on the textbox to retrieve the data in the database which starts with the text currently existing in the textbox, and display this in a datasheet; The user can then either click Find or double-click the datasheet to select a customer which you then return to the calling form, probably by either a call to a public sub, or setting a tag value on that form - or click Add to add a new customer and invoke logic similar to the above.

Autofill value in subform based on value in parent form

I am creating a subform (in datasheet view) based on a query (qryClientEmployment) to display employment history for the client whose information is stored in the parent form (frmClientInformation). frmClientInformation is based on another query (qryClientInformation).
I have a filter on qryClientEmployment to select records based on the client ID field displayed on the parent form (frmClientInformation).
I WANT to be able to add new employment records to the subform and have access autofill the client ID based on the ClientID field on the parent form.
I have tried, without luck, setting the default value on the Client ID field on the table (which stores Client Employment history) equal to form!frmClientInformation!txtID.
I’m new to Access and am stumped.
When you are new to Access, it is nearly always best to use the wizard to create controls. In this case, it would have offered you the option of adding Link Child Fields and Link Master Fields, and these are what you need. You can edit these properties for the subform control, but you must be careful to select the control, not the form contained. Link child fields will be completed with data from the parent form master field, a misleading name, because the link master field can be a control. You can have multiple link child and master fields separated by semi-colons.
Do yourself a favour and change from datasheet to continuous form, you will have much more control.