Hi all,
I've built a small(ish) function that I referred to in another thread, here. It currently uses Dlookup, which is simple but far too slow. I'm trying to convert it to use Recordsets and being completely new to their use, I've come up with the below.
Basically the only modifications were establishing new variables, opening the recordset as with SQL (including limiting it with use of the variables found in the function) and then trying to grab the values from the fields in the recordset to be used in formulae. However, when I run a query that uses the function I get:
With
highlighted. If I change this to
I then get the error
with get_invoiced highlighted.
I know this is probably pretty simple but I am new to using recordsets, so be gentle
If there are any other glaring problems with my code, please comment as I'm trying to improve the speed of the main query this runs in.
EDIT: I should mention that with that SQL query, only one record should ever be found with that combination of 'pid' and 'weekid'
EDIT EDIT: Just after I posted this I realised I had made some dumb mistakes - must return object or variant error was due to the function being renamed get_invoiced_test and me not reflecting this in the code. Secondly, I had misspelled rstTest as rstest in the code. After the above changes however, I still get the type-declaration character error.
I've built a small(ish) function that I referred to in another thread, here. It currently uses Dlookup, which is simple but far too slow. I'm trying to convert it to use Recordsets and being completely new to their use, I've come up with the below.
Code:
Function get_invoiced_test(hosted, share_type, rev_share, host_share, pid, week, period, delay) As Currency
''*get invoicing based on weeks
Dim thisweek As Integer
Dim thisperiod As Integer
Dim DelayedAmount As Currency
Dim eco_gross As String
Dim eco_net As String
Dim DelayedWeek As Integer
'Dim year As Integer
Dim dbs As Database
Dim rstTest As Recordset
Dim strQuery As String
thisweek = Format(Date, "ww", 0, 0)
DelayedWeek = week - delay
Set dbs = CurrentDb
strQuery = "SELECT [Ecosystem Gross LE], [Ecosystem Net LE], [Ecosystem Gross Actual], [Ecosystem Net Actual] FROM tblRecords WHERE PartnersetID = " & pid & " AND WeekID = " & DelayedWeek & ""
Debug.Print strQuery
Set rstTest = dbs.OpenRecordset(strQuery)
Select Case thisweek
Case 27 To 31
thisperiod = 7
Case 32 To 35
thisperiod = 8
Case 36 To 39
thisperiod = 9
Case 40 To 44
thisperiod = 10
Case 45 To 48
thisperiod = 11
Case 49 To 52
thisperiod = 12
Case Else
thisperiod = 0
End Select
If period >= thisperiod Then
eco_gross = "[Ecosystem Gross LE]"
eco_net = "[Ecosystem Net LE]"
Else
eco_gross = "[Ecosystem Gross Actual]"
eco_net = "[Ecosystem Net Actual]"
End If
If hosted = -1 Then
If share_type = "Gross" Then
''' Gross, Hosted
DelayedAmount = rstTest! & eco_gross
get_invoiced = Nz(DelayedAmount, 0) - (Nz(DelayedAmount, 0) * Nz(rev_share, 0) * Nz(host_share, 0))
Else
'''Nett, Hosted
DelayedAmount = rstTest! & eco_net
get_invoiced = Nz(DelayedAmount, 0) - (Nz(DelayedAmount, 0) * Nz(rev_share, 0) * Nz(host_share, 0))
End If
Else
If share_type = "Gross" Then
'''Gross, Not Hosted
DelayedAmount = rstest! & eco_gross
get_invoiced = Nz(DelayedAmount, 0) * Nz(rev_share, 0)
Else
'''Nett, Not Hosted
DelayedAmount = rstest! & eco_net
get_invoiced = Nz(DelayedAmount, 0) * Nz(rev_share, 0)
End If
End If
End Function
Basically the only modifications were establishing new variables, opening the recordset as with SQL (including limiting it with use of the variables found in the function) and then trying to grab the values from the fields in the recordset to be used in formulae. However, when I run a query that uses the function I get:
Code:
Complie error: Type-declaration character does not match declared data type
Code:
rstTest!&eco_gross
Code:
rstTest!eco_gross
Code:
Compile error:Function call on left hand side of assignment must return Variant or Object
I know this is probably pretty simple but I am new to using recordsets, so be gentle

EDIT: I should mention that with that SQL query, only one record should ever be found with that combination of 'pid' and 'weekid'
EDIT EDIT: Just after I posted this I realised I had made some dumb mistakes - must return object or variant error was due to the function being renamed get_invoiced_test and me not reflecting this in the code. Secondly, I had misspelled rstTest as rstest in the code. After the above changes however, I still get the type-declaration character error.
Last edited: