MS Access: Dependent drop down combobox in Split form without affecting the rest? [duplicate] - sql

This question already has an answer here:
How to query combo box of only current record/row in Access data entry form?
(1 answer)
Closed 1 year ago.
I am trying to make a dependent drop down work with my database here but it is giving me a hard time for different reasons that I will explain.
This is what I have:
A form called "tblOTS" (split form based on an actual "tblOTS" table):
As well as a table called "tblAlphaCode":
When looking at the property sheet for the "Alpha Code" combobox (actually called "strOTSAlphaCode") on my tblOTS form, this is what I have:
The current SQL statement under "Row Source" for this strOTSAlphaCode is:
SELECT tblAlphaCode.strAlphaCode, tblAlphaCode.strCategory,
tblAlphaCode.ID, tblAlphaCode.numSortingOrder
FROM tblAlphaCode
ORDER BY tblAlphaCode.strCategory, tblAlphaCode.numSortingOrder;
Basically, when user select an Alpha code in the drop down from "tblOTS" form, it adds the ID of the tblAlphaCodes into the actual tblOTS record.
My goal is to have the user select first a "Category" (here FASTENERS/HARDWARE ...) when adding/modifying an OTS record, and then have the "Alpha Code" drop down dependent on what category was just selected.
I first tried to changed the SQL statement using a WHERE condition on the category field, based on the form category field itself:
SELECT tblAlphaCode.strAlphaCode, tblAlphaCode.strCategory, tblAlphaCode.ID, tblAlphaCode.numSortingOrder
FROM tblAlphaCode
WHERE (((tblAlphaCode.strCategory)=[Forms]![tblOTS]![strOTSCategory]))
ORDER BY tblAlphaCode.strCategory, tblAlphaCode.numSortingOrder;
And created a strOTSAlphaCode.requery in my Form_Current event to update it everytime.
However, it affects the whole form itself as my control source is directly affected by my Row Source in this instance, and the form looks like this:
You can see that the drop down is working beautifully, however, all the fields that do not have the same category as the current record that I work on (FASTENER <> HARDWARE here), are missing their Alpha code (the second record is missing "O-RING" compared to the first picture from this post); this behavior is problematic!
I tried to find workarounds by changing strOTSAlphaCode into a simple text box only containing the Alpha Code ID, added an unbound text box on top with dlookup function to find the actual alpha code related to this ID, and an unbound combobox in between with the exact same SQL statement inside the Row Source Property, that would update the strOTSAlphaCode with some VBA ... not ideal right?? Ahaha. This is why I believe there is a simpler way that I am not aware of, but also I would like to use a "search as you type" code for this combobox later on and this solution was making it very difficult.
How can I make this work? I believe the answer is a simple/different SQL statement to put in my Row Source property, that is probably something related to some type of JOIN statement? Or something else? I expect to write some VBA code to make all of this work flawlessly, but I want to make sure that I have the right SQL statement first.

Thank you June7;
Yes, the link you gave me ( this link ) states 2 solutions, and the second one is the one that I talked about when describing my problem:
for forms in Continuous or Datasheet view, include lookup table in form RecordSource, bind a textbox to descriptive field from lookup
table, position textbox on top of combobox, set textbox as Locked Yes
and TabStop No
So I guess, the answer is that I MUST have a textbox with dlookup setup for my case; I put it on "locked" so that if the user wants to change it by typing in it, he will first have to select the arrow. This will work with what I wanted to do
Cheers,

Related

How to display out of list items in a combobox?

I have a tbl_ProjectList that includes the project's Name and End Date.
A qry_cboProject queries this table to only display projects in a combobox on Subform A where the End Date is after the selected date on Subform B, both on Mainform C.
On Subform A, a macro copies (INSERT INTO SQL) projects from Subform B's previous months into the new months. However, if an out-of-date project gets copied over to a new month, the combobox field would be empty for that record, even though the Key exists on the back-end.
I've tried playing with the combobox's properties on Subform A by changing the Bound Column, Column Count, Column Widths, and Limit To List, but am only able to get the out-of-date project to display by its Key, rather than its Name.
The front-end reasoning for this macro is that employees do not have to repetitively select the same projects for each month, and employees already working on out-of-date projects may still need to put in some hours to close out the project.
Does anyone have any suggestions? Thank you in advance!
The order in which fields show up in you combo box depends on how the control source is querying the information i.e. to get name and not the key to show up in a combobox using the a control source query like the following:
SELECT Key, Name FROM tbl_ProjectList
You would need to set the following attributes:
Column Count: 2
Column Width: 0"; 2"
Bound Column: 1
It sounds like you may need to requery the control source as well. This should cause all the information to update.
#Parfait - apologies for not describing my problem in more detail. There are multiple subforms on one main form to allow the user to select a date in one subform, which populates projects on a second subform and responsibilities on a third subform.
Jeffrey's suggestion made me realize that the underlying query to the combobox should be adjusted for projects that are carried over into new months, where a foreign key exists in the underlying tbl_ProjectUserEntry
Therefore, I added a WHERE criteria to the query, which uses a DLookUp function to see if the foreign key exists:
DLookUp("[DateID]","tbl_ProjectUserEntry","[DateID] =" & Forms.frm_UserEntry.tbDateID) IS NOT NULL
frm_UserEntry is the main form..
Again, apologies for my brief description to a complex problem.

MS Access Binding Textfield to tables

I am currently building a MS Access database as a project #Work.
The Database is fed with a huge, not sorted Table DataQueryExport, and the objective is to create a GUI that makes that huge table workable.
Now to the question:
I have a textfield Comment that is supposed to show a comment to a specific data entry WorkID. The WorkID Is stored in a seperate table called WorkID_Comments which is filled via SQL query. The Table WorkID_Commentshas 2 columns, WorkIDand Comment.
The GUI has a ComboBox WorkID_Combo, where you can select a specific WorkID from the main Table DataQueryExport. This exact value is what is supposed to be used as a reference to show the comment for that WorkID.
TL:DR; I need the Comment Box to show and store the Text I wrote for that Value selected in the ComboBox. How do I get the Textfield to show the Value from the Table WorkID_Comments.Comments where the Value in WorkID_Combo(View ComboBox) is the same as WorkID_Comments.WorkID
I apologize beforehand for any weird capitalizations, I am used to it, German is my main language.
Thank you for reading, I am thankful for any response.
Build a query in the query designer with your needed columns (If I understood it correctly: ID and the Comment).
Then go to your combobox and chose under properties recordsource table/query and your designed query.
Now you can specify which colums should be shown and use the ID as the value in the background.
Then bind another query to your form (here is the text you wrote as column) which is linked to the ID from the combox. Put a textbox in the form and use the column as source.

MS Access Form Combobox selection as variable to be used in a query [duplicate]

Pretty simple explanation. I have a table with 10 entries, 5 entries with the year 2010 and 5 entries with 2011 in a column.
In the query I have, I use Like *2010 to filter out all entries equal 2010 and display onl those records.
On my form, I have combobox being populated with each unique year (from a different table). So my combobox values are 2010 and 2011.
Is it possible, when I select say 2011, I trim the right 4 characters and use as my Like criteria to refresh and requery the form, all done within VBA?
You can refer to the value of a control in a query run from within an Access session, as long as the form which contains that control is open.
SELECT *
FROM YourTable
WHERE date_field_as_text Like "*" & Forms!YourForm!YourCombo;
So perhaps you can use a similar query as your form's record source, and do Me.Requery in the combo box's after update event.
If that's not close enough to what you want, please give us more information about the data types of the fields involved. Adding brief samples of the table data to your question could also help; please make it clear whether the "date" fields are text or Date/Time.
Yes, it's possible.Just add your stuff to the 'YourComboName.After_Update'.
Look for the Events of the Form that are fired before the form is shown. There build your query as like HansUp suggested.

Access form - how to make text field have a control source from a SQL query?

Background
I have two tables:
Projects
EmployeeID
Employee
EmployeeID
Name
I have a query I am basing a form on containing, among other items:
SELECT e.Name FROM Projects p JOIN Employee e ON e.EmployeeID=p.EmployeeID
When I make a form in Access based on this query, I can very easily display e.Name on my form because it is joined from the query.
My real example is of course considerably more complicated than this simple example. The above works fine for read-only queries and scales well. However I would like to use a Splitform view and this becomes very slow with many joins for even small numbers of recordsets. Considering a large percentage of my joins are simple like the above, I am hoping for a way to remove as many as possible.
On the form, e.Name will be read only and not be update-able.
Similar question
In this question I am able to successfully change a combo-box into a lookup. The accepted answer allows a combo box to control Projects.EmployeeID by displaying Employee.Name field in the combo box.
Possible work-arounds
I know one way I could do this is use a combo-box but disable it. This would look a bit weird since it'd have the drop down selector but not be selectable.
Alternatively, I could make it a completely unbound field and write VBA code to update the form each time the recordset changes by running quick queries, getting the text value I am searching for, and updating a label accordingly.
Neither of these are overly appealing, however.
Question
How can I display a single text field on an Access split-form which is the result of a very simple query lookup, based on an ID from the main table field?
You can use Dlookup to return this reference very simply
Construct a Dlookup formula like:
=DLookUp("Name", "Employee", "EmployeeID =" & "[EmployeeID]"
and use this as the ControlSource for the textbox.
Some notes:
The & is important as it binds the formula to the single record displayed on the form
[EmployeeID] refers to the current record displayed on the form. This assumes that "EmployeeID" is included in the query for the form, whether bound to the Projects table or included in the query

MSAccess: Change in subform recordsource causes loss of LinkChildFields binding

I have a problem that Im completely stumped by. Or rather, I have a work-around that is messy and I don't understand why what I want to do doesn't work:
I have a mainform / subform nest. The subform is linked by master/child fields (neither form is editable- so Im using the recordsource field directly rather than a separate control). The subform is a continuous form; so the subform displays the records related to the master in the mainform when the current record changes. The subform is based on a query- there are no parameters or filters being applied- the query presents the recordset exactly as needed. This all works perfectly (in that when the record of the mainform changes, so to do the related subform records).
Problem arises when I want the subform to be based on a different a different query (ie based on different tables)- but with the same unique-key (the 'ID') and fields as the original query. The queries all work fine, and displaying them in the form works fine.
The issue arises when I change the recordsource of the subform (so from QueryA to QueryB). The records the subform are correct (ie the query results are displayed as expected)- but the LinkChildFields is no longer functioning: the subform no longer shows the related records from the query- it displays them all. In other words, the form displays the entire recordset as if I had no LinkChild / LinkMaster set.
So, the code in the AfterUpdate event of the toggle control in the MainForm is ;
Private Sub optActRep_AfterUpdate()
Select Case Me.optActRep
Case 1
MainFormSubFormControl.Form.RecordSource = "QueryA"
Case 2
MainFormSubFormControl.Form.RecordSource = "QueryB"
End Select
Ive debugged and the property 'linkchldfields' is still set correctly ('ID'). Ive tried setting these to vbNullString before the recordsource update and resetting after the recordsource update (same issue). The odd thing is if I update the linkchildfield property in the mainform (in the Current Event)- then it works and seems to fire. So,
Debug.print Me!Subform.LinkChildFields
correctly returns 'ID'. The only way I can make it fire is by coding;
Me!Subform.LinkChildFields = "ID"
into the Current event of the Mainform (ie so that every time the MainForm record changes, the correct QueryB record is displayed)... even though Im only setting the property to what my debug is telling me it is already set to. Its as if it no longer fires.
Very confused.
Access continues to do the daftest things.
Nothing to do with the form setup at all. The underlying query (QueryB) was based on a Crosstab query. Whilst the query worked fine independently of the form (including when directly using the query as the forms record source), it obviously didnt like using the product of one of the fields as the Unique key (even though it was the same format type and Grouped / Row rather than being a cross-tab field).
I changed the crosstab part of the query to a Grouped query (using IIF & Sum to manually create the Crosstab-equivalent field values) and the form now works fine.
My lesson learnt- don't use Crosstab queries! Apologies for the red-herring.
Surferosa
Apologies for the late reply, I only found this thread when I encountered problems similar to yours. I'm not sure the Crosstab query is the cause of your problem.
I can set LinkMasterFields to be either a column name or a control name.
I can only set LinkChildFields to be a column name, setting it to a control name does NOT work. Ouch.
You can change LinkChildFields in the sub-form control either in Design View or via VBA, but in many cases Access continues to use the previous value even though it displays the new value. This is true for sub-forms whose record source is either a table or a simple two table query. I do not have the time or the patience to characterise this problem any further. Sorry !
My solution is to set LinkChildFields in the sub-form control to a value e.g 'ID' and then create an alias with that name in each and every record source used by my different sub-forms. Then you only need to change the Source Object when you switch sub-forms.
Hope this helps !
HB