Function to return two values
You seem to have already solved your problem. so, you maybe don't need
this one. but just for a reference. I would use a user defined variable
instead of using global variable. Using this way, your sample code looks
like this.
Type fboo
msg As String
state As Boolean
End Type
Function fbooDataIsValid(ByVal pstrParam1 As String, _
ByVal pintParam2 As Integer) As fboo
Dim tmp As fboo
If pstrParam1 = "Hi Jim" Then
tmp.msg = "Error, my friend. You should have said Hello!."
tmp.state = False
fbooDataIsValid = tmp
Exit Function
End If
If pintParam2 < 100 Then
tmp.msg = "Error, your number is < 100."
tmp.state = False
fbooDataIsValid = tmp
Exit Function
End If
tmp.msg = "Suceed"
tmp.state = True
fbooDataIsValid = tmp
End Function
' ---
Sub TestDeJac()
Dim strText As String
Dim result As fboo
strText = "Hi Jim"
result = fbooDataIsValid(strText, 136)
If Not result.state Then
MsgBox result.msg, vbCritical, "Sofica"
Else
MsgBox "It's OK...", vbInformation, "Sofica"
End If
End Sub
keiji
Jac Tremblay wrote:
Hi Jim,
When I first read your answer, I thought that wouldn't work. But I tried it
out and found it is interesting. I changed it a bit so I could get the two
values in two separated variables. Here is what I came up with:
Function IcelandBanking(ByRef strMsg As String) As Boolean
Dim WontPay As Long
Dim NoReserves As Boolean
'Code here to calculate WontPay and NoReserves
WontPay = 1
NoReserves = False
If WontPay 0 Then
strMsg = "Small Withdrawals Only"
Else
strMsg = "You are out of luck"
End If
IcelandBanking = NoReserves
End Function
'--
Sub FinancialStatus()
Dim strText As String
Dim strTrueOrFalse As String
Dim strMsg As String
Dim strResult As String
Dim intPosVbCr As Integer
' MsgBox IcelandBanking(strText) & vbCr & strText
strResult = IcelandBanking(strText) & vbCr & strText
intPosVbCr = InStr(1, strResult, vbCr)
strTrueOrFalse = Mid(strResult, 1, intPosVbCr - 1)
strMsg = Mid(strResult, intPosVbCr + 1, Len(strResult) - intPosVbCr)
MsgBox "strTrueOrFalse = " & strTrueOrFalse & vbCrLf & _
"strMsg = " & strMsg
End Sub
It works OK, but I am not sure I will use it.
My problem is that I have to check 4 different information from a
transaction. I want to group the 4 validations in a separate procedure or
function. If an error occurs, I want to display a pertinent message and exit
the main proc. Here is another way I figured out to solve my problem:
Option Explicit
Public strMessage As String
' ---
Function fbooDataIsValid(ByVal pstrParam1 As String, _
ByVal pintParam2 As Integer) As Boolean
If pstrParam1 = "Hi Jim" Then
strMessage = "Error, my friend. You should have said Hello!."
fbooDataIsValid = False
Exit Function
End If
If pintParam2 < 100 Then
strMessage = "Error, your number is < 100."
fbooDataIsValid = False
Exit Function
End If
fbooDataIsValid = True
End Function
' ---
Sub TestDeJac()
Dim strText As String
strText = "Hi Jim"
If Not fbooDataIsValid(strText, 36) Then
MsgBox strMessage, vbCritical, "Sofica"
Exit Sub
End If
MsgBox "It's OK...", vbInformation, "Sofica"
End Sub
Thank you for your original comment and good night.
|