I want to use a user defined function in my query. This is the function.
The function works if placed inside a form module with the resultant string written to an unbound text box. If the function is placed in its own module so that the query has access to it as well, it doesn't. So when I try to use the function in the query, it can't find it.
This is my query:
SELECT ALLDISP.[Disposition Number], ALLDISP.[Current Status], ALLDISP.[NTS Map Reference], GetOwners(ALLDISP.[Disposition Number]) AS OWNERS, ALLDISP.Location, ALLDISP.[Staking Date], ALLDISP.[Effective Date], ALLDISP.[Date Protected to], ALLDISP.[Area (Hectares)], ALLDISP.[Grouping Certificate]
FROM ALLDISP
ORDER BY ALLDISP.[Disposition Number];
Any Ideas?
Code:
Public Function GetOwners(Dispos As String) As String
'MAKE THE OWNER string
Dim qdfGetOwners As QueryDef
Dim rstGetOwners As Recordset
Dim strSQL As String
Dim strOWNERS As String
Dim db As DAO.Database
Set db = CurrentDb()
Set qdfGetOwners = db.CreateQueryDef("")
strSQL = "SELECT tblHolders.DispositionNumber, tblHolders.Holder, tblHolders.Percentage " & _
"FROM tblHolders " & _
"WHERE (((tblHolders.DispositionNumber)= """ & Dispos & """ ))" & _
"ORDER BY tblHolders.DispositionNumber ;"
With qdfGetOwners
.SQL = strSQL
Set rstGetOwners = .OpenRecordset() ' error trap needed for no records?
End With
With rstGetOwners
Do While Not .EOF
If .OpenRecordset.Fields(0).Value = Dispos Then
strOWNERS = strOWNERS & .Fields(1).Value & " " & .Fields(2).Value & " "
End If
.MoveNext
Loop
strOWNERS = RTrim(strOWNERS)
End With
rstGetOwners.Close
GetOwners = strOWNERS
End Function
The function works if placed inside a form module with the resultant string written to an unbound text box. If the function is placed in its own module so that the query has access to it as well, it doesn't. So when I try to use the function in the query, it can't find it.
This is my query:
SELECT ALLDISP.[Disposition Number], ALLDISP.[Current Status], ALLDISP.[NTS Map Reference], GetOwners(ALLDISP.[Disposition Number]) AS OWNERS, ALLDISP.Location, ALLDISP.[Staking Date], ALLDISP.[Effective Date], ALLDISP.[Date Protected to], ALLDISP.[Area (Hectares)], ALLDISP.[Grouping Certificate]
FROM ALLDISP
ORDER BY ALLDISP.[Disposition Number];
Any Ideas?