vba / call function & assign value into variable string

Posted on 2004-08-21
Last Modified: 2012-06-21
i am trying to call a function and use the returned value as the value of a string (using vba). Apparently it is not working. For example,

Function myFunction(name As string) As String
end Function

Sub mySub()
dim stName As string
stName = myFunction(something) ///error
' or
stName = Call myFunction(something) ///error
end Sub

Please let me know how to solve this problem


Question by:itc307
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Accepted Solution

Mike Tomlinson earned 495 total points
ID: 11859252
Here is an example similar to yours:

Option Explicit

Private Sub Command1_Click()
End Sub

Public Function myFunction(ByVal name As String) As String
    ' do something with name and return it
    myFunction = Left(name, 1) & Right(name, 1)
End Function

Public Sub mySub()
    Dim stName As String
    Dim stParam As String
    stParam = "something"
    stName = myFunction(stParam)
    MsgBox "Input: " & stParam & vbCrLf & "Output: " & stName
End Sub

Expert Comment

ID: 11859384
where you have written this code. Just create a simple application (standared exe application) in vb6. now call your mysub function in form onload event like this

Private Sub Form_Load()
    Call mySub ' here is your function call
End Sub

Function myFunction(name As String) As String
    myFunction = name
End Function

Sub mySub()
    Dim stName As String
    stName = myFunction("something")
    MsgBox stName
    ' or
    stName = myFunction("something")
    msgbox stName

End Sub

LVL 18

Expert Comment

ID: 11859540
Should the line
stName = myFunction(something)
stName = myFunction("something")
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Expert Comment

ID: 11862570
I don't know what you need to know !
I hope that yuo know a function returns a value and a sub does something. Either one can  have parameters (arguments)
Here is a sample:
Private Sub Command1_Click()
End Sub

Function myFunction(name As String) As String
myFunction = "Hello " & name
End Function

Sub mySub()
Dim stName As String
strName = "John"
stName = myFunction(strName)
MsgBox (stName)
End Sub

Author Comment

ID: 11862610
thanks Idle Mind,
apparently I had problems returning the value of the function - i have solved it now. Regardless can u please tell me what "vbclrf" means. U have used this term in your code.

thanks sanjana
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11862615
vbCrLf stands for Visual Basic Carriage Return & Line Feed.  When used in a string it causes the cursor to move down to the next line allowing you to make multiline messages.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 23 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question