Visual Basic help please

Associate
Joined
11 Nov 2006
Posts
549
Location
Devon
Im creating an application for an assignment and having some troubles and its getting me stressed cause i know its simple.

This is the problem:

On form1 there is a text box where the user enters their name, this text box is called txtNameenter. Then on form3 there is a label this is called lblNamedisplay. Basically i want the name that is entered on form1 to be displayed in the label on form3. This can either be on the press of a command button or it can just be displayed when form3 is displayed.

I have tried the following

Without the command button, code entered into lblNamedisplay:

Private Sub lblNamedisplay_Click()
lblNamedisplay.Caption = Form1.txtNameenter.Text
End Sub

Trying with command button:

Private Sub cmdGenerate_Click()
lblNamedisplay.Caption = Form1.txtNameenter.Text
End Sub

Neither work, can someone please help me out cheers!!!
 
oh my god im so stupid lol

I was putting that code on form3 under the label where i wanted the name entered to be displayed. Where as you put it where the name was entered. I just changed mine and it now works!!!

Thanks I LOVE YOU!!!

EDIT: i will be coming back shortly because:

i need to know how to calculate an average from different figures entered and for it to happen once a command button is displayed.

I also need a total to be calculated on figures once a command button is entered.

i also want the words PASS, FAIL, MERIT, DISTINCTION to be displayed and set to display and defined by a certain number. They should be displayed depending on the total calculated on the figures entered. Again this needs to happen on the press of a command button.

And if possible i want all this to happen when 1 command button is pressed i dont want a seperate command button for each bit if possible?
 
Last edited:
Ok im now at, what i believe to be, the hard part.

Please see images and explanation below:

form2.jpg


Ok on the picture above a student is to enter their marks into the text boxes on the right. I will then want to use these figures entered for some calculations on form3 (below).

form3.jpg


On this form i want the marks entered on the previous form to be calculated and then display : Total Marks and in the box below Average Mark, then in the box below will be grade. In the grade box i want to display Fail, Pass, Merit or Distinction, this will depend on the total mark they have got.

I want the grades defined as

less than 39 = fail
equal to or more than 40 but less than 55 = pass
equal to or more than 56 but less than 70 = merit
more than 70 = distinction

I want these words to be displayed i the grade box after the calculation has been made.

I want all the above things to happen once the generate button has been pressed, is it possible to do this? Basically so that total, average and grade will all be calculated and displayed at once?

Thanks peeps
 
Hi

The following code should do the trick, you might need to change the name of the textbox on your form2 as I have just used a control array for the text boxes as it makes it easier to add the values together.
There’s probably a neater way of doing this but this should do the trick.

Option Explicit
Dim Total As Integer
Dim Avg As Integer

Private Sub btn_generate_Click()
Dim x As Integer

For x = 0 To Form2.Text1.Count - 1
Total = Form2.Text1(x) + Total
Next x
txt_Total = Total

Avg = Total / 5
txt_avg.Text = Avg

If Total <= 39 Then
Me.txt_grade.Text = "Fail"
Exit Sub
End If
If Total >= 40 And Total <= 55 Then
Me.txt_grade.Text = "Pass"
Exit Sub
End If
If Total >= 56 And Total <= 70 Then
Me.txt_grade.Text = "Merit"
Exit Sub
End If
If Total >= 71 Then Me.txt_grade.Text = "distinction"

Total = Nothing
Avg = Nothing



End Sub

Cheers

MattB
 
shadow4509 said:
wow thanks matt that is much appreciated!!!! Just a quickie should this code be placed under the generate button?

Maybe the bits between "Private Sub btn_generate_Click()" and "End Sub"? Just a guess. :D :D
 
Matt,

Thanks again for the code! You say to change the name of the text box on form2, because you have written the code using a control array!

I really have about 3 weeks knowledge of VB (by 3 weeks i mean 3x3hour lessons ;) ). Could you please explain this a little to me in simple terms and possibly recommend what i should rename the textbox/s to?

I also await flaming from other members for my previous posts lol
 
Hi Shadow

The easiest way to for you to create a control array is add a new textbox onto form2 call it what ever you like, then take a copy of the text box and paste it onto your form2.
When you do this you will should be asked by VB if you want to create a control array click yes and just paste as many text boxes on to the form as you need.
The only other you will need to do is change the name of the textbox under the generate button from Text1 to whatever you named your textbox.

Hope this is of some help

Cheers

Matt
 
Hi Matt,

I'm having problems still with my work and wondered if you might be able to help me out again. I did what you said above but im getting an error meesage when pressing the generate button. The error message reads:

Compile Error Invalid inside procedure

IF you have 5 mins could you post back or perhaps add me on msn:

[email protected]

Cheers
 
I'm saying this in the nicest possible way, but the only way to learn is to do things yourself... Quite a lot of errors are quite easy to fix, the description and piece of code it points to will help you on your way, and if you google each error description to see how others fixed it you might also see the problem.

It's all very well getting the occasional bit of help here and there, but I dunno - I just scroll through this thread and see Matt's given you a big chunk of code and wonder if you've actually learnt anything from it. I remember trying to help a friend with his project, and after a while it became quite apparent that he was switching off when I explained how to do something then almost waiting for me to type it for him.

Of course learning from other peoples code is perfectly fine and sometimes you do just need to be shown how to do it, but you know. Just make sure you're learning from it, not just getting your work done for you :p

Things being invalid inside (or outside) a procedure are quite often hidden and hard to find if you have bad a indenting structure. Off the top of my head (haven't used VB6 for a while), something being invalid INSIDE a procedure would be if you created a function within a function. Something invalid OUTSIDE a procedure would be writing code that's not within a subroutine like Form_load.

:) I could probably summarise this whole post by saying "just make sure you've spent some good time trying to fix it yourself before you ask someone to step you through the problem"
 
yeah i get what your saying furnace but i think i've tried everything and have been working on it for 2 days now trying to crack it without asking but i've hit a wall!
 
ok i think im one step closer to getting this to work, i have changed a few things and now im getting the error message:

variable not defined

and its higlighting the x
 
"variable not defined" means pretty much what it says :)

in VB6, you can define "OPTION EXPLICIT" which basically means every variable has to be defined (you know like, when you say Dim strMyText as String - that's defining a variable).

If you haven't used "OPTION EXPLICIT", then whenever VB6 comes across a variable that hasn't been defined, it will automatically define it.

It's always a good idea to have option explicit set - firslty, it teaches you 'proper' programming practises, and secondly it can REALLY help if you run into problems with variables later on (such as miss-spelling a variable name, as it will error, rather than just creating the miss-spelt name).

I'm going to guess you have option explicit set, so you have to define a variable before you use it :)
 
hi furnace

thanks for your post, your right i have used Option Explicit but im unsure ive used it in the right place?

As in my posts above im trying to get 3 calculations to happen on the press of the generate button, so i have coded (as mattyb wrote out for me):

Option Explicit
Dim Total As Integer
Dim Avg As Integer

and then

For x = 0 To Form2.Text1.Count - 1
Total = Form2.Text1(x) + Total
Next x
txt_total = Total

Its the first x in the above code that is being highlighted in when the variable not defined error message appears.
 
Yeah, see in that code you are using three variables - Total, Avg and x.

You can see at the top of that code, two variables have been created (aka 'defined') - Total and Avg. Both are integers because numbers are going to be stored in them. If text was going to go in them, they'd be a string, etc.

Remeber, like I said, when you have option explicit set visual basic requires that any variable you use must be defined before you use it.

I'm trying hard not to give you the answer here :p but at the same time I'm trying to make you understand then figure out the problem yourself..!
 
furnace are you my programming tutor?????

I know your trying to push me along and i understand that i need to create a variable for x to define it. The problem is i really dont know how, and google is not being my friend at the moment.
 
Back
Top Bottom