Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass variable to function 1

Status
Not open for further replies.

rmh3619

Technical User
May 29, 2006
29
0
0
NL
i am trying to pass a variable to a function in VBA but i cant get it to work.

this is an example of what i'm trying to do:

private sub myButton_Click()
myString="this is a test"
call myMessage
end sub


function myMessage()
msgbox myString
end function


what am i doing wrong?

 
How are ya rmh3619 . . .
Code:
[blue]Private Sub myButton_Click()
     myString = "this is a test"
     Call myMessage([purple][b]myString[/b][/purple])
End Sub

function myMessage([purple][b]Msg[/b][/purple] as String)
      MsgBox [purple][b]Msg[/b][/purple]
End function[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thanks TheAceMan1 but how do i do this with multiple variables?

Private Sub myButton_Click()
myString1 = "this is a test"
myString2 = "this is the second test"
myString3 = "this is the last test"

Call myMessage(myString)
End Sub

how will the function look in this case?
 
i guess i should clarify my question:

i want 3 message boxes following each other with message 1 to 3

something like this:

Private Sub myButton_Click()
myString1 = "this is a test"
myString2 = "this is the second test"
myString3 = "this is the last test"

Call myMessage(myString1, myString2, myString3)
End Sub

function myMessage(Msg as String)
MsgBox Msg
msgbox msg2
msgbox msg3
End function

now i wonder how the function should look like?
 
Function myMessage(Msg as String, msg2 as String, msg3 as String)
MsgBox Msg
msgbox msg2
msgbox msg3
End function

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
thanks aceman1, I really appreciate your help!
 
i tried it but i'm getting an error message:

ByRef argument type mismatch

do you know how to solve it?
 
I resolved the error my self by declaring the type of variable:

Private Sub myButton_Click()

Dim myString1 as String
Dim myString2 as String
Dim myString3 as String

myString1 = "this is a test"
myString2 = "this is the second test"
myString3 = "this is the last test"

Call myMessage(myString1, myString2, myString3)
End Sub

Function myMessage(Msg as String, msg2 as String, msg3 as String)
MsgBox Msg
msgbox msg2
msgbox msg3
End function
 
rmh3619 . . .

The help system provided by microsoft is'nt the greatest as far as finding what your after is concerned. Get to know the [blue]F1[/blue] key!
[ol][li]Put the cursor on any [blue]event[/blue] or [blue]property[/blue] line and hit F1[/li]
[li]In any code window, put the cursor within a keyword and hit F1, or type a word and do the same.[/li][/ol]
Its called [blue]Context Sensitive Help[/blue].

In your case, type byref ina code window, put the cursor on it and hit F1!

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top