I now see several things to correct.
Code:
With doc
.FormFields("wrdSurveyID").Result = Nz(Me!strSurveyID)
End With
"Me" is a shortcut to "Forms!FormName" and is used on a form. You cannot refer to a query in code using "ME". You have to use the recordset reference, in this case "rst"
You are also missing part of the NZ() function. From Help:
The
Nz function is useful for expressions that may include
Null values. To force an expression to evaluate to a non-
Null value even when it contains a
Null value, use the
Nz function to return a zero, a zero-length string, or a custom return value.
You are missing the "ValueIfNull" part.
Code:
.FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,ValueIfNull)
"ValueIfNull" should be replaces with what you want to be returned if "strSurveyID" is NULL.
For Instance, if "strSurveyID" is a Long Int, you might want to return a Zero. It would look like this
Code:
.FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,0)
If "strSurveyID" is a string, you might want to return custom text. It would look like this:
Code:
.FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,"Not found. Please re-enter")
You might try
Code:
With doc
.FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,0)
End With
BUT, the main problem is you opened the recordset (rst) , and didn't check to see if there were records returned and handle the error before it gets to the "with DOC" statement
Code:
strSQL = "SELECT tblSurveySites.strSurveyID, tblSurveySites.dtmSurveyDate, tblSurveySites.dtmSurveyTime, tblSurveySites.strSurveyorsFullName, tblSurveySites.strXCoordinate, tblSurveySites.strYCoordinate, tblSurveySiteLocation.strCountyName, tblSurveySiteLocation.strFeatureName, tblSurveySiteLocation.strRoadName, tblSurveySiteLocation.strRiverBasin " & vbCrLf & _
"FROM tblSurveySites INNER JOIN tblSurveySiteLocation ON tblSurveySites.strSurveyID = tblSurveySiteLocation.strSurveyID " & vbCrLf & _
"WHERE (((tblSurveySites.strSurveyID)= [txtSurveyID]));"
Set rst = CurrentDb.OpenRecordset(strSQL)
'not check for records
If rst.bof and rst.EOF then
'do something
' maybe exit the sub???
end if
Or you could check to see if rst!strSurveyID is NULL and handle the error someway.
You could do this:
Code:
With doc
If IsNull(rst!strSurveyID) then
'strSurveyID is NULL
' do something
ELSE
.FormFields("wrdSurveyID").Result = rst!strSurveyID
End If
End With
(don't have to do both)
Sorry this is so long....