hi folks merry christmas to all of you
it is month of sharing and loving

but folks i have A big problem On function i dont know how to call a functions on vb can you help me or give me some example program
hoping for your kindness

god bless you all

Recommended Answers

All 5 Replies

It is real simple to call a function and once you see how you will slap your own head and make the homer simpson sound of DOH!

Consider the following function...

Public Function MyStringFunction() As String
MyString = "This Is A Test!"
End Function

Now, this function placed in a standard code module is visible throughout the program because it is "Public" and to simpily call it you could go something like so...

Dim MyString As String
MyString = MyStringFunction
MsgBox MyString

or simply...

MsgBox MyStringFunction

However, if you place that code in a form, say Form1, and you want to call it from Form2, then you will need to qualify it by using the "dot" syntax. Meaning, in Form2 you would call it like so...

MsgBox Form1.MyStringFunction

And the same goes for placing it in a class, however, you must instantiate the class. Meaning, you must declare it...

Dim C As New Class1
MsgBox C.MyStringFunction

Now, if you place this function in a standard module and make it "Private", then only procedures within that same module will be able to call the function. The same goes for forms and classes.

Good Luck

hi folks
my problem was not solved i dont know how to call a function to other form they say you declare it as public but i dont how to do it can you give me some examples or program
hoping for your kindness folks.........
Merry Christmas to all of you and happy New Year
God bless you all

Here is a example .I use this function in project to track user activity.
Be sure to write your function code in a module and declare it as public.Hope it will help you..

Public Sub AddLog(Log As String)
Dim Fso As New Scripting.FileSystemObject
Dim Ts As TextStream
Dim Fil As File

If Fso.FileExists(App.Path & "\Acc.log") = False Then
   Set Ts = Fso.CreateTextFile(App.Path & "\Acc.log", True)
   Ts.WriteLine ("Log File of " & App.Title)
   Ts.WriteLine ("---------------------------------------------------")
   Ts.Close
End If
   Set Ts = Fso.OpenTextFile(App.Path & "\Acc.log", ForAppending)
   Ts.WriteLine (Log & " # on " & WeekdayName(Weekday(Date)) & "," & Format(Date, "dd/mmm/yyyy") & " at " & Time)
   Ts.Close
End Sub

To call this function
AddLog "Checked the Daysummary"

The following link might be helpful for you.

Create a Form1.

Private Sub Form_Load()
Form2.showme
End Sub

Create Form2
Put this code

Public Function showme()
MsgBox "hello world"
End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.