Function Problem

J

James

Hello,
I hope some one can please help me with returning a string from a function
back to a label on a form?
I have a document that has several OptionButtons on it that I use to help
test my grandson.
After I check the answers I put, in a label, the words "You're Correct" or
Incorrect" in appropriate labels for the questions.
I can get the right string into the function, but I can't get anything back
Can some one show me how to do this?

Here is some code that sends the string to the function
'Begin Code========================================
If radSaveAs2.Value = -1 Then
Dim answer As String
intNumberCorrect = intNumberCorrect + 1 'Add one to the
correct answer total
CorrectMyAnswer ("Correct") 'Call
the function for correct
Else
CorrectMyAnswer ("Incorrect") 'Call the
function for incorrect
End If
lblSaveAsAnswer.Caption = CorrectMyAnswer(answer)
'End Code==========================================


'Function Code=======================================
Public Function CorrectMyAnswer(ByVal answer As String) As String
If answer = "Correct" Then
answer = "You're Correct"
Else
answer = "Sorry, Incorrect"
End If
End Function
'End Code===========================================

I know I could just code the lblSaveAsAnswer.Caption = "Correct" or
"Incorrect" for each one,
but I'm trying to learn how to do this so I can get better at Word VBA as I
go.
Thanks James
 
D

D

'Function Code=======================================
Public Function CorrectMyAnswer(ByVal answer As String) As String
If answer = "Correct" Then CorrectMyAnswer = "You're Correct" '<<<<
Else
CorrectMyAnswer = "Sorry, Incorrect" '<<<<
End If
End Function
'End Code===========================================

You have to assign the value to the Function Name.
 
J

James

I assigned the right strings to the function name as you suggested, but then
how do I get the answer back to the calling sub?
I've tried
lblPageNumberAnswer1.Caption = CorrectMyAnswer.answer
and
lblPageNumberAnswer1.Caption = CorrectMyAnswer
and
lblPageNumberAnswer1.Caption = "CorrectMyAnswer"
the first two give an error saying argument not optional and the third
returns the string "CorrectMyAnswer"
How do I return the function?
James
 
Å

Åsa Holmgren

Try this:

Public Sub MyCode
Dim answer As String
Dim intNumberCorrect As Integer

If radSaveAs2.Value = -1 Then
intNumberCorrect = intNumberCorrect + 1
answer = CorrectMyAnswer("Correct")
Else
answer = CorrectMyAnswer("Incorrect")
End If

lblSaveAsAnswer.Caption = answer
End Sub

Public Function CorrectMyAnswer(ByVal answer As String) As String
If answer = "Correct" Then
CorrectMyAnswer = "You're Correct"
Else
CorrectMyAnswer = "Sorry, Incorrect"
End If
End Function
 
J

James Agostinho

Asa,
Thanks for the help
James
Åsa Holmgren said:
Try this:

Public Sub MyCode
Dim answer As String
Dim intNumberCorrect As Integer

If radSaveAs2.Value = -1 Then
intNumberCorrect = intNumberCorrect + 1
answer = CorrectMyAnswer("Correct")
Else
answer = CorrectMyAnswer("Incorrect")
End If

lblSaveAsAnswer.Caption = answer
End Sub

Public Function CorrectMyAnswer(ByVal answer As String) As String
If answer = "Correct" Then
CorrectMyAnswer = "You're Correct"
Else
CorrectMyAnswer = "Sorry, Incorrect"
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top