Full Version: Why Doesn't this simple statement work?
UtterAccess Forums > Microsoft® Access > Access Forms
Hank44
Have a form that has field to calculates sums etc. The following produces the math function even when 0 is used... Code Follows:
Private Sub TxtRate1_AfterUpdate()
If Me.TxtRate1 = 0 Then
Me.TxtChargeTotal1 = "As Agreed"
Else
Me.TxtChargeTotal = Me.TxtChargeableWeight1 * Me.TxtRate1
End If
Why For does this not seem to produce the desired result?.... Perplexed.......
Omean, after all, it should work.. Shouldn't it?
It doesn't fault, it just does the math no matter what.....
theDBguy
Hi Hank,
Try changing your code to this:
If Val(Me.TxtRate1) = 0 Then
...
Hope that helps...
EDIT: Changed Int() into Val(). Sorry.
Hank44
Okay,
tried your suggestion and it doesnt seem to update the fields. If anything but 0, it does the calculations. I have added the entire sub:
Private Sub TxtRate1_AfterUpdate()
If Int(Me.TxtRate1) = 0 Then
Me.TxtChargeTotal1 = "As Agreed"
Else
Me.TxtChargeTotal = Me.TxtChargeableWeight1 * Me.TxtRate1
End If
If Forms![On Track Logistics Management House Bill]![Combo164] = "PREPAID" Then
Me.TxtWeightChgPPD = Me.TxtChargeTotal
Me.TxtTotalPPD = Me.TxtChargeTotal
Me.TxtChargeTotal1 = Me.TxtChargeTotal
Else
If Forms![On Track Logistics Management House Bill]![Combo164] = "COLLECT" Then
Me.TxtWeightChgCOLL = Me.TxtChargeTotal
Me.TxtCollectChgs = Me.TxtChargeTotal
e.TxtChargeTotal1 = Me.TxtChargeTotal
End If
End If
End Sub
What do you think....... Interesting, isnt it.. All works except for the If,Else, Zero portion of the statement.
Jeff B.
Are you saying "zero" (0) because you don't see anything there? If so, are you sure it isn't a "null" or a "zero-length string" ("")?
theDBguy
Hi Hank,
My apologies. I thought I had edited my post quick enough but I guess I was wrong.
Note: I changed Int() into Val().
Hope that helps...
Hank44
Okay,
changed to Val and still cant get it to work. I am trying to set it so that if a user enters a zero it will default to "As Agreed" Can you help?
theDBguy
Hi,
Can you post a small copy of your db with test data? Will the user type in "zero" or the number "0"?
Hank44
It will be the number 0.
I have a split database that has multiple tables and forms. Have never posted a copy here before. Do not really know how either. I guess I can try to provide the form that this is on. The entire DB is 200 Mb +
theDBguy
Have you tried stepping through the code and evaluating the value in each variable as you run it?
Hank44
I know that this sounds stupid but I do not know how to do this! I am a quick study and would love to sample if you could give me a little instruction (in very layman s terms) How do I step through? I thought that it had something to do with a calculated control not updating when it has had data already
theDBguy
Hi,
o problem. What you could do is add a "break point" in your code by clicking on the gray bar on the left side next to the line of code where you want the code execution to stop. For example, in your case, you could add the break point next to the first If/Then statement.
When you go back to the form and click on the button, the code window should open up and stop the execution at the point where you put the break point. If you hover your mouse over the variable and control names, you will see their current values. To step through each line of code, you can hit the F8 key.
Hope that helps...
datAdrenaline
Assuming the control is UN-bound, give this a shot:
If Val(Me.TxtRate1 & "") = 0 Then ...
....
If the form is BOUND, then you will want to update the record first ... and last ... if you are modifying values in a table:
CODE
    If Me.Dirty = True Then Me.Dirty = False
    If Val(Me.TxtRate1 & "") = 0 Then ...
    ...<all your code>
    End If
    If Me.Dirty = True Then Me.Dirty = False
End Sub
Hank44
Okay,
Oused this debugging method to solve this problem. Thanks!!!! I can use this in the future to help with a lot.
THere was the problem.
named several controls with similar names and got them mixed in the writing of the code. IF I WAS SMART ENOUGH TO MENTION THE PROPER CONTROLS IN MY CODE THEN IT MIGHT EXECUTE PROPERLY!. I discovered this when using your debugging method and got a "Null" on mouseover.
Now I will retire to the tool shed where I will submit myself to pain infliction with a claw hammer to my left hand. This should provide for a "lesson Learned"
PS When you hear screaming, it is just me submitting to punishment!!!!
Thanks for you help though, I hope that I didn't waste your time.
Hank
theDBguy
Hi Hank,
Glad to hear you got it sorted out. Jeff, Brent, and I are happy to help. Good luck with your project.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.