PDA

View Full Version : Help with a code



jonnye1122
09-30-2008, 03:20 PM
hi im a total noob when it comes to vba so I was wondering if i can get some help here i have office 07 and was trying to code a program to add things together when i put em in a text box

here is there code (could u tell me where i went wrong?):
Option Explicit
Dim Text2 As Integer
Dim Text4 As Integer
Dim Text6 As Integer
Dim Text8 As Integer
Dim Text10 As Integer
Dim Text12 As Integer
Dim Text14 As Integer
Dim Text16 As Integer
Dim Text18 As Integer
Dim Text20 As Integer
Dim Text22 As Integer
Dim Text24 As Integer
Dim Text26 As Integer
Dim Text28 As Integer
Private Sub Command30_Click()
Jonnye = Text2
Hitman = Text4
HiAndBye = Text6
Raerap = Text8
Primoz = Text10
Tmgun = Text12
Curakil = Text14
G -Jonathan = Text16
evan = Text18
Scorpio = Text20
Spitfire = Text22
Sjabba = Text24
Brian = Text26
Totaal = Text28

Totaal = Totaal + 1

If IsNumeric(Jonnye) And IsNumeric(Hitman) And IsNumeric(HiAndBye) And IsNumeric(Raerap) And IsNumeric(Primoz) And IsNumeric(Tmgun) And IsNumeric(Curakil) And IsNumeric(G - Jonathan) And IsNumeric(evan) And IsNumeric(Scorpio) And IsNumeric(Spitfire) And IsNumeric(Sjabba) And IsNumeric(Brian) Then
Totaal = ((Jonnye) + (Hitman) + (HiAndBye) + (Raerap) + (Primoz) + (Tmgun) + (Curakil) + (G - Jonathan) + (evan) + (Scorpio) + (Spitfire) + (Sjabba) + (Brian))
End If


End Sub

Demosthine
09-30-2008, 04:02 PM
Good Afternoon.

First, I'd like to ask you to post a copy of your database file so that we can avoid duplicating all of the Form creation. This will let me be more accurate in responding to your question.

Next, from the looks of your code, you have not assigned anything to your variables. At the top of your module, you define a large number of Integer variables. By default of these values are assigned a value of 0.

Now, once inside your Click Event, you assign another large number of variables equal to your Integer variables defined at the Module level. Previous to these assignments, I'll remind you all of your 'TextXX' variables are equal to 0.

In regards to all of your named variables (Jonnye, Hitma, etc.), with the Option Explicit code at the beginning of your Module, all of these should cause an error when you attempt to run the code. It will most likely give you a "Variable not defined" error.

The last obvious error I noticed is that your line "G -Jonathan = Text16" does not have a valid variable name. Variable names can not contain symbols such as spaces or dashes. This will cause an error on this line, as well as in your IsNumber Function and the actual addition line.

Hopefully this helps you get going some more.
Scott

jonnye1122
09-30-2008, 04:19 PM
wel ill upload the file if thats ok....and i really need help with this:P i know nearly nothing

Demosthine
09-30-2008, 04:25 PM
Hi again.

Would you mind saving the database as an Access 2003 database? I don't have Office 2007. That'll help me out.

Scott

jonnye1122
09-30-2008, 04:27 PM
kk will do
HUH? it doesnt upload anymore? when i try to do it...it says invalid extension
would it possible for me to mail it to u??

jonnye1122
09-30-2008, 04:44 PM
ok i think i got it...had to download winzip because i had winrar and it doesn't take winrar

Demosthine
09-30-2008, 05:57 PM
Hey there.

I got the database, and opened it up. So we'll go step by step...

First thing I noticed from looking at the code is that at the end of your Command30_Click Event, you have an extra End If. Remove this first.

The second thing I'll mention is that at the Module Level, you are defining all of your Integer Variables with the exact same name as all of your objects. This creates an error because of duplicate Object names.

Because you already have the TextBoxes defined, it is not necessary to do that again, much less as an Integer. Delete all of your Dim ... as Integer statements from Line 2 through Line 15.

Inside your Command30_Click Event, you need to Dim each of your variable as Integers. I.E. Dim Jonnye as Integer. Remember also, that you can not have symbols such as spaces and dashes in your Variable Names. You'll need to find something different to name 'G -Jonathan' like 'GJonathan'.

The next step is assigning the values from the TextBoxes to the Variables. You need to do so some error checking before you actually assign it. Otherwise you'll find yourself getting an error attempting to assign a Null Value to an Integer.

Use the code below for checking to determine if there is a value in the TextBox. If there is a value, assign it. If not, assign a zero value.


If IsNull(Text2.Value) Then
Jonnye = 0
Else
Jonnye = Text2.Value
End If



Now, all of this information should get you started and moving along quite nicely. Let me know if you need more explanation on anything or more errors come up.

Scott

jonnye1122
09-30-2008, 06:47 PM
THX ALLLOTTT!!!!

jonnye1122
09-30-2008, 07:20 PM
srry to bother u again...but i get this error ill send u my code that i have now...for some reason it wont save the way i want it...so ill just send the code...hope u dont mind

Demosthine
09-30-2008, 07:52 PM
Don't worry about bothering me. I wouldn't be here if I wasn't willing to help out, now would I?

Now, where are you getting the error. I am assuming it is the 'Invalid Use of Null' error I mentioned before. This error comes up because one or more of your TextBoxes do not have a value in them. I would assume that you are not putting a value in the Text28 TextBox because that is where your Totaal will calculate to.

First, I'm not quite following why all of your Dim statements are in outside of Command30_Click event.

But aside from that, you need to delete your lines where you merely assign the TextBox Values to the variables. Avoiding this error is the reason for all of the If IsNull statements. Once you remove those lines, your code should work.

The last note of interest is that would be that you have to set the resulting value of Totaal to Text 28.

Scott

jonnye1122
09-30-2008, 07:55 PM
thx man....ill call it a day for today...already got an headache from it:P
ill contact u tomorrow if i get stuck again...c u