Updating a subform from a list on form - ms-access

I have a list control on my form. The values of this list come from a query. the Row source of the List is like this:
SELECT tb_lable_Daten.name
FROM tb_lable_Daten;
and Control Source of the list is name
I want if the user change the value of the List(with mouse, key down,...) the value of controls( 3 Texts) in Sub form changes too. The query in Sub form should be:
SELECT XValue, YValue, Wert
FROM tb_DCM_Daten
WHERE (tb_DCM_Daten.name)=name); // It is List value
I put this query on Subforms recordsource but that doesnt work.
Could you please tell me how can I do that?

I know I'm coming in three years late. However for anyone that comes here... The .Requery() is needed but the main issue is that for the Record Source for the field in the subform it should read =[FORMS]![MainForm]![ListField] where mainform = the name of the parent form and listfield = the name of the list field on the parent form. In addition having the .Requery() on update would push the new value to the subform.

Related

Indepedent Controls/Fields on Multirecord form DB ACCESS

I have created a simple inventory management Access DB which in its basic functionality works perfectly.I would like to start to make some polish here and there and I've found the next issue.
I do have a main form for inventory transactions. It has a Header section (with a table of transactions header as a recordsource) and a details section (datasheet type subform into the main form; which means a multiple records form where I can add or remove multiple lines for each header transaction and it is linked to the main form, that works OK).
In this detail/lines subform I have the fields: Category (combobox), Item (combobox) and Quantity (category and item fields are linked to "master categories" table and "master items" table).
I do want to add the following behavior:
When I select a category on category combobox field, the item field updates its selection to show only the items in the selected category.
I can do this easily on a single record form with this code:
Private Sub Category_AfterUpdate()
Me.Item.Requery
End Sub
On the Item field datasource I do have a simple query:
SELECT ID,DESC FROM ARTICLES WHERE ARTICLES.CATEGORY = [FORMS]![SUBFORM].[CATEGORY]
This works flawlessly with a single record form.
However as this is a multiple lines/records form when I select a category on a line, it updates the "Item" selection field for ALL the lines/records, even changing values depending on the selected category.
Is there a way to update controls/fields in an independent way for each record on a datasheet form? Let's say something like:
Private Sub Category_AfterUpdate()
Me.Item(current record).Requery
End Sub
I'm thinking on adding an index to the combobox article field and update only the field on that index but I don't know how to proceed or if it's possible to do that.
Any ideas or workaround to do this?

How to get a subform field value?

I have an Access form with (continuous) subform and one of the combobox fields on the subform is populated with data depending on the value of another field.
For this I use the following in the Data Row Source:
SELECT VendorName FROM VendorsPerAction WHERE (Action= Forms![LocalSubformActions]![fldAction]) UNION SELECT distinct null FROM VendorsPerAction ORDER BY VendorName;
This works fine when I test the form outside the mainform. But when I test this as part of the mainform Access keeps asking me for the parameter. I tried changing it into:
(Action= Forms![LocalRequest].[LocalSubformActions]![fldAction])
and many other variations but I keep getting the parameter question.
Is there anyone who knows what I should use? Thank you!
Probably
Forms![LocalRequest].[LocalSubformActions].Form![fldAction]
(assuming the subform control has the same name as the subform)
See Refer to Form and Subform properties and controls
--> Forms!Mainform!Subform1.Form!ControlName

Populating listbox from another listbox selection

I am pretty much a newbie to using VBA in Access and I'm having trouble with something that seems like it should be quite simple.
I have two listboxes (called LB1_ID and LB2_ID) on my form (MainForm) that I want to list related IDs from their respective Row Sources. I need LB2 to be populated based on the selection in LB1. They both have Row Sources from the same Table (Table1) and it is a many to many relationship of Requirement IDs ("Req ID1" and "Req ID2"). My current form, which is not working, has the Row Source of LB1 as:
SELECT Table1.ID, Table1.[Req ID1] FROM Table1 ORDER BY Table1.ID;
and the Row Source of LB2 as:
SELECT Table1.ID, Table1.[Req ID2] FROM Table1 WHERE ([Forms]![MainForm]![LB1_ID]=Table1.[Req ID1]);
When I make a selection in LB1, nothing happens in LB2. The column widths are formatted correctly and I can get it to work if I use Me.[Forms]![MainForm]![LB1_ID] but I have to type out the LB1 selection manually in a popup box if I use that.
What am I missing?
If your listbox is multi-select, you cannot use a simple form reference as query criteria. If it is not multi-select, keep in mind that its value may be a hidden column (usually an ID field), so there are two possible issues and solutions:
Possible Issues:
Single-Select listbox has an ID field that is hidden (column width = 0") and you are matching it to the wrong field in your table. To check the output of the listbox, open the VBE and type ?[Forms]![MainForm]![LB1_ID] into the immediate window and press enter when your form is open in form view and a row is selected in LB1_ID. If the returned line is what you expect, then the problem must be elsewhere.
Multi-Select listbox property is enabled. In this case, your query will not work, because the listbox will only return Null. You will need to write some VBA to loop through the rows and figure out which ones are selected, which is a bit of a pain. Ultimately you'll build some code that will alter your query with the specific criteria for each selected row. Instead of explaining here, take a look at this article for a tutorial.
The .Requery method is still important to put in the AfterUpdate event of your first listbox to refresh the second.
Your query seems to work, but you need to refresh your listbox2 whenever you make selection into listbox1, so if both listbox are in the same form add this event handler :
Private sub LB1_ID_Change()
Me.LB2_ID.Requery
End sub
Without this, your listbox2 will only get populated once on load based on the initial value of listbox1.
Also, if you have not already done it, I would recommend to add your listbox1 control as a parameter into your listbox2 query (in query builder, right click -> parameters).

Update linked fields in access

I have a form that contains a continuous subform that is linked with ID,Date and Shift fields.
ID is not changable in mainform. but date and shift fields can be changed.
The problem is when i change the value of Shift or Date in mainform the corresponding values are not being updated.
EDIT
I made a query to refresh the table of continuos form. It works. But i need to use this code in mainform.
UPDATE SubTable, MainTable SET SubTable.[Date] = [MainTable].[Date], SubTable.[Shift] = [MainTable].[Shift]
WHERE (((SubTable.ID)=[MainTable].[ID]));
I think you need to link the two forms only by a single field. In My scenario I created a field on the sub table called "MainLookup" (Integer). This links to field "ID" on the main form. Changes are made to Date and Shift in the main form. If you were to create a query based on the sub and main you would have only one record of Dates and shifts for each ID. Looking at forms, I created a Main form and a sub form linked ID:MainLookup. The sub form only shows data in the sub table, but the Date and Shift data are intact and as recorded on the main form.

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!