if else statement (1 Viewer)

rainbows

Registered User.
Local time
Today, 07:12
Joined
Apr 21, 2017
Messages
425
happy new year to everyone

what i am trying to do is when i enter a number in the yellow box called update it will remove that amount from the stock qty and it also did from the allocation but i am trying to change it so that if the allocation is less than the value i put into the update field then just make the allocation filed blank or 0

so the example below i would like to see 5 left in stock and 0 in allocation not -5 but if i had put 10 in the update field i would want to see 20 in stock and 10 in allocation

but i am not managing to get the if statement etc correct

thanks steve



1672650379564.png

Code:
Private Sub cmdUpdate_Click()
10        On Error GoTo cmdUpdate_Click_Error


30    Me.stockqty = Me.stockqty - Me.Update
35   If Me.allocation < Me.Update Then Me.Update = ""
36 Else
 
40    Me.allocation = Me.allocation - Me.Update
50    Me.Update = ""
60      Refresh
End If



 MsgBox "The selected item in Stocklist has been Updated", vbInformation
          
100       On Error GoTo 0
110       Exit Sub

cmdUpdate_Click_Error:
          
        
      
160       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdUpdate_Click, line " & Erl & "."

End Sub
 

ebs17

Well-known member
Local time
Today, 16:12
Joined
Feb 7, 2020
Messages
1,949
If Me.allocation < Me.Update Then Me.Update = ""
Else
' ...
End If


Code:
If Me.allocation < Me.Update Then
   Me.Update = ""                              ' instruction in new line
Else
   ' ...
End If
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:12
Joined
Feb 19, 2013
Messages
16,617
Aside from the fact you should not be storing calculated values such as stock balances- you calculate it when required - I presume your update field is numeric. So why are you setting it to a string?
 

rainbows

Registered User.
Local time
Today, 07:12
Joined
Apr 21, 2017
Messages
425
Code:
Private Sub cmdUpdate_Click()
10        On Error GoTo cmdUpdate_Click_Error


30    Me.stockqty = Me.stockqty - Me.Update
35   If Me.allocation < Me.Update Then
      Me.Update = 0
36 Else

40    Me.allocation = Me.allocation - Me.Update
50    Me.Update = 0
60      Refresh
End If



MsgBox "The selected item in Stocklist has been Updated", vbInformation
        
100       On Error GoTo 0
110       Exit Sub

cmdUpdate_Click_Error:
        
      
    
160       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdUpdate_Click, line " & Erl & "."


End Sub

i am not sure if i have put the code as you have said

but if you look at the first 2 screen shots the update is greater than the allocation so i was hoping to see 0 in the allocation field and 5 in the stock qty field . that bit is correct

the 3rd and 4th screen shots is correct because the update was not less greater than the allocation

steve



1672655109769.png

1672655163632.png



1672655208973.png




1672655302517.png
 
Last edited:

rainbows

Registered User.
Local time
Today, 07:12
Joined
Apr 21, 2017
Messages
425
i found my mistake, thanks for your help

steve
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:12
Joined
Feb 19, 2013
Messages
16,617
For the benefit of others who may view this thread, what was the mistake and what was the solution?
 

rainbows

Registered User.
Local time
Today, 07:12
Joined
Apr 21, 2017
Messages
425
Line 35 was wrong




Code:
Private Sub cmdUpdate_Click()
10        On Error GoTo cmdUpdate_Click_Error


30    Me.StockQty = Me.StockQty - Me.update
35   If Me.allocation < Me.update Then
      Me.allocation = 0
36 Else
 
40    Me.allocation = Me.allocation - Me.update
50    Me.update = 0
60      Refresh
End If



 MsgBox "The selected item in Stocklist has been Updated", vbInformation
          
100       On Error GoTo 0
110       Exit Sub

cmdUpdate_Click_Error:
          
        
      
160       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdUpdate_Click, line " & Erl & "."


End Sub
 

Users who are viewing this thread

Top Bottom