I have the following lines of code in 2 different VBA functions in a Module they both have the same aim of opening a form to a specific record;
stLinkCriteria = "[ID]=" & Reports![Rpt_Manufacture].[ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
and
stLinkCriteria = "[ID]=" & Forms![frmManufactureList]![frm_Products].[ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
How can I change this so I only have one function that I can call from reports or forms and it will open the form to a specific record. I have tried the me! version on reports but I get an 'Invalid use of Me keyword' which I guess is because I can not use it from a Module.
UPDATE #1 Based on the answer below by Thomas G I used this code;
Option Compare Database
Public Function CmdOpenProductDetails(ByRef theObject As Object)
On Error GoTo Err_CmdOpenProductDetails
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "FrmProductDetails"
stLinkCriteria = "[ProductID]=" & theObject![ProductID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_CmdOpenProductDetails:
Exit Function
Err_CmdOpenProductDetails:
MsgBox Err.Description
Resume Exit_CmdOpenProductDetails
End Function
And from the Form and Report for the product names I have an onClick event that reads;
=CmdOpenProductDetails()
However, if I click a product name on a form I get the error message;
The express On Click you entered as the event property setting produced the following error. The expression you entered has a function contain the wrong number of arguments. (The expression may not result in the name of a macro, UDF or Event Proc) (There may have been an error evaluating the function).
If I click from a report I get the error message;
MS Access cannot find the object 'CmdOpenProductDetails(). Make sure you have saved it and that you have typed it correctly.
Meas an argument... - Erik AOpen_Form Me- Erik A