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
Solved

vba / call function & assign value into variable string

Posted on 2004-08-21
6
398 Views
Last Modified: 2012-06-21
hi,
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

thanks
Sanjana

 
0
Comment
Question by:itc307
6 Comments
 
LVL 85

Accepted Solution

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

Option Explicit

Private Sub Command1_Click()
    mySub
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
0
 
LVL 4

Expert Comment

by:hamood
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


hamood
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11859540
Should the line
stName = myFunction(something)
be
stName = myFunction("something")
instead?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:kouroshparsa
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()
mySub
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
0
 

Author Comment

by:itc307
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
0
 
LVL 85

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.

Idle_Mind
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

840 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