Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566

    Update using a Recordset

    Hi Everyone

    My VB Skills are minimal and I am trying to use some Code to Update a Specific Field in a Table.



    My Form structure is Main Form named "frmPurchaseDates"
    with a Continuous Subform named "frmPurchasedItemsSubform"

    When I run my code I get the following Error:-

    Click image for larger version. 

Name:	error.png 
Views:	22 
Size:	50.0 KB 
ID:	51203

    The Code is as follows and any help is appreciated:-

    Code:
    Private Sub cmdUpdate_Click()
    
    10        On Error GoTo cmdUpdate_Click_Error
          Dim strSQL As String
    20    If Me.Dirty Then Me.Dirty = False
          Dim rs As Object
    30    Set rs = frmPurchasedItemssubform.Form.RecordsetClone
    40    With rs
    50    Do While Not .EOF
          'Do Something
    60      strSQL = "UPDATE [tblStock] SET InvQtyOH = " & Me.NewQOH & " WHERE StockID = " & Me.StockID & ";"
    70      CurrentDb.Execute strSQL, dbFailOnError
    80    .MoveNext
    90    Loop
    100   End With
    110   Set rs = Nothing
    120   MsgBox "All Stock Updated", vbInformation
          
    130       On Error GoTo 0
    140       Exit Sub
    
    
    cmdUpdate_Click_Error:
    
    
    150       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdUpdate_Click, line " & Erl & "."
    
    
    End Sub
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If you have Option Explicit at top of header, then Debug > Compile should locate undeclared variable.

    Disable the "On Error GoTo" line then run your code. What line does it stop on?

    Best practice is to declare ALL variables at beginning of procedure.

    Regarding saving stock balances, review http://allenbrowne.com/AppInventory.html
    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
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Hi June

    It Highlights the name of the Form in Line 30

    How would I declare the Form ?
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  4. #4
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Mike, can you please review the updated code below:
    Code:
    Private Sub cmdUpdate_Click()
    
    
    10        On Error GoTo cmdUpdate_Click_Error
          Dim strSQL As String
    20    If Me.Dirty Then Me.Dirty = False
          Dim rs As DAO.Recordset 'Object
    30    Set rs = Forms!frmPurchaseDates!frmPurchasedItemssubform.Form.RecordsetClone 'Vlad assumes the subform control has the same name as its source object; your original expression would work as the form is not actually open as a stand alone form
    40    With rs
    50    Do While Not .EOF
          'Do Something
    60      strSQL = "UPDATE [tblStock] SET InvQtyOH = " & Me.NewQOH & " WHERE StockID = " & Me.StockID & ";"
    70      CurrentDb.Execute strSQL, dbFailOnError
    80    .MoveNext
    90    Loop
    100   End With
    	  rs.Close
    110   Set rs = Nothing
    120   MsgBox "All Stock Updated", vbInformation
          
    130       On Error GoTo 0
    140       Exit Sub
    
    
    
    
    cmdUpdate_Click_Error:
    
    
    150       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdUpdate_Click, line " & Erl & "."
    
    
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Do you need to add Set db = CurrentDb and maybe change to "Dim rs as Recordset" instead of object

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Why refer to a form externally if the code is in that form?
    As Vlad has pointed out, you completely missed Forms! at the start of the reference.
    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

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Where is this code located? On main form named "frmPurchaseDates"? Then if subform container control is named "frmPurchasedItemssubform", your code should work. But good practice to qualify with object name or use Me.

    Set rs = Me.frmPurchasedItemssubform.Form.RecordsetClone

    I always name subform container control differently from the object it holds, like ctrPurchasedItems, so:

    Set rs = Me.ctrPurchasedItems.Form.RecordsetClone

    If code is behind subform then simply Me.RecordsetClone.

    If this code is on neither of these forms, then need full path reference as shown by Vlad.
    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.

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Looks to me like the form reference is to the subform on the main form and the code is on the main form. In that case, a simple Me. type of reference should work?
    Late again.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Hi Vlad

    Thanks for that the Code now runs but it is not doing as expected and does not move to the next record.

    There are 2 records on the Continuous Subform with StockID's 58 & 59

    It updates StockID 58 but not 59.

    I put a Debug.Print strSQL and the Immediate Windows shows the following:-

    Code:
    UPDATE [tblStock] SET InvQtyOH = 121 WHERE StockID = 58;
    UPDATE [tblStock] SET InvQtyOH = 121 WHERE StockID = 58;
    It should update 58 to equal 121 and 59 with 10

    Any pointers to make this work ?
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  10. #10
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Show us a screen shot to see where the 121 and 10 are coming from. Your SQL statement is the problem, the Me.StockID will always refer to the current record of the form on which this code is run; you need to get the Me.StockID and Me.NewQOH in a different way if they come from different records (so might need to add a nested loop).
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Hi Vlad

    Thanks for that the Code now runs but it is not doing as expected and does not move to the next record.

    There are 2 records on the Continuous Subform with StockID's 58 & 59

    It updates StockID 58 but not 59.

    I put a Debug.Print strSQL and the Immediate Windows shows the following:-

    Code:
    UPDATE [tblStock] SET InvQtyOH = 121 WHERE StockID = 58;
    UPDATE [tblStock] SET InvQtyOH = 121 WHERE StockID = 58;
    It should update 58 to equal 121 and 59 with 10

    Any pointers to make this work ?
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  12. #12
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Duplicate post Mike, please see above!
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #13
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Hi Vlad

    On the Continuous Form I have a Combobox for selecting "Stock" with the following Row Source:-

    Code:
    SELECT tblStock.StockID, tblStock.PartTypeID, tblStock.Code, tblStock.InvQtyOH FROM tblStock ORDER BY tblStock.Code;
    There is a bound Control for "Qty"

    I then have an Unbound Control named "StockID" with a Control Source of :-
    Code:
    =[cboStock].[Column](0)
    I then have an Unbound Control named "NewQOH" with a Control Source of:-
    Code:
    =Nz([Qty],0)+Nz([InvQtyOH],0)
    which display the correct values 121 & 10
    Attached Thumbnails Attached Thumbnails Stock.png  
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Mike, you do this every time.

    I do not know how many times you need to be told.

    You process a recordset yet use the form controls, so the DATA STAYS THE SAME !!!!

    You did the same over at UA multiple times.
    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

  15. #15
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I do recall some ...
    So as you can see Mike, you need to replace Me.StockID with rs("StockID") and Me.NewQOH with Nz(rs("Qty"),0)+Nz(rs("InvQtyOH"),0) in the update SQL statement.

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

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Recordset refuses to update :)
    By ironfelix717 in forum Programming
    Replies: 7
    Last Post: 03-09-2020, 12:32 PM
  2. Recordset to update a table
    By john134 in forum Programming
    Replies: 6
    Last Post: 04-19-2017, 11:01 AM
  3. recordset won't update
    By charlieb in forum Access
    Replies: 3
    Last Post: 04-13-2015, 05:56 PM
  4. Update recordset - 'Not responding'
    By tariq1 in forum Programming
    Replies: 2
    Last Post: 08-11-2012, 11:33 AM
  5. Update Query?? or RecordSet??
    By bbrazeau in forum Queries
    Replies: 2
    Last Post: 01-13-2012, 08:44 AM

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