Access combo box for DB mapping table 2-1 Table relation - ms-access

I've run into a bit of a snag with a project I'm working on, and being new to Access I don't know if what I want to do is possible without VBA. I've looked around but all I can find are answers related to showing multiple columns, not controlling multiple fields in my DB.
To paint a picture I have a mapping system set up in my DB to help me distinguish the name and type of data is held in a table.
The setup is as follows:
-Data table is "LineItems" with an ID and the line data (think typical excel format)
-Mapping table is "LineItem_Mapper" with LineItem_ID, DataType, and Entity_ID
-A helper table "Data_Type" with ID and Name
-two "Entity" tables with differing properties both have ID and Name
The reason for the split is on data type is that the two types of data behave differently. One type has a parent child relation, and the other is a standalone row. I want to preserve this structure in my DB and feel I have done so with this mapping.
Now, on to the issue I'm running into. In my Access data entry form I want to use a combo box, as the options a user may chose for each line when entering are finite. However, this combo box is affecting the Mapping table above. I have been able to populate the box with my desired list with a custom query built from my 2 entity tables, but I don't know how to get Access to create or update the Mapping table using this box.
what I want to happen is when I chose something in the box, a line is created (or changed) in the mapping table with all 3 columns being populated. first the LineItem_ID for the line I am populating, and then the DataType and Entity ID to reflect the proper mapping.
Can Access do this on its own? Or do I need to do this with VBA?

As requested by the OP converting my commend as an answer (with a little bit more detail):
By far your best option is to use VBA. I doubt there is another way and even if there is it would be so convoluted it would be unworkable and unmanageable.
This should get you started:
In the combo box properties go tot the events tab and in After Update or On Change (look up the difference between the two events to see which behavior you prefer) click the down arrow and select [Event Procedure], then click on the … button. This will create a VBA module for you complete with the function that runs when the selected event is triggered.
You can use DoCmd.RunSQL "[Access SQL INSERT statement]" to add records to tables.
You can use Me.[MyComboBoxName] to get the current value of the combo box. Similarly the value of anything else in your active form.
You can use DLookup to get the value of any record in your tables.
Hopefully these will give you a relatively quick start.

Related

Access: Bind Combobox to recordset but disallow alterations to recordset on change/update

I'm facing an issue with Access Comboboxes that may be a terrible idea or be an improper use of the tool.
Let's say I have a form that shows metadata of a "Report" (things that are common in the report from day to day like reporter, project, target due date, expected total cost, etc...) with a Continuous subform of line items that are being reported (like cost of labor, materials, overhead expenses, etc)
In the backend I have 2 tables to represent this. The "Metadata" table and the "line items" table. When trying to capture individual items being reported the reporter is free to name the item as they see fit, however, in the system we want to map that reporter item naming to our internal item naming.
In order to do this I have a few helper tables and a mapping table to create this link. An 'Internal Line Item" table, a "DataType" table and a "Line Item Mapper" table.
The structure would look as follows:
- "Line Item" table with ID, Name, Metadata_ID and data (typical excel format)
- "Metadata" table with ID, and metadata common to all reports
- "Internal Line Item" table with ID and Name
- "Line Item Mapper" table with Internal_ID, LineItem_ID and DataType
- "DataType" table with ID and Name
So on to what I am having trouble doing.
I want to display a combo box in the subform for each row and the drop down will have a list of possible internal names that can be selected. I thought that by using an unbound box I could do this, but found that each row would get a copy of the same box (thus changing the selection for all rows at the same time).
I then tried to bind the box to the recordset using the ControlSource, but it changed id numbers when doing so (obviously not ok).
I then thought to tie the box to the Mapping table itself, but don't know how to couple it to the recordset as well...
What I want to do is bind this box to the recordset without allowing it to change any data in the "List Item" table because this box is meant to change the mapping table. I could then use the OnChange event to populate or update the mapping table with the proper data.
Is it possible to create such a binding (where the control is tied to a recordset, allowing me to get the LineItem ID, but not able to change the recordset while still allowing the user to interact with it)? Or is there a better way to approach this issue?
Turns out that I was just doing something foolish with my data model that lead to my troubles.
the full answer may be found here: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_access-msoffice_custom-mso_2013_release/bind-a-control-without-data-alteration-access-2013/51a98460-6e1a-425d-8f7b-eba5f2825ba9?tm=1552503037022&auth=1&page=2
but the short of it is that I was improperly placing all my FK's in a single table trying to make a form of "mapping" table. I have since corrected my structure and have gotten my combobox to work as intended.

Lookup tables displaying number instead of text [duplicate]

I am trying to create a report putting a field called contact which has the name of a person. This name is linked directly to another table where I keep all the contacts.
For some strange reason, when I include this name (which in query view displays as the name of the contact), instead of the name appearing, the unique ID number is shown on my report.
As mentioned in the article cited in the above comment, you can use a Combo Box control on your report to do the lookup for you. To see how this can be done, create a new report based on the table containing the lookup field, then drag and drop that field onto the report. That will create a Combo Box control with properties that look something like this:
Row Source: SELECT [Clients].[ID], [Clients].[LastName] FROM Clients;
Bound Column: 1
Column Count: 2
Column Widths: 0";1"
You could use a similar Combo Box control on your actual report to display the client's name rather than their numeric ID value.
Another alternative would be to change the Control Source of the report's Text Box control to have it do a DLookUp() on the table. If the lookup field is named [client] then changing the Control Source of the Text Box to something like
=DLookUp("LastName","Clients","ID=" & [client])
would also work.
I wanted to add to the great answer by Gord:
When using a "web" database (started in Access 2007 I think), you cannot change a report's fields to ComboBox style, nor can you use DLookUp(). (web databases lack a ton of features)
The workaround for this, if you want to create a Web-Report that uses lookup fields, is to create a Web-Query first based on your Web-Table (all the Web-* stuff has a www planet icon over the logo, if you create a new Web-DB in Access 2007+ you'll see what I mean)
So, instead of Table -> Report, you'll have to do W-Table -> W-Query -> W-Report.
Then, the only thing you need to customize to get the data right is the W-Query. Start by trying to reproduce the look in the query to match what you want users to see in the report. Note that here in the query, lookups will work fine (instead of the unique ID's, you get field names like you want). However, this will not carry over to the report. To do that, you gotta get the actual text field name you want into the query:
You should already have one table in your query; start by adding the table that your first lookup field points to. For example, the table I want to print is called Stock_Boards, and it has a lookup field called PCBID_lookup that points to the table Stock_PCBs.
Since you're using lookup fields, there should already be a relationship line between the two tables when you add the second one. If there isn't, something has gone horribly wrong.
Now, see how that line connects two fields on the two different tables? For example, I've got my PCBID_lookup field on my Stock_Boards table, which connects to the ID field on my Stock_PCBs table. If I created a report from this now, PCBID_lookup would be a number, a number that correlates to the ID of a record on Stock_PCBs.
To fix it, I will add the name field I want to show up on the report. In my example, that happens to be a Part Number, rather than the ID. I add the PartNumber field from my Stock_PCBs table to the query, and remove the PCBID_lookup field of the Stock_Boards table from my query.
Since PartNumber is what I want to show up on my report, it effectively replaces the original field (PCBID_lookup)
Repeat for all lookup fields you want in your report.
I had 1 more: I removed the Status field of the Stock_Boards table (which was an ID/Lookup) and added the 'Status' field from the Status table (which was the actual text name)
When finished, your query should look exactly how you want the data to appear, without any special tricks or asking Access to do something unnatural. Save your query, and create a web-report from it. Done!

Access form must be opened twice before combobox values will appear

I have a checklist that is used to perform QC audits. When a reviewer answers a question that requires an exception, a pop-up form will open with the correct exception detail already populated. They then have to provide some further information, which I have set-up in the format of a combobox for a field named 'Condition_Detail.' Some exceptions have only one condition that would be an option in the drop-down whereas others have multiple possible conditions, which is why I don't have that field automatically populated as well. I have a table of possible exceptions and conditions that I have used to create the combobox query. When the database was created, the field the 'Condition_Detail' combobox is dependent on was labeled 'Exception Detail' with a space and in my new table it is called 'Exception_Detail.' I did this because the old data and tables will eventually become irrelevant and I know that it is easier to write code with underscores instead of spaces. I include this information because I had to create a relationship between those two fields in the combobox query so that the database would know they are the same. When I test the database, the form opens as expected with the exceptions already populated (NOT in combobox format - the data appears as text on the form). The issue is that when I select the combobox, it is blank. If I answer the exact same question the same way to trigger the same exception to open in a new record, the combobox does have the correct data in it. I have tried to requery the field both when the form loads and after it updates. It still won't show the combobox values unless I trigger that the form open twice with the same detail populated. An additional piece of information that likely doesn't affect the problem is that the combobox is set-up as three columns so that I can populate two additional fields when the condition detail has been updated. I used the code below to populate that, which works perfectly when I can get the condition detail to appear in the combobox:
Private Sub Condition_Details_Change()
Me.Responsible_Position.Value = Me.Condition_Details.Column(2)
Me.Severity_Description.Value = Me.Condition_Details.Column(3)
End Sub
Thanks in advance for your help!
I figured it out. The relationship I created within the combo box query was unnecessary. The query was searching for values in the table that stored the actual responses/conditions instead of the table that stored the available response options. Because of this, once I had answered the question it knew what to look for, but in the wrong place. Setting up the combo box the usual way with the look-up control value in the form referenced as follows was sufficient to get the combo box to work properly.
Field: Exception_Text
Table: tblPreCloseExceptionDescEnc
Criteria: [Forms]![frmEncompassExceptions].[Form]![Exception Text]

Matrix of boolean values in an Access form

I am designing a form at work where I need to be able to set "Properties" for a large number of (accounting) "structures". I have a "Value" field where the user enters the value that property must take and then I have 1 column for every structure where the user must be able to check / uncheck each property for each structure. Also, I need to be able to suggest checkbox values (aka mapping of properties to structures) to the user so that he/she doesn't have to manually click all the checkboxes that will always need to be ticked. Finally, the number of properties (rows) and the number of structures (columns) should not be assumed to be fixed though I don't want the user to be able to modify it himself. I just want it so that a dev (probably me) doesn't have a hard time adding or removing structures.
For now I have used a local table where each structure is a column and I have hardcoded my properties (which is good). However, I am not sure that using a local table is good design. We normally avoid having forms and tables in the same Access DB to separate forms and data. Also, I'm wondering if there is an elegant solution that I am missing. There will be at least 10-15 structures and 11 properties, that would make 110 (11*10) checkboxes to handle so I cannot do it manually (i.e. create 110 checkboxes and check 110 values everytime...).
Here you can see what that part of the form looks like for now.
I know this will be a chattier question but I really need a design check on this so here are a few questions that I try to make as general and objective as possible:
In Access, how is it possible to create a matrix of controls where 1 column contains a fixed (but changeable by a dev) number of properties, a "value" field that can take text and then 10+ columns with Yes/No values ?
Is it possible to do it without a local table ?
VBA is perfectly admissible.
Thanks.
In Access, how is it possible to create a matrix of controls where 1 column contains a fixed (but changeable by a dev) number of properties, a "value" field that can take text and then 10+ columns with Yes/No values ?
I've never seen any VBA code that does what you describe. VB6 allowed the creation of "control arrays" to logically group controls (and work around some limitations on the number of controls on a form), but I've never seen that mentioned for VBA.
Is it possible to do it without a local table?
Is it possible? Maybe, since you can modify a form via code by opening it in Design View and using CreateControl() to add controls.
Is it practical? Probably not, because the "Access Specifications" section of Access Help mentions the following limit...
"Number of controls and sections that you can add over the lifetime of the form or report: 754"
..so it sounds like code that repeatedly modifies a form could very well break after a while.
My recommendation would be to create a temporary table, use it, and then discard it. If you're worried about front-end bloat then you could create the temporary table in a temporary .accdb file and then link to it.
Maybe I'm missing something, but it seems pretty easy to me.
Make each structure a record.
Then in another table, make each property a record with a 1-many relationship with the structure table. So, each structure will have many properties.
Then a form based on the structure with a sub-form based on it's properties.
Default property values can be set in the table structure.
And of course the tables can be linked from another DB.
In Access, how is it possible to create a matrix of controls where 1 column contains a fixed (but changeable by a dev) number of properties, a "value" field that can take text and then 10+ columns with Yes/No values?
I'm still not certain why you want multiple bits/booleans in a single column, but you might be able to utilize bit logic and long integer column. VBA does something like this with its constants. For instance, in a MsgBox, you make your type = vbCritical + vbYesNo. Its result is in an integer value that VBA interprets to make a Critcal MsgBox with only a Yes and a No button. The reason this works is because the value of each constant is so distinct that any summation with like constants results in a unique value that can be de-parsed. It's fairly elegant from a user perspective, but I'd hate to do the math on the back-end to make such a function work.
It might be better to maintain a separate table of available properties and/or property sets and build / maintain your property sheet that way. You could assign property sets as well as individual properties to particular controls. You could also specify in either table whether there is a default value for that property and/or what that value is.
It is also likely that you do not need that many bits. I'd be happy to narrow my answer to your situation if you could update your question with more detail regarding the nature of your bit need(s).
Is it possible to do it without a local table?
You might be able to create a DAO.Recordset dynamically in Access.
It might be better to adjust your properties table(s) (see above) to be a permanent table in one of your databases rather than re-populating the same data every time.
You could take more of a master/detail approach, combining your bit fields into a string description describing the boxes which are checked.
For example, if you have a structure named 'structure1', value 100, and 5 condition bits set, you could have one string column with text 'cond1, cond2, cond7, cond8, cond9' and then another button allowing them to modify that set of conditions for that single structure.
You'd have fewer checkboxes, anyways.
The only thing I can think of equivalent to 'a matrix of controls where 1 column contains a fixed (but changeable by a dev) number of properties' is an editable query result set.
You'll need a table for your structure -> default conditions set, if nothing else, but it doesn't have to be in the same db as the front end.
HTH

How to look up values from a table in Access

I have an Access database that is used to store basic info in a table such as first and last name. How would I go about adding the functionality to lookup by last name?
Is there a way to type in the last name and then hit like F12 or something like this? Can someone please point me in the right direction or provide me a link?
SELECT tblPatient.LName AS [Last], tblPatient.FName AS [First]
FROM tblPatient
WHERE (((tblPatient.LName)=[Enter Last Name]));
How do I tie this into my form now?
I'd suggest you create a form, with a textbox 'search' at the top, then either a listbox or subform below to display results.
The listbox record source would be:
SELECT tblPatient.LName, tblPatient.FName
FROM tblPatient
WHERE tblPatient.LName LIKE Forms!myForm!search & '*';
You can either add a Search button, which requeries the listbox, or do the requery via the Change event of the search textbox. The later may be slow if you have a large number of records; if that's the case, you could check that at least 3 (?) characters have been entered before calling the requery.
You just need to create a query in which you put =[?] as the "last name" value.
When you open that view, you'll be asked to type in a lookup value for that field.
Not sure if this is what you are trying to archieve, though...
This is probably a bit overkill for what you want to do, but I assume that you want to perform a search by last name. You should be able to glean the information you need from this article:
Build a search criteria form
http://www.everythingaccess.com/tutorials.asp?ID=Build-a-search-criteria-form
You can create queries in Access if the user you're targeting with the searchability has Access themselves.
From the main Access UI (assuming Access 2007), go to the Create tab and then select the "Query Wizard." Here is an article on the subject.
Otherwise you can create a program and connect to the MDB/ACCDB file running the query programmatically.
Seeing you wish to look up a name and populate the form based on the name selected, I suggest you need a combobox. There is even a wizard for doing exactly what you want. To start, you will need a form bound to a table or query, that is a form with a Record Source.
Add a combobox to your form
Select :
Find a record on my form based on the value I select in my combobox
Select the ID (primary key), Last and First name fields.
Access will display an example, suggesting that you hide the Key (id) column. Accept this.
Choose a name and finish.
There are a few other small things that could be done for neatness, but you will end up with a form that find the record you want. In addition, the combo will autocomplete if you type in a few letter.
If this is an mde, which your subsequent post seems to suggest it is, there is little you can do wuth out the original file. However, you could try opening the database while keeping the shift key held down and see if that allows you to edit. If you cannot get the original and the shift does not work, you could try rescuing the data, if it, too, is stored in this file.