Search code examples
ms-accesscomboboxdata-entry

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


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?


Solution

  • 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.