Results 1 to 7 of 7
  1. #1
    New Comer is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2025
    Posts
    10

    Lightbulb Need Help With VBA code

    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

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    If you used a BOUND form and controls, this code would likely be unnecessary or reduced.

    Why is it "not able" - error message, wrong result, nothing happens?

    Calculated data such as Total and Remaining would not normally be saved to table.

    You could provide db for analysis. Follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    New Comer is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2025
    Posts
    10

    File attached

    Dear sir,
    No error message appears. The code clears the form and saves the data except for the RemainingAmount field
    ٖFile attached.
    Attached Files Attached Files

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Your forms ARE BOUND - data entered into controls is passed directly to table.

    When you set controls to NULL, you are DELETING data from record.

    Your form is set with DataEntry Yes - that means form opens to a new record row. You input data and that initiates creating a record.

    Your SaveRecord code commits data to table then your code deletes data.

    Data from BOUND form is committed to table when closing or navigating or run code.

    You need to rethink what you are doing. Instead of setting controls to Null, move to new record row or close form.

    Again, saving calculated data is usually unnecessary and risks data getting "out of sync".
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    436
    in addition to the above advice, you also add Validation
    on BeforeUpdate event of your form.

    see the code on the Save button and test your form.
    Attached Files Attached Files

  6. #6
    New Comer is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2025
    Posts
    10
    Thank You Sir!! You make My day. I am thankful to you. I just Run the form.. every thing is fine even data stored in all fields. You are Great.

  7. #7
    New Comer is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2025
    Posts
    10
    Quote Originally Posted by jojowhite View Post
    in addition to the above advice, you also add Validation
    on BeforeUpdate event of your form.

    see the code on the Save button and test your form.
    Thank You Sir!! You make My day. I am thankful to you. I just Run the form.. every thing is fine even data stored in all fields. You are Great.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-02-2018, 01:10 PM
  2. Replies: 3
    Last Post: 10-16-2014, 08:49 AM
  3. Replies: 6
    Last Post: 10-04-2014, 12:22 PM
  4. Replies: 6
    Last Post: 03-26-2014, 10:04 AM
  5. [Need Help ASAP] Form VBA Code
    By endlessd in forum Access
    Replies: 5
    Last Post: 03-27-2011, 11:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums