I want to pass a report object to a function, and get a field I parse out of a string and pull it's value as if it is a value in the record source for the current record... Basically substituting a value for a field value...
I am passing report as .... ByRef rpt As Access.Report
I can't figure out how to find the field value for say field "x" in the recordsource... This seemed intuitive but clearly I am not getting to the recourdsource... rpt.Fields("x").Value
Oh I see now.... I am calling function in control source... I need to pass the current record somehow instead to parse it out? My brain hurts on this one.
The below function is what I am trying to call from my control source... I am just trying to avoid having to manually apply nested replaces in the report controls.
It represents a maintenance issue. Any clever ideas are appreciated. For reasons that are too long to go into, replacing embedded field names is the correct approach.
I am passing report as .... ByRef rpt As Access.Report
I can't figure out how to find the field value for say field "x" in the recordsource... This seemed intuitive but clearly I am not getting to the recourdsource... rpt.Fields("x").Value
Oh I see now.... I am calling function in control source... I need to pass the current record somehow instead to parse it out? My brain hurts on this one.
The below function is what I am trying to call from my control source... I am just trying to avoid having to manually apply nested replaces in the report controls.
It represents a maintenance issue. Any clever ideas are appreciated. For reasons that are too long to go into, replacing embedded field names is the correct approach.
Code:
Function fnReplaceFields(ByRef rpt As Access.Report, ByRef Source As String) As String
'Expected Call in control source: fnReplaceFields([Report], <FieldWhoseValueWIllBeReplaced>)
Dim strValues() As String
Dim i As Long
Dim lngUpperArray As Long
Dim strReturn As String
Dim strField As String
Dim lngPos As Long
strValues = Split(Source, "[")
lngUpperArray = UBound(strValues)
strReturn = Source
For i = 0 To lngUpperArray Step 1
lngPos = InStr(1, strValues(i), "]")
If lngPos > 0 Then
strField = Left(strValues(i), lngPos - 1)
If InStr(1, strReturn, "[" & strField & "]") > 0 Then
strReturn = Replace(strReturn, "[" & strField & "]", Nz(rpt.Fields(strField).Value, "")) '[red]NZ Paremeter Failes[/red]
End If
End If
Next i
fnReplaceFields = strReturn
End Function