To do this we are going to use the example of a Customer Order form. The main section of the form displays the Customer Order, and the Subform displays the Order Details stored in the junction table. When a user clicks the product link in the Order Details subform, the Product Form opens at the record for that particular product.
Figure 1: The Orders Form. Clicking the product link in the Order Details Subform opens the Product Form (see Figure 2 below) at that particular record. |
Figure 2: The Products Form displaying the record specified in the Subform in Figure 1 above. |
Dim varWhereClause As String varWhereClause = "ID = " & Me!productId DoCmd.OpenForm "frmProducts", , , varWhereClause
It begins by defining a string variable to hold an SQL Where Clause. The next line sets the string variable. Notice how the end of the string references the productId field of the subform field that has been clicked. The final line uses the DoCmd OpenForm Statement to open the Products Form. The varWhereClause string variable is used as the statement's WhereCondition, thereby opening the form at that particular product record.
Here is the full procedure for putting all this in place:
- Create the main Customer Order Form (with the Order Details Subform).
- Create the Products Form.
- Next you need to go back and edit the Order Details Subform.
- Click on the ProductId field, and then open the PROPERTIES SHEET.
- Under the FORMAT tab, change the IS_HYPERLINK property to YES. Then change the DISPLAY_AS_HYPERLINK property to SCREEN_ONLY. This changes the appearance of the ProductId field to a hyperlink style.
- Under the EVENT tab of the PROPERTIES SHEET, select the ON_CLICK cell in the grid. Then click the three dot symbol on it's right to open the CHOOSE BUILDER dialogue box.
- Select CODE BUILDER from the list, and click OK to open the VBA Editor.
- Past the code listed below in between the lines, "Private Sub ... " and "Exit Sub", in the VBA Editor.
On Error GoTo myError Dim varWhereClause As String varWhereClause = "ID = " & Me!productId DoCmd.OpenForm "frmProducts", , , varWhereClause leave: Exit Sub myError: MsgBox Error$ Resume Next
Nice work........
ReplyDeletehelps a lot
thank u
Thanks KSAK - glad you found it helpful.
ReplyDeleteAwesome! Exactly what i was looking for.
ReplyDeleteThank you so much for this
Thanks Gryd - glad that it helped.
DeleteI can't get this to work. When I click on the record field a box pops asking "Enter Parameter Variable". Clicking OK opens a blank form. Entering a value also gives a blank form. The record I want to open is the same as on the datasheet just with more field displayed. The data in the field is a custom order number in the following format AAS12-001 where AAS is the system, 12 is the year and 001 is the record number. Any ideas why this won't work for me.
ReplyDeleteHi Ridgetech
DeleteIt sounds like your custom order number is of the text data type. As such you will need to modify the varWhereClause variable in the code snippet to:
varWhereClause = "ID = '" & Me!productId & "'"
This contains the necessary quotation marks that enclose the search value. This is important if it is a text data type as opposed to a number data type. My example code was intended for the latter type.
Thanks Justin. It works perfectly now and passing the text value was the issue. Besides being an excellent learning experience, my frustration level just went down 100% because I could not figure out what I was doing wrong. Thanks again.
ReplyDeleteAnd if we want to add new record in the subform with the same way?
ReplyDeleteHi
DeleteThe simple answer is that you wouldn't use the subform to add a new record. You would click the link to open that same record in its own form. The reason we have gone to the trouble of creating the subform link is to enhance the user friendliness of the database application. Subforms are great for displaying summary information, but not quite as good for entering data (especially when there are many fields to fill in).
All of this is working properly, however, when the form opens (what would be your productid form) it shows that the linked record is the only one available, I want to still be able to navigate through all of the records in the table.
ReplyDeleteIt appears that it is because there is a filter in place. I noticed that the open form command has a filtername argument that is left blank, how do I remove the filter.
DeleteYou just need to click the FILTERED button at the bottom of the form (alongside the navigation buttons). This removes the filter manually. The user may then navigate through all the other records. Alternatively you could set up a command button which runs the following line of code:
DeleteMe.FilterOn = False
This removes the filter. However, the filter does need to be on initially, otherwise it will not display the original record that the user wanted to view.
This worked great at first, but then my form stopped opening. I've been over it a lot and I can't imagine what I changed.
ReplyDeleteHere is my code:
Private Sub btnYouthView_Click()
Dim varWhereClause As String
varWhereClause = "ID = " & Me!ID
DoCmd.OpenForm "frmYouthView", , , varWhereClause
End Sub
nevermind....Access security disabled the buttons...fixed now
ReplyDeleteHow would you apply this if you wanted to open another form with a subform in it. I want the subform of the 2nd form to only show details of the record I clicked on in the first subform.
ReplyDeleteand both fields are text data fields. They are the same field just on 2 different subforms.
Delete