Why does an extra row automatically get added when I insert a new row?

siamandm
BronzeLounger
Posts: 1323
Joined: 01 May 2016, 09:58

Why does an extra row automatically get added when I insert a new row?

Post by siamandm »

Hello All
in this subform below, when I add a row it adds another row by itself! why is that please?
Screenshot 2024-10-02 090048.png
below is the code for after update event for the combo box categories

Code: Select all

Private Sub cboProductCategories_AfterUpdate()
    
    On Error GoTo ErrorHandler ' Start error handling
    
    ' Check if a category is selected in cboProductCategories
    If Me.cboProductCategories = 0 Then
        ' Set RowSource to show all products
        Me.ProductID.RowSource = "qrycboProducts_All"
    Else
        ' Set RowSource to show products filtered by category
        Me.ProductID.RowSource = "qrycboProducts"
    End If

    ' Select the first item in the ProductID combo box
  '  If Me.ProductID.ListCount > 0 Then
        Me.ProductID = Me.ProductID.Column(0, 0)  ' Select the first item
   ' End If
          ' Call the AfterUpdate event for ProductID
    Call ProductID_AfterUpdate

    ' Exit sub to avoid reaching the error handler if no error occurs
    Exit Sub

ErrorHandler:
    ' Display a message box with the error number and description
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error in cboProductCategories_AfterUpdate"
    
    ' Optionally, you can also log the error to a file or table if needed.
    
End Sub

and below is the code for the on current of the form

Code: Select all

Private Sub Form_Current()
On Error GoTo Err_Handler ' Start error handling

    ' Set the value of the unbound combobox
    Me.cboProductCategories = Me.ProductCategoryID

    ' Requery products dropdown so it will have products for this category
    Me.ProductID.Requery

    ' Validate the form and remove any error highlights
    ' ValidateForm_RemoveHighlights Me

    ' Cache the row, in case user changes the product
    'If Not Me.NewRecord And Me.OrderDetailStatusID = enumOrderDetailStatus.odsAllocated Then
     '   m_RowCache.ProductID = Me.ProductID
    '    m_RowCache.Quantity = Me.Quantity
   ' End If
'
    ' Exit the subroutine before reaching the error handler if no error occurs
    Exit Sub

Err_Handler:
    ' Display a message box with the error number and description
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error in YourSubroutine"
    
    ' Optionally, you can add logging or additional error handling here, if needed

    Resume Next ' Continue execution with the next line of code after handling the error
End Sub
and the row source for the form as below
Screenshot 2024-10-02 103114.png
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 79444
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Why does an extra row automatically get added when I insert a new row?

Post by HansV »

This is the default behavior. A form with a recordset that can be updated always displays a new record at the end.
If you set the Allow Additions property of the form to No, this new record won't be displayed, but as a consequence users won't be able to create a new record the normal way - you'd have to provide a command button with code that creates a new record if required.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1323
Joined: 01 May 2016, 09:58

Re: Why does an extra row automatically get added when I insert a new row?

Post by siamandm »

Thank you very much for your reply,

I have attached the database, i get an error when i put data in the from frmOrderDetails
Stock-Update.zip
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 79444
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Why does an extra row automatically get added when I insert a new row?

Post by HansV »

Remarks:
1) The control source of the OrderStatusName text box on frmOrderDetails does not exist. OrderStatusID is a field in the OrderDetails table, not in the Orders table. Remove this control.
2) The combo box ProductID on sfrmOrderLineItems is unbound. It should have ProductID as control source.
3) The OrderStatus table should contain at least one record.
4) You should add a combo box bound to OrderDetailStatusID to sfrmOrderLineItems.

I'm sure that a lot more should be corrected, but for the moment, see the attached version.

Stock-Update.zip
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1323
Joined: 01 May 2016, 09:58

Re: Why does an extra row automatically get added when I insert a new row?

Post by siamandm »

Thank you very much for the answer,
the order status was making some issues for me so i decided to remove it as I believe I don't need this in the mean time, I think it was causing issue because the table was not containing any record, If i want to add this order status in the feature will this be easy or I have to do it now?

this sentence scared me " I'm sure that a lot more should be corrected, but for the moment, see the attached version. "

User avatar
HansV
Administrator
Posts: 79444
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Why does an extra row automatically get added when I insert a new row?

Post by HansV »

You can always add the order status later on.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1323
Joined: 01 May 2016, 09:58

Re: Why does an extra row automatically get added when I insert a new row?

Post by siamandm »

Thank you very much :)