Update a field if a condition is true and another if not true

AOGFADUGBA

Board Regular
Joined
Sep 30, 2015
Messages
74
Hi All, i have a form and i want two fields to be updadated with the content of another field based on a condition.
the fields are "PChequeNo" "PCash D" "PAmount P" "PBankNet" .
PChequeNo is a text field
PCash D, PAmount P, PBankNet are all currency field

i want if payment is made by cheque (PChequeNo will not be empty) the amount paid (PAmount P) will appear in the PBankNet field, but if cash is paid(PChequeNo will be empty) then it will appear in the paid cash (Pcash D) field.

below is the code i wrote. it worked to a point:

Private Sub FA_AfterUpdate()
If IsNull(Me.PChequeNo) Then
[PCash_D] = PAmount_P
ElseIf Not Me.PChequeNo Then
[PBankNet] = PAmount_P
End If
End Sub


if the PChequeNo field content is deleted the PBankNet Field still retain the amount paid and so does Pcash D.
But if the PChequeNo is not empty, PBankNet Field retain the amount paid and Pcash D remains empty again

Please can someone help
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try the following Code into the [PChequeNo] & [PAmount_P] after update Event Procedure:

Code:
Private Sub PChequeNo_AfterUpdate()


Dim dblPaid As Double


dblPaid = Nz(Me![PAmount_P], 0)


If dblPaid = 0 Then
  Exit Sub
End If


If IsNull(Me![PChequeNo]) Or Len(Me![PChequeNo]) = 0 And Me![PBankNet] > 0 Then
   Me![PCash_D] = dblPaid
   Exit Sub
ElseIf Len(Me![PChequeNo]) = 0 And Me![PAmount_P] > 0 Then
    Me![PCash_D] = dblPaid
    Me![PBankNet] = 0
End If


End Sub

Code:
Private Sub PAmount_P_AfterUpdate()
Dim varChq As Variant
Dim dblPaid As Double


dblPaid = Nz(Me![PAmount_P], 0)


If dblPaid = 0 Then
  MsgBox "Amount Paid Field Not filled."
  Exit Sub
End If
If Len(Me![PChequeNo]) > 0 And Me![PAmount_P] > 0 Then
    Me![PCash_D] = 0
    Me![PBankNet] = dblPaid
ElseIf IsNull(Me![PChequeNo]) And Me![PAmount_P] > 0 Then
    Me![PCash_D] = dblPaid
    Me![PBankNet] = 0
End If


End Sub

I have made few test-runs and corrected the code. You may try it out and refine the code, if necessary.
 
Upvote 0

Forum statistics

Threads
1,221,837
Messages
6,162,282
Members
451,759
Latest member
damav78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top