Solved

curWeeklyPay1 = 0

Posted on 2004-04-07
3
154 Views
Last Modified: 2010-05-02
I have the following simple question. I am studying visual basic and spent several hours trying to figure out why an equation turns out to be zero

Dim curWeeklyPay1 As Currency
curWeeklyPay1 = 250 + (2000 - 250) * 0.15 - 1000
curweeklypay1 through the debug shows a value of zero.

gonzal13(Joe)
0
Comment
Question by:gonzal13
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
jmwheeler earned 50 total points
ID: 10776947
I tried it and did not get 0, I got -487.5 which is the correct answer.  Try putting   Option Explicit  at the top of your code like the example below, if you get an error message about Variable not defined then you misspelled your variable somewhere.  This sounds like a very likely cause of the problem.  I would suggest always using Option Explicit, it forces you to declare you variables and will save you a lot of headaches when creating large programs.

Option Explicit

Private Sub Command1_Click()

    Dim curWeeklyPay1 As Currency
    curWeeklyPay1 = 250 + (2000 - 250) * 0.15 - 1000
    MsgBox (curWeeklyPay1)
   
End Sub
0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 10776974
Also, one more thought when using debug.  There are some other possibilities.  CurWeeklyPay1 will show 0 until this (curWeeklyPay1 = 250 + (2000 - 250) * 0.15 - 1000) line of code is run, so if you break on this line you will see 0.  Also, you are using Dim curWeeklyPay1 which will cause it to lose it's value after you leave it's current scope.  For example in my code from my previous post, curWeeklyPay1 would not retain a value after the Command1_Click() subroutine is finished.  You could try changing your declarion to :

Private CurWeeklyPay1 as Currency

and see if that helps.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 10810356
Thanks, you are right. I was having tunnel vision and was hyperfocused. The points are yours
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now