I am New With Ms Access and want to create a little Database for shop.
Here is My Form structure Based on Table "Sales".
Label |
Name |
Customer Name |
CustomerID(FK)(Combo Box) |
Product Name |
ProductID(FK)(Combo box) |
Date |
SalesDate(Bounded with Table 'Sales') |
Quantity |
Quantity(Text Box) |
Unit Price |
PricePerUnit(text box) |
Total |
TotalAmount(text box) |
Paid |
PaidAmount(text Box) |
Remaining Amount |
RemainingAmount(text Box) |
Here is OnClick event code:
Code:
Private Sub SaveButton_Click()
On Error GoTo ErrHandler
' Validate mandatory fields
If IsNull(Me.CustomerID) Then
MsgBox "Select a customer!", vbExclamation, "Missing Data"
Exit Sub
End If
If IsNull(Me.ProductID) Then
MsgBox "Select a product!", vbExclamation, "Missing Data"
Exit Sub
End If
If Not IsNumeric(Me.Quantity) Or Me.Quantity <= 0 Then
MsgBox "Enter valid quantity (greater than 0)!", vbExclamation, "Invalid Input"
Exit Sub
End If
If Not IsNumeric(Me.PricePerUnit) Or Me.PricePerUnit <= 0 Then
MsgBox "Enter valid price (greater than 0)!", vbExclamation, "Invalid Input"
Exit Sub
End If
' Calculate totals
Me.TotalAmount = Me.Quantity * Me.PricePerUnit
Me.RemainingAmount = Me.TotalAmount - Nz(Me.PaidAmount, 0)
' Save the record
If Me.Dirty Then DoCmd.RunCommand acCmdSaveRecord
' Clear fields for next entry (retain CustomerID if needed)
Me.ProductID = Null
Me.Quantity = Null
Me.PricePerUnit = Null
Me.PaidAmount = Null
Me.TotalAmount = Null
Me.RemainingAmount = Null
MsgBox "Sale saved successfully!", vbInformation, "Success"
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
If Me.Dirty Then Me.Undo
End Sub
'======= AUTO-UPDATE CALCULATIONS =======
Private Sub Quantity_AfterUpdate()
If IsNumeric(Me.Quantity) And IsNumeric(Me.PricePerUnit) Then
Me.TotalAmount = Me.Quantity * Me.PricePerUnit
Me.RemainingAmount = Me.TotalAmount - Nz(Me.PaidAmount, 0)
End If
End Sub
Private Sub PricePerUnit_AfterUpdate()
If IsNumeric(Me.Quantity) And IsNumeric(Me.PricePerUnit) Then
Me.TotalAmount = Me.Quantity * Me.PricePerUnit
Me.RemainingAmount = Me.TotalAmount - Nz(Me.PaidAmount, 0)
End If
End Sub
Private Sub PaidAmount_AfterUpdate()
If IsNumeric(Me.TotalAmount) Then
Me.RemainingAmount = Me.TotalAmount - Nz(Me.PaidAmount, 0)
End If
End Sub
ُProblem is:
The code is not able to clear the last field RemainingAmount's entries and not able to save the this field data to into the table. Data of Other Fields cleared and saved into to table successfully.
Please Help.
Thanks