PDA

View Full Version : VB help


George Hallam
10-03-2008, 10:17 AM
ok at school i am taking VB lessons i have just started and i am stuck already

the question is (we are using variables)

"display your first name on a form on a single line, when the ok button is clicked it will display for last name. The first name should be stored during the forms load even and the surname during the buttons click even. you will need to use concatenation operator for the out put"

The form itself looks like this

http://i289.photobucket.com/albums/ll221/jiggyghallam/form1.png

this is the code i have so far (i know its a long winded way to do this task but the whole point of the exercise is by clicking the OK command it will add my surname)

Option Explicit
Dim surname As String

Private Sub cmd_Click()
surname = "Hallam"
End Sub

Private Sub Form_Load()
Form1.Show
Form1.Print "My name is George "; surname
End Sub





if i am not clear with what i am saying ask (its hard to word what i need to do)


Thanks,
VB noob

:D

Paul Komski
10-03-2008, 11:36 PM
You don't say which version of VB. The following certainly would apply in VB6.

I would setup Form1 with a label top left and with the caption being
"My name is George " and I would declare the surname variable within the sub that uses it rather than in the module as a whole - but that is up to you. Then:-

Private Sub cmd_Click()
Dim surname as string
surname = "Hallam"
Me.Label1.caption = Me.Label1.caption & surname
End Sub

If you are loading a second form from the main form then this would be changed to:

Private Sub cmd_Click()
Dim surname as string
surname = "Hallam"
Form1.show
Form1.Label1.caption = Form1.Label1.caption & surname
End Sub

And note that if loading a second form from the main form you should also ensure that you unload it when the main form is unloaded or the app is likely to create memory leaks etc.

Public Sub Form_Unload(Cancel As Integer)
Unload Form1
End Sub

PS Unless you have very good reasons not to then always use Option Explicit at the top of any modules. It is also a good practice to set the type of any variable when it is declared even if this is a Variant variable. Both will trip more compile errors when you compile but debugging will always be much easier.

PPS And if you want to use Form.Print instead of a label the load event would be:

Private Sub Form_Load()
Form1.Show
Form1.Print "My name is George "
End Sub

And the click event could be:

Private Sub cmd_Click()
Dim surname as string
surname = "Hallam"
Form1.cls
Form1.Print "My name is George " ; surname
End Sub

or finally another variation using the semicolon after the text to allow for later concatenation:


Private Sub Form_Load()
Form1.Show
Form1.Print "My name is George " ;
End Sub

And the click event could be:

Private Sub cmd_Click()
Dim surname as string
surname = "Hallam"
Form1.Print surname ;
End Sub

So many ways to skin cats - but the different methods all have their uses - though the direct "printing" to a form's background is not that common in my experience.

George Hallam
10-04-2008, 03:44 AM
cheers for that Paul, it really stumped me.

i have been reading through the chapters on my book and i can make a calculator.

but i see many more questions popping up in the foreseeable future :p