VBA IF THEN ELSE statement stopped working... (1 Viewer)

ChrisC

Registered User.
Local time
Today, 03:09
Joined
Aug 13, 2019
Messages
90
Hi everyone, I hope you are all keeping safe out there!

For an unknown (to me) reason, the following VBA code has stopped working as it should.

Code:
Private Sub txtNewQty_AfterUpdate()

If Me.txtNewQty < Me.txtLocatStock Then
   Me.txtTransactionInfo.Value = "You are deleting " & Me.txtLocatStock - Me.txtNewQty & " from this location"
   Me.txtStockAdjustmentType.Value = "Stock Loss"
   Me.txtTransactionQty.Value = 0 - (Me.txtLocatStock - Me.txtNewQty)
    
Else
   Me.txtTransactionInfo.Value = "You are adding " & Me.txtNewQty - Me.txtLocatStock & " to this location"
   Me.txtStockAdjustmentType.Value = "Stock Gain"
   Me.txtTransactionQty.Value = Me.txtNewQty - Me.txtLocatStock

End If

End Sub

In a nutshell: the value of txtLocatStock is prepopulated. If the figure entered manually in to txtNewQty is greater than txtLocatStock, it should say "You are adding *the difference* to this location".
Alternatively, if the entered amount is Less than txtLocatStock, then it should say that you are "deleting" from this location.

The "less than" option works fine, however the "more than" option simply doesn't work and the message the user gets is: "You are deleting -"x" from this location"; where X is the difference value.

As it happens, deducting a negative does provide the correct result, but I need the wording to be correct for the user as it is understandably causing confusion.

This has worked fine before and I just cant work out why it has stopped now!

Many thanks for all your help!

Chris
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:09
Joined
Oct 29, 2018
Messages
21,455
Hi Chris. Have you tried stepping through the code?
 

ChrisC

Registered User.
Local time
Today, 03:09
Joined
Aug 13, 2019
Messages
90
Hi DB guy,

yes, even went through everything else line by line on the form that is completely unrelated to this part) and i cant see anything untoward.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:09
Joined
Oct 29, 2018
Messages
21,455
Hi DB guy,

yes, even went through everything else line by line on the form that is completely unrelated to this part) and i cant see anything untoward.
Can you post a sample copy of your db?
 

ChrisC

Registered User.
Local time
Today, 03:09
Joined
Aug 13, 2019
Messages
90
I will try but sadly its a bit of a beast with lots of information already in it (we have been using it in the business I work for a while now, which is odd as to why it should stop working now). I will see what I can do though!

Many thanks
Chris
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 23:09
Joined
May 21, 2018
Messages
8,525
VBA never stops working unless there is corruption of the db or a missing reference. The condition is not being met as you thing. Also check the datatype on these fields to ensure not string. My guess is this is being called before the field is updated.

Code:
debug.print "new qty: " nz(Me.txtNewQty,0) & " Stock" " & nz(Me.txtLocatStock,0)


If nz(Me.txtNewQty,0) < nz(Me.txtLocatStock,0) Then
   Me.txtTransactionInfo.Value = "You are deleting " & Me.txtLocatStock - Me.txtNewQty & " from this location"
   Me.txtStockAdjustmentType.Value = "Stock Loss"
   Me.txtTransactionQty.Value = 0 - (Me.txtLocatStock - Me.txtNewQty)
   
Else
   Me.txtTransactionInfo.Value = "You are adding " & Me.txtNewQty - Me.txtLocatStock & " to this location"
   Me.txtStockAdjustmentType.Value = "Stock Gain"
   Me.txtTransactionQty.Value = Me.txtNewQty - Me.txtLocatStock


End If

what the debug say?
 

ChrisC

Registered User.
Local time
Today, 03:09
Joined
Aug 13, 2019
Messages
90
Hi MajP, thank you.

Forgive my ignorance - how do I get that debug to run? simply add that line of code in after Private Sub txtNewQty_AfterUpdate() I guess?
 

ChrisC

Registered User.
Local time
Today, 03:09
Joined
Aug 13, 2019
Messages
90
Hi MajP,

I ran things with the following tests:
txtLocatStock = 30
txtNewQty = 38 (which would add 8)

and then I tried txtNewQty = 28 (which would deduct 2).

then results of the debug are:
1608132085523.png
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:09
Joined
Oct 29, 2018
Messages
21,455
Hi MajP,

I ran things with the following tests:
txtLocatStock = 30
txtNewQty = 38 (which would add 8)

and then I tried txtNewQty = 28 (which would deduct 2).

then results of the debug are:
View attachment 87569
Hi. Can you please show us exactly how you implemented @MajP's code? I think you may have to modify this:
debug.print "new qty: " nz(Me.txtNewQty,0) & " Stock" " & nz(Me.txtLocatStock,0)
into this:
Code:
debug.print "new qty: " & nz(Me.txtNewQty,0) & " Stock: "  & nz(Me.txtLocatStock,0)
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 23:09
Joined
Feb 19, 2002
Messages
43,233
Please fix the debug command.

debug.print "new qty: " & nz(Me.txtNewQty,0) & " Stock: " & nz(Me.txtLocatStock,0)
 

ChrisC

Registered User.
Local time
Today, 03:09
Joined
Aug 13, 2019
Messages
90
Hello everyone, sorry for only just being able to get back to you now.

Code:
Private Sub txtNewQty_AfterUpdate()

Debug.Print "new qty: " & Nz(Me.txtNewQty, 0) & " Stock: " & Nz(Me.txtLocatStock, 0)

If Me.txtNewQty < Me.txtLocatStock Then
   Me.txtTransactionInfo.Value = "You are deleting " & Me.txtLocatStock - Me.txtNewQty & " from this location"
   Me.txtStockAdjustmentType.Value = "Stock Loss"
   Me.txtTransactionQty.Value = 0 - (Me.txtLocatStock - Me.txtNewQty)
    
Else
   Me.txtTransactionInfo.Value = "You are adding " & Me.txtNewQty - Me.txtLocatStock & " to this location"
   Me.txtStockAdjustmentType.Value = "Stock Gain"
   Me.txtTransactionQty.Value = Me.txtNewQty - Me.txtLocatStock

End If

End Sub

Here is the result when entering the txtNewQty value as 38 then 28.
1608194300637.png


thank you!
Chris
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 23:09
Joined
Feb 19, 2002
Messages
43,233
and what happens when you step through the code?
 

Users who are viewing this thread

Top Bottom