The WhereCondition is a SQL Where Clause which means that it should be written using SQL Syntax, which varies a bit on which datatype is being compared.
So if the [ID] Column in the database is Alphanumeric, it will need to be surrounded by Single Quotes if it is Numeric then the Single Quotes will cause an error. This is a common stumbling block when first starting out. It still bothers me. I would be happy to stuff everything into quotes and not worry about it, but it doesn't work that way.
If [ID] is an Alphanumeric Column, the Syntax is:
- DoCmd.OpenReport ReportName:="LastRecord", View:=acViewPreview, WhereCondition:="[ID]='" & [ID] & "'"
if it is just Numeric then the Syntax is:
- DoCmd.OpenReport ReportName:="LastRecord", View:=acViewPreview, WhereCondition:="[ID]=" & [ID]
Also, you might need/want to define your ID a bit differently. What you have, might work, but I think you'll find it a little work with:
- DoCmd.OpenReport ReportName:="LastRecord", View:=acViewPreview, WhereCondition:="[ID]=" & Me!ID
Lastly, ZMDB would like to see things like this as he believes, and I agree, it is easier to troubleshoot:
- Dim sTemp As String
-
sTemp = "[ID]='" & Me!ID & "'"
-
DoCmd.OpenReport ReportName:="LastRecord", View:=acViewPreview, WhereCondition:= sTemp