R
Redsoxnation
I get a runtime error in my query using the code below...
Option Explicit
Function PrevRecVal_NPDES_1812_Eff_A_qry(KeyName As String, KeyValue,
FieldNameToGet As String)
'*************************************************************
' FUNCTION: PrevRecVal_NPDES_1812_Eff_A_qry()
' PURPOSE: Retrieve a value from a field in the previous form
' record.
' PARAMETERS:
' F - The form from which to get the previous value.
' KeyName - The name of the form's unique key field.
' KeyValue - The current record's key value.
' FieldNameToGet - The name of the field in the previous
' record from which to retrieve the value.
' RETURNS: The value in the field FieldNameToGet from the
' previous form record.
' EXAMPLE:
' =PrevRecVal(Form,"ID",[ID],"OdometerReading")
'**************************************************************
Dim RS As DAO.Recordset
Dim db As DAO.Database
On Error GoTo Err_PrevRecVal_NPDES_1812_Eff_A_qry
' The default value is zero.
PrevRecVal_NPDES_1812_Eff_A_qry = 0
Set db = CurrentDb()
' Get the form recordset.
Set RS = db.OpenRecordset("Sys_Comp_NPDES_1812_Eff_A_qry", dbOpenDynaset)
' Find the current record.
Select Case RS.Fields(KeyName).Type
' Find using numeric data type key value?
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & KeyName & "] = " & KeyValue
' Find using date data type key value?
Case DB_DATE
RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
' Find using text data type key value?
Case DB_TEXT
RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
Case Else
MsgBox "ERROR: Invalid key field data type!"
Exit Function
End Select
' Move to the previous record.
RS.MovePrevious
' Return the result.
PrevRecVal_NPDES_1812_Eff_A_qry = RS(FieldNameToGet)
RS.Close
Bye_PrevRecVal_NPDES_1812_Eff_A_qry:
Exit Function
Err_PrevRecVal_NPDES_1812_Eff_A_qry:
Resume Bye_PrevRecVal_NPDES_1812_Eff_A_qry
End Function
When I debug it, it stops on this line...
PrevRecVal_NPDES_1812_Eff_A_qry = RS(FieldNameToGet)
Option Explicit
Function PrevRecVal_NPDES_1812_Eff_A_qry(KeyName As String, KeyValue,
FieldNameToGet As String)
'*************************************************************
' FUNCTION: PrevRecVal_NPDES_1812_Eff_A_qry()
' PURPOSE: Retrieve a value from a field in the previous form
' record.
' PARAMETERS:
' F - The form from which to get the previous value.
' KeyName - The name of the form's unique key field.
' KeyValue - The current record's key value.
' FieldNameToGet - The name of the field in the previous
' record from which to retrieve the value.
' RETURNS: The value in the field FieldNameToGet from the
' previous form record.
' EXAMPLE:
' =PrevRecVal(Form,"ID",[ID],"OdometerReading")
'**************************************************************
Dim RS As DAO.Recordset
Dim db As DAO.Database
On Error GoTo Err_PrevRecVal_NPDES_1812_Eff_A_qry
' The default value is zero.
PrevRecVal_NPDES_1812_Eff_A_qry = 0
Set db = CurrentDb()
' Get the form recordset.
Set RS = db.OpenRecordset("Sys_Comp_NPDES_1812_Eff_A_qry", dbOpenDynaset)
' Find the current record.
Select Case RS.Fields(KeyName).Type
' Find using numeric data type key value?
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & KeyName & "] = " & KeyValue
' Find using date data type key value?
Case DB_DATE
RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
' Find using text data type key value?
Case DB_TEXT
RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
Case Else
MsgBox "ERROR: Invalid key field data type!"
Exit Function
End Select
' Move to the previous record.
RS.MovePrevious
' Return the result.
PrevRecVal_NPDES_1812_Eff_A_qry = RS(FieldNameToGet)
RS.Close
Bye_PrevRecVal_NPDES_1812_Eff_A_qry:
Exit Function
Err_PrevRecVal_NPDES_1812_Eff_A_qry:
Resume Bye_PrevRecVal_NPDES_1812_Eff_A_qry
End Function
When I debug it, it stops on this line...
PrevRecVal_NPDES_1812_Eff_A_qry = RS(FieldNameToGet)