简体   繁体   中英

Form and subform based on same table in ms access 2010

I have a customers table in an ms access 2010 database including such things as CustomerID, FullName, Address, City, StateProvince, PostalCode, etc.

I want to set up a main form that contains two subforms. ListSubForm has just a list of FullNames, which the user can filter by typing into a textbox. DetailSubForm then prints out things like Address, City, StateProvince, PostalCode, etc for the specific customer selected by the user from the filtered list in ListSubForm.

All of the examples I can find on the web are for Master/Detail subforms where the master and detail are two different tables with a one to many relationship. That is not what I want. Can anyone point me to instructions for setting up master/detail subforms based on the same table? With one subform just giving more detail about the specific record chosen from the first subform?

Or if you do not have a link to a working example, at least please show me how to set this up? I want to avoid getting too deep into VBA because I am new to it. I would prefer something I can set up using simple macros.

Here are the two subforms on the MainForm in Design View:

Here are the two subforms on the MainForm in Form View: [![][1]][1] And here is the result of clicking the ... button next to the On Current Event in the Property Sheet of ClientListForm: [![][2]][2] Where do I go from here? EDIT: I created a split form as per Yawar's suggestion. It allows the user to select a customer whose data is then shown in more detail at left, but it looks like this: The datasheet is ugly, and the datasheet is not filterable, so the user would have to tab through thousands of records. Is there a way to make it look like it is not a datasheet at left? Perhaps by not making it a datasheet in the first place? The continuous form with a textbox filter in my original example above looked fine. Also, is there a way to make the list of customers at left filterable so that a user can type in a customer's name and then be able to have the list of customers shrunk down to only those with the desired name? SECOND EDIT: After Yawar's edit, I followed the combobox instructions again and got the error message again. Also, there are problems with this approach because it is a drop down list that does not show all the options at once, also the combobox is not filtered like what I showed in my original posting above. However, I am posting the results as follows. Here is the error message that comes up, note that the detail info on right does not match the selected client on left: [![][3]][3] Here is the line of code to which you are directed if you click the debug button in the preceding image: And for good measure, here is an image of part of the datasheet of the underlying table, to show that the field name is correct: [![][4]][4] THIRD EDIT: I changed the code to match Yawar's new suggestion, and got the following error message when trying to change the selection of the combobox: [![][5]][5] When I clicked the debug button, it gave me the following: [![][6]][6] [1]: https://i.stack.imgur.com/6Uy9V.gif [2]: https://i.stack.imgur.com/jRyZO.gif [3]: https://i.stack.imgur.com/RnJyL.gif [4]: https://i.stack.imgur.com/kQxmo.gif [5]: https://i.stack.imgur.com/bDm6C.gif [6]: https://i.stack.imgur.com/5Dqis.gif

You don't need subforms to handle a one-to-one relationship. Put your combo box of full names (let's call it cmbCustomer ) on the main form. But don't bind it to your underlying Clients table. Instead, leave it unbound and fill it with customer full names using its Data > Row Source property. You can use a SQL statement in the Row Source , like:

select distinct FullName, CustomerID
from Clients
order by FullName

Make sure the Data > Bound Column property is set to 2 . You want the customer ID to be captured in this field. Also make sure Format > Column Count is 2 , and adjust some of the widths in the Format tab as necessary.

Now fill up the rest of the main form with all the customer table fields, including the customer ID field. This time, bind them all to the underlying data source. A shortcut I use is to just drag and drop the fields from the listing in the Field List panel (Design > Add Existing Fields). However, hide the CustomerID field ( Format > Visible = No ).

Finally, set a VBA procedure to handle cmbCustomer 's After Update event by navigating the form to whatever customer ID was just selected in the combo box:

Private Sub cmbCustomer_AfterUpdate()
  set rst = Me.RecordsetClone

  rst.FindFirst "CustomerID = " & cmbCustomer
  Me.Bookmark = rst.Bookmark

  set rst = nothing
End Sub

Voila, the form will automatically jump to the chosen customer ID whenever you select a new customer from the combo box.

I know little about Access, but think the following approach is workable: create a detail form and a datasheet form. Do NOT link them via some key, but when the user clicks a row in the datasheet, pass the key parameter (RecordID) to the detail form and trigger a requery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM