AC2007: Need two different values (string & number) from one function?

EdNerd

Registered User.
Local time
Today, 12:40
Joined
Dec 13, 2012
Messages
15
Not quite sure how to go about this. In Access 2007, I need to pass a date into a function. The function will calculate and return the fraction of the year between that date and today. Then I need to use that fraction to determine a string value. The number and the string will be used in two different places on my form.

-- Can I return both values from one function?
-- Should I use two functions, and call the second within the first?
-- I could make it a Sub instead, and push the values into my form.

What looks best here?
Ed
 
1) You could create a custom class
2) Have the code be a subroutine / function of that class
3) Define some class attributes
4) Have the subroutine / function code set the attributes
5) Once the subroutine / function ends / returns cleanly, then reference the class object to obtain the two (or more) values from the class object via accessing the object attributes the subroutine / function left the values in while executing

Yes, alternately you could have a class which an instance of is created on the form. Thus the class when handed a reference to the form could use that reference to push values to the form fields. I have used this approach to develop Validation classes. The call to the class from a form event (button perhaps) passes in itself (Me) and I receive that refereince into "ByRef MePointer Access.Form". Then the class may manipulate the Form with that MePointer object.
 
Functions on their own can only return a single value. Mdl's method sounds quite neat, but depends on "need to use that fraction to determine a string value"
You haven't stated how this works, but you could use a case statement like this:

Select Case getYearFraction(myDate)
Case some decimal range : myString = "result1"
Case some other decimal range : myString = "result2"
Case Else : myString = "result3"
End Select
Once you have your string value, you can push it to a form etc
Depends on how many fractions you're wanting to cope with

David
 
Michael: the class is a bit above me. I'd love to learn that some day - but I'm not there yet.

David: I agree with you. I went with a simple function to calculate the date and an If to get the string, and I'll push the string string into the form.

Thanks for the help!
Ed
 
Functions can return a Type that contains two values:

Code:
Public Type IntStrPair
    strValue As String
    intValue As Integer
End Type
 
Private Function TestFn(ByVal pValue As Integer) As IntStrPair
    TestFn.intValue = pValue + 1
    TestFn.strValue = "You passed " & pValue & " which is one less than "
End Function
 
Public Sub TestIntStrPair()
    Dim result As IntStrPair
    result = TestFn(2)
    Debug.Print result.strValue & result.intValue
End Sub
 

Users who are viewing this thread

Back
Top Bottom