I have a data entry form for a table with information about buildings and am having a problem making a combo box do what I want. I would like to make the combo box show a list of unique bldg mgmt company names and then to open a building management company form to show all records with this name, so the user can find the correct branch location to select. Then, upon closing the building management company form, the active/selected record will be returned in the combo box on the buildings form.
The main form is called frmBldgs. The table that frmBldgs is based on is called tblBldgs.
tblBldgs includes:
Field 1: BldgID autonumber primary key
Field 2: BldgNm name of the building
Field 3: BldgMgmtCoIDFK foreign key linking to a table about the building mgmt company
and a number of other attributes about the building.
The table detailing the Building Management Company is called tblBldgMgmtCo.
tblBldgMgmtCo includes:
Field 1: BldgMgmtCoID autonumber primary key
Field 2: BMCNmIDFK foreign key linking to a table listing building mgmt company names
Field 3: Addr1IDFK foreign key linking to a table listing street addresses
The reason I have a separate building mgmt company name table is that there can be more than one company with the same name (e.g. branch locations with different addresses).
The table listing the Bldg Mgmt Company names is called tblBldgMgmtCoNm .
tblBldgMgmtCoNm includes:
Field 1: BMCNmID autonumber primary key
Field 2: BldgMgmtCoName unique company name
On frmBldgs I have a combo box, called cboBldgMgmtCo, with control source BldgMgmtCoIDFK. Right now, cboBldgMgmtCo shows building names, but it shows all unique records in tblBldgMgmtCo, which means that bldg mgmt company names repeat if there is more than one branch (with a different address and therefore a different record).
The current row source query for cboBldgMgmtCo is:
Expand|Select|Wrap|Line Numbers
- SELECT DISTINCT tblBldgMgmtCo.BldgMgmtCoID, tblBldgMgmtCoNm.BldgMgmtCoName
- FROM tblBldgMgmtCoNm INNER JOIN tblBldgMgmtCo ON tblBldgMgmtCoNm.BMCNmID = tblBldgMgmtCo.BMCNmIDFK
- ORDER BY tblBldgMgmtCoNm.BldgMgmtCoName;
Column Width is 0;1
Bound Column is 1
This results is the name of the building mgmt companies are listed in the combo box, but as I mentioned above there are repeats if more than one record in tblBldgMgmtCo has the same name.
I have also added an AfterUpdate Event for cboBldgMgmtCo to open the building management company form, called frmBldgMgmtCo (based on tblBldgMgmtCo). The code is as follows:
Expand|Select|Wrap|Line Numbers
- Private Sub cboBldgMgmtCo_AfterUpdate()
- Dim stDocName As String
- Dim stLinkCriteria As String
- stDocName = "frmBldgMgmtCo"
- stLinkCriteria = "[BldgMgmtCoID]=" & Me![cboBldgMgmtCo]
- DoCmd.OpenForm stDocName, , , stLinkCriteria
I want cboBldgMgmtCo to list the potential building mgmt company names once each and then open frmBldgMgmtCo filtered to show all records with that name (BMCNmIDFK) so the user can find the correct branch to select. The user can then select the correct BldgMgmtCo record (or create a new one) and when they close frmBldgMgmtCo, the active / selected record will be returned in cboBldgMgmtCo on frmBldgs.
To do this, I assume I need to change the query for the row source to somehow only show unique names, though I cannot quite figure out the correct code. Then, I think I need to change the AfterUpdate event so that it will open frmBldgMgmtCo filtered to the field BMCNmIDFK of the bldg mgmt company that equals the name chosen in the combo box. Finally, I need a button on frmBldgMgmtCo to close the form, once the correct record is selected and add an OnClick Event to this that returns the correct record in cboBldgMgmtCo on frmBldgs. I have a button that closes the form, called btnClose,but have not created the code to ensure the correct record is returned in the combo box.
I am new to VBA and Access and so need help on how to write the code to achieve these ends. Any help is greatly appreciated! Thanks in advance.
Bridget