Results 1 to 6 of 6
  1. #1
    ezybusy is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2015
    Posts
    119

    check if subform has records, then do insert or update

    Hello everyone,
    Please i need your help on the following :

    i am trying to check that my subform has record and perform the following relevant actions :

    * if no records on subform the do insert
    * if there are record, then check if those records don't already exist.
    - if record already exist the do update


    - else do insert


    So far i manged to have everything working. But i am not happy using DCOUNT for this routine. Simply because i believe everything might get slower as the db tables grow in size due to data insertion.

    I believe there could be a more clever way to do this, especially by doing all the checkings only at the forms level (not involving tables check). Below is my working code, can someone help me tweak it properly ?

    Thank you in advance

    Code:
    Private Sub CmdGoToData_Click()
    Dim rst As DAO.Recordset
    
    ' We check the available stok first
    
    If Me.ProdStock < 1 Then
        Dialog.box "Ooops !!! stock épuisé!", vbOKOnly + vbExclamation, "Erreur!"
        Exit Sub
    
    Else
    
    DAO.DBEngine.BeginTrans
    
        Me.Parent!SaleDate = Now()
        Me.Dirty = False
        Me.Parent.Refresh
        
    ' We Check if there are sale entries on the subform SubSalePOS
    
        Set rst = Me.Parent.SubSalePOS.Form.RecordsetClone
        
        If rst.RecordCount = 0 Then
        
            ' Set the field value for deletion purpose
            Me.Parent.SubSalePOS.Form.TxtHold_Qty = 1
    
            ' Insert the product and a minimum quantity of 1 into sale details
            CurrentProject.Connection.Execute "INSERT INTO Tbl_SaleDetail ([SaleID_FK], [ProdID_FK], [SalePrice], [SaleQty]) " & _
            "VALUES (" & Nz(Me.Parent!SaleID_PK, 0) & ", " & Nz(Me.CboProduct.Column(0), 0) & ", " & Nz(Me.CboProduct.Column(5), 0) & ", 1);"
    
            ' update the product stock
            CurrentProject.Connection.Execute "UPDATE Tbl_Product SET ProdStock = ProdStock - 1  WHERE ProdID_PK=" & Nz(Me.CboProduct.Column(0), 0) & ""
            
        Else
        
            If DCount("SaleID_FK", "Tbl_SaleDetail", "[SaleID_FK] = " & Nz(Me.Parent!SaleID_PK, 0) & " And [ProdID_FK]=" & Nz(Me.CboProduct.Column(0), 0) & "") <> 0 Then
            
                ' Set the field value for deletion purpose
                Me.Parent.SubSalePOS.Form.TxtHold_Qty = 1
         
                ' Update the ordered quantity
                CurrentProject.Connection.Execute "UPDATE Tbl_SaleDetail SET SaleQty = SaleQty + 1 WHERE ProdID_FK=" & Nz(Me.CboProduct.Column(0), 0) & ""
            
                ' update the product stock
                CurrentProject.Connection.Execute "UPDATE Tbl_Product SET ProdStock = ProdStock - 1  WHERE ProdID_PK=" & Nz(Me.CboProduct.Column(0), 0) & ""
                
            Else
            
                ' Set the field value for deletion purpose
                Me.Parent.SubSalePOS.Form.TxtHold_Qty = 1
    
                ' Insert the ordered product and quantity into sale details
                CurrentProject.Connection.Execute "INSERT INTO Tbl_SaleDetail ([SaleID_FK], [ProdID_FK], [SalePrice], [SaleQty]) " & _
                "VALUES (" & Nz(Me.Parent!SaleID_PK, 0) & ", " & Nz(Me.CboProduct.Column(0), 0) & ", " & Nz(Me.CboProduct.Column(5), 0) & ", 1);"
    
    
                ' update the product stock
                CurrentProject.Connection.Execute "UPDATE Tbl_Product SET ProdStock = ProdStock - 1  WHERE ProdID_PK=" & Nz(Me.CboProduct.Column(0), 0) & ""
                
            End If
            
        End If
    
    DAO.DBEngine.CommitTrans
    
    End If
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Check the subform recordset count?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    ezybusy is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2015
    Posts
    119
    Thank you,
    how do i do that ?

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by ezybusy View Post
    Thank you,
    how do i do that ?
    You start with a Google.

    https://www.google.com/search?q=chec...hrome&ie=UTF-8

    Try the first link.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    ezybusy is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2015
    Posts
    119
    Hello,
    i have tried searching from Google using the link you provided, yet i can't get the right way to do what i have in mind. Maybe i did not explain myself enough. Please below are more details about what i am trying to achieve :

    Loop through all the records on the subform, then if the specific Product_ID already exist, then do UPDATE stuffs, else do INSERT stuffs. However, as i said earlier on i prefer do to do all the checkings at form level if possible, not involving checking through tables.

    Do you think it can be done ? If yes, can you kindly give me a starting guideline as to how the code should look like ?.

    Thank you in advance.

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You need to use the subform's RecordsetClone FindFIrst method to attempt to find the desired product id; if the recordset's NoMatch property returns true then you insert else you update.

    https://learn.microsoft.com/en-us/of...recordsetclone

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 9
    Last Post: 09-12-2018, 01:20 PM
  2. Replies: 1
    Last Post: 05-17-2018, 03:59 PM
  3. Replies: 3
    Last Post: 11-26-2013, 03:40 PM
  4. Replies: 5
    Last Post: 10-22-2013, 07:37 AM
  5. SubForm will not update after an insert
    By kowalski in forum Forms
    Replies: 11
    Last Post: 08-16-2013, 06:49 PM

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