Calling function in query with no result (1 Viewer)

terrytek

Registered User.
Local time
Today, 01:49
Joined
Aug 12, 2016
Messages
75
I have a query where I am trying to convert a test date into the academic year in which it occurs.

Code:
SELECT qryTestingMostRecentTest1.StudentID, qryTestingMostRecentTest1.LastTestDate, qryTestingMostRecentTest1.PrevTestDate, TestToAcademicYr([LastTestDate]) AS AcademicYr
FROM qryTestingMostRecentTest1
GROUP BY qryTestingMostRecentTest1.StudentID, qryTestingMostRecentTest1.LastTestDate, qryTestingMostRecentTest1.PrevTestDate;

Here is the code module:
Code:
Option Compare Database

Public Function TestToAcademicYr(LastTestDate As Date) As String
Dim AcademicYr As String

If LastTestDate >= #7/1/2015# And LastTestDate <= #6/30/2016# Then
   AcademicYr = "2015-16"
ElseIf LastTestDate >= #7/1/2016# And LastTestDate <= #6/30/2017# Then
   AcademicYr = "2016-17"
ElseIf LastTestDate >= #7/1/2017# And LastTestDate <= #6/30/2018# Then
   AcademicYr = "2017-18"
ElseIf LastTestDate >= #7/1/2018# And LastTestDate <= #6/30/2019# Then
   AcademicYr = "2018-19"
ElseIf LastTestDate >= #7/1/2019# And LastTestDate <= #6/30/2020# Then
   AcademicYr = "2019-20"

End If
End Function

When I run the query, the field AcademicYr is blank. Why is this not working?

Thank you.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:49
Joined
Oct 29, 2018
Messages
21,485
Hi. You forgot to assign a return value. Add the following code at the end of your function.
Code:
TestToAcademicYr = AcademicYr
 

essaytee

Need a good one-liner.
Local time
Today, 15:49
Joined
Oct 20, 2008
Messages
512
Add this to the last line of your function:

TestToAcademicYr = AcademicYr
 

plog

Banishment Pending
Local time
Today, 00:49
Joined
May 11, 2011
Messages
11,653
I'd also advise making that function work indefinitely. Instead of a bunch of if/else statements that work for a narrow range of dates, test the passed date and determine what school year it falls in no matter what date is passed.
 

Micron

AWF VIP
Local time
Today, 01:49
Joined
Oct 20, 2018
Messages
3,478
you don't even need the variable

If LastTestDate >= #7/1/2015# And LastTestDate <= #6/30/2016# Then
TestToAcademicYr = "2015-16"


You have Option Compare Database
If you don't have Option Explicit set automatically, you'll probably have issues that are hard to fix. Check it out.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:49
Joined
May 21, 2018
Messages
8,547
Code:
Public Function AcademicYear(LastTestDate As Date) As String
  Dim Yr As Integer
  If LastTestDate >= DateSerial(Year(LastTestDate), 7, 1) Then
    Yr = Year(LastTestDate)
  Else
    Yr = Year(LastTestDate) - 1
  End If
  AcademicYear = Yr & "-" & Right(CStr(Yr + 1), 2)
End Function
 

terrytek

Registered User.
Local time
Today, 01:49
Joined
Aug 12, 2016
Messages
75
Hi. You forgot to assign a return value. Add the following code at the end of your function.
Code:
TestToAcademicYr = AcademicYr

I did that, and it worked. The odd thing is, though, I've written other simple functions and never needed that before. I don't really understand why those worked and this didn't.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:49
Joined
Oct 29, 2018
Messages
21,485
I did that, and it worked. The odd thing is, though, I've written other simple functions and never needed that before. I don't really understand why those worked and this didn't.
Can’t tell you without seeing an example but glad to hear you got this one fixed. Good luck with your project.
 

essaytee

Need a good one-liner.
Local time
Today, 15:49
Joined
Oct 20, 2008
Messages
512
I did that, and it worked. The odd thing is, though, I've written other simple functions and never needed that before. I don't really understand why those worked and this didn't.

I'm guessing, whereas in this function you purposely created a separate variable that wasn't assigned to the function name, maybe in your other functions where you think you didn't do this is because you were using the function name directly (nothing wrong with that) and so returned values.
 

terrytek

Registered User.
Local time
Today, 01:49
Joined
Aug 12, 2016
Messages
75
I'm guessing, whereas in this function you purposely created a separate variable that wasn't assigned to the function name, maybe in your other functions where you think you didn't do this is because you were using the function name directly (nothing wrong with that) and so returned values.

Yes, it turns out that I called the variable I was trying to find the same thing as the function in the other modules I had, while in the thread in this particular case, the variable AcademicYr did not match the function name TestToAcademicYear.

That's a good thing to know!!
 

terrytek

Registered User.
Local time
Today, 01:49
Joined
Aug 12, 2016
Messages
75
Code:
Public Function AcademicYear(LastTestDate As Date) As String
  Dim Yr As Integer
  If LastTestDate >= DateSerial(Year(LastTestDate), 7, 1) Then
    Yr = Year(LastTestDate)
  Else
    Yr = Year(LastTestDate) - 1
  End If
  AcademicYear = Yr & "-" & Right(CStr(Yr + 1), 2)
End Function

Yes, that's obviously a much better way to do it. My way is what happens when your VBA skills are so rudimentary!!
 

Users who are viewing this thread

Top Bottom