I try to make a simple .vb like this
create three textboxes named txtA, txtB and txtAplusB
then if you click a button named cmdPlus then the text in txtAplusB will include the texts in txtA and txtB

Here is the code for form
Private Sub cmdPlus_Click()
Call Calculate
End Sub

About Caculate() function, I create a .bas file and put code in it
Public Function Calculate() As String
txtAplusB.Text = txtA.Text + txtB.Text
End Function

It said error, txtA (txtB) is not defined ???
How can I make it run ?
Thanks :)

Recommended Answers

All 6 Replies

Hi Smiles,

First of all, you have created a function with no return value and you are not using the return anyway. VB allows you to do that but you shouldn't anyway. Change the funtion header to

Public Sub Calculate()

Second, if you are in a .bas file (module) then you can only refer to global objects. The form is a global object but the text boxes are local to that form. Hence you can refer to the text boxes as follows

FormName.txtB.Text

Thirdly, the text concatenation operator is not +, although again VB will let you do this. For the correct result in all circumstances use & instead.

Fourth, the Call keyword is optional, you really don't need it

Having said all that you could write it as follows which I would consider more appropriate

Private Sub cmdPlus_Click()
    Me. txtAplusB.Text = Calculate(txtA.Text, txtB.Text)
End Sub

Public Function Calculate(byval pstrFirst as String, byval pstrSecond as String) As String
    Calculate = pstrFirst & pstrSecond 
End Function

Hi ! Thanks for your reply, two ways run very well.
Now I do adding two number

Public Function Calculate() As String
Dim C As String
C = Val(Form1.txtA.Text) + Val(Form1.txtB.Text)
Form1.txtAplusB.Text = C
End Function

I wonder how to check if they filled text (not number) into textbox ?
Thanks !!! :)

The easiest way is to use the IsNumeric function

If IsNumeric(Form1.txtA.Text) Then

or see if the output from the Val function matches the text

If CStr(Val(Form1.txtA.Text)) = Form1.txtA.Text Then

Hi ! Thanks so much !!!
Could you tell me shortly when should we use module (.bas) and class module (.cls) ... and .ocx what is it used for ???
Thanks !!!

Class modules are used to define objects. They can be very usefule to encapsulate a lot of related information. I suggest you look into object oriented programming to learn more about them. Forms can be used in the same way as class modules, but again you should try to get a handle on OO before looking into that.

Normal modules allow you to create code outside of any objects (forms or classes) for global use in your application. Their use should be minimised, however never miss an opportunity to share the same code amongst multiple parts of your app.

Ocx files are controls, just like a text box or list box. You can create your own and use them in your apps, just watch for memory leaks (I've seen a major company app go down because someone forgot to check the custom ocx for leaks).

Looks like you've gotten yourself off to a good start anyway, so keep plugging away :)

Maybe :-/
I had saved many time for Web Languages, I hope that via what you said to me, I will find a equivalence between them, hope for a successful quick start !!!
Thanks anyway :)

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.