Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 38

Thread: User changes in a table/form

  1. #1
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location

    User changes in a table/form

    If a User changes the data in a form/table, how can I find out excatly which row of data has been changed, and what it was changed from and to? (To will be recorded on the table, but that means that the old data will be overwritten)

    The reason I need to know this, is for use with my warehouse stock database.

    Everything works fine, as long as the user doesn't make an error, and have to correct an order. The reason for this, is that as soon as the data is input into the form, a series of calculations take place, telling the user which area of the warehouse the user should take the stock from.
    If the user then edits the amount of stock required, the database will need to reset the data, and then recalculate where to pull the stock from(as there might not be enough stock in the area originally suggested).

    The process I need to run through if the user needs to change the data is:
    1) Add the original amount of stock pulled back to each area it was pulled from.
    2) Change the status of the changed data from "Calculated" to "Requires Calculation"
    3) Run the code that is activated if an order row has the status "Requires Calculation".

    Hope this makes a little bit of sense.

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Hello again, I have VBA code for a form that logs all of the changes made to it's records and stores the log in a Table.
    This could be used by you to do what you want, however if you are really only interested in certain fields you could just store those.
    I am not sure how you can log the other changes to the Stock Location areas, do you use a query to do that or VBA that we generated on here?

  3. #3
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    I've been playing around with various solutions, and believe that all I need to know is which row is being edited. That way I can get the unique id, and change the 1/0 field accordingly.
    How do I find out the uniqueid of the row that is being changed?
    (I only need to know the id when it is being changed, NOT when it is being created for the first time).

    Do you have any code you can share for doing this?

    Edit: With regards to the stock location changes, these are stored in another table, along with the "unique id" for the row in the order that it refers to. Therefore if I know which unique id is being changed, I was thinking, I can look this id up in the stock location table, and either delete, or create a negative copy (to add the stock back in).

  4. #4
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    I assume that the UniqueId is on the form when the User inputs the Change?

  5. #5
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Basically there are 2 ways to do this, one the a Recordsetclone and the other with a Public Variable.

  6. #6
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    It isn't on the userform, but only because I didn't put it there, as it isn't necessary for the user to see. BUT it can be put there, as it is just an autonumber created when the user adds a new line to the dataform.

  7. #7
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Basically there are 2 ways to do this, one the a Recordsetclone and the other with a Public Variable.

    Go on...

  8. #8
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Which one do you want to try?
    What is the name of the Unique Key Field on the Form?

  9. #9
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    As I'm learning, I'm happy to try both ideas, and see which one works best for me.

    (Start with the easiest) .... what's a recordsetclone?

    The uniqueid, is called salesorderlineid: you should be able to see it on the relationships table in the other post, on the TblSalesOrderLine.

  10. #10
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    OK, in the form's Before Update Event Procedure enter

    [vba]Dim rs As Object
    Set rs = Me.RecordsetClone
    msgbx "For record " & me.salesorderlineid & " Old value = " & rs.fieldnamewhichhasstock & " New Value = " & me.fieldnamewhichhasstock
    rs.Close
    Set rs = Nothing
    [/vba]
    Where fieldnamewhichhasstock is the actual name of the stock value that is being changed.
    The Recordsetclone (rs) value is the one when the record is "current" and the me. value is the new one that will be written to the table.

    That should show you how it works.

  11. #11
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    [vba]m rs As Object
    Set rs = Me.RecordsetClone
    msgbox "For record " & me.salesorderlineid & " Old value = " & rs.fieldnamewhichhasstock & " New Value = " & me.fieldnamewhichhasstock
    rs.Close
    Set rs = Nothing
    [/vba]

    Tried this, and I'm getting an error in the line:
    msgbox "For record " & me.salesorderlineid & " Old value = " & rs.fieldnamewhichhasstock & " New Value = " & me.fieldnamewhichhasstock
    the me.fieldnamewhichhasstock (which should be the same as the text i've used as re.fieldnamewhichhasstock) is returning an error: "Sub or function not
    defined"


    Just to be clear, this is the actual code that I used
    [vba]Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim rs As Object
    msgbox "For record " & Me.saleorderlineid & " Old value = " & rs.saleorderlineunitrequired & " New Value = " & Me.saleorderlineunitrequired
    rs.Close
    Set rs = Nothing
    End Sub[/vba]

    I removed the " & Me.saleorderlineunitrequired" and I then got a similar error for the "Me.saleorderlineid".
    Something isn't right. :-)

    Edit: I've just noticed I was missing the line [VBA]Set rs = Me.RecordsetClone [/VBA]
    Last edited by ukdane; 12-17-2009 at 03:21 AM.

  12. #12
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    My mega post has now been deleted twice!

    I'll post in short bursts to avoid this happening for a third time.

    I changed the Msgbx to Msgbox
    so the code [VBA]MsgBox "For record " & Me.saleorderlineid[/VBA]
    this now returns the number of the line that has been changed.

    This is good.
    I now need to use this number to do two things.
    1) Table TblSaleOrderLine contains the following info:
    • saleorderlineid (this is a unique id created by an autonumber and is the number found above.)
    • saleorderid (this is the sales order id)
    • productid (this is the product id)
    • saleorderlineunitrequired (this is the number of units that are required)
    • sakeprocessed (this is a tick box it is ticked when a sereis of code/marcos have been run. This needs to be reset to not ticked/0 for the row that has been changed.
    and post....

  13. #13
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Yes (succes)
    The second thing that I need to do is:
    Table TblOrderPull contains:
    • pullid (a unique autonumber id)
    • productid (the product number)
    • areaid (the location where the goods have to be taken from)
    • saleorder id (the number of the sale order)
    • saleorderline (this is the number that needs to be located, and could occur on this table more than once)
    • unitstopull (this is the number of units that must be pulled from each location)
    I need to to one of two things, (unsure which is best)
    Either: delete each occurance of the salesorderline (this is probably more accurate with regards to other calculations)
    Or: the entire row needs to be copied, and the unitstopull number needs to be negated (so that the total product/area/unitstopull = 0) I'm not so sure this will be as accurate.


    Any clues/code/help with both these things problems please?

  14. #14
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    With regards to the first question, I came up with the following code:
    [vba]Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim myconnection As ADODB.Connection
    Dim myRecordChange As New ADODB.Recordset
    Dim myorderline As Variant
    myRecordChange.ActiveConnection = myconnection
    myorderline = Me.saleorderlineid
    myRecordChange.Open "TblSaleOrderLine", , adOpenKeyset, adLockOptimistic
    myRecordChange.MoveFirst
    While Not myRecordChange.EOF
    If myRecordChange.Fields("saleorderlineid").Value = myorderline Then
    myRecordChange.Fields("saleprocessed").Value = 0
    Else
    End If
    Wend
    myRecordChange.Close
    Set myRecordChange = Nothing
    End Sub[/vba]

    BUT, the line [vba]myRecordChange.Open "TblSaleOrderLine", , adOpenKeyset, adLockOptimistic [/vba] is causing a runtime error 3709 "The connection can not be used to perform this operation. It is either closed or invalid in this context."

  15. #15
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    [vba]Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim myRecordChange As Recordset
    Dim myorderline As Variant
    Set myRecordChange = Me.RecordsetClone
    myorderline = Me.saleorderlineid

    myRecordChange.FindFirst "saleorderlineid = " & myorderline
    If myRecordChange.NoMatch Then
    'MsgBox "record not found"
    Else
    myRecordChange.Edit
    myRecordChange.Fields("saleprocessed").Value = False
    myRecordChange.Update
    End If
    myRecordChange.Close
    Set myRecordChange = Nothing
    End Sub[/vba]

    Tried the above code, but the saleprocessed field doesn't change from ticked to unticked.
    Last edited by ukdane; 12-17-2009 at 03:43 AM.

  16. #16
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Use
    myRecordChange.Fields("saleprocessed").Value = 0

  17. #17
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Hi OBP,
    Actually I did have it set to 0 to start with.
    = false does work... but the tabel doesn't keep the change.
    the myRecordChange.update works, and the table changes,
    but the change isn't kept, when the sub ends.

  18. #18
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Still not keeping the changes.

  19. #19
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Can I somehow use this
    [VBA]
    mySQL = "UPDATE TblSaleOrderLine SET TblSaleOrderLine.saleprocessed = 0" & _
    "WHERE (((TblSaleOrderLine.saleorderlineid)= " & myorderline & "))"
    [/VBA]
    within the beforeupdate instead?

  20. #20
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Finally sussed out what was missing wrong.
    [VBA]Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim myRecordChange As Recordset
    Dim myorderline As Variant
    Set myRecordChange = Me.RecordsetClone
    myorderline = Me.saleorderlineid

    myRecordChange.FindFirst "saleorderlineid = " & myorderline
    If myRecordChange.NoMatch Then
    'MsgBox "record not found"
    Else
    myRecordChange.Edit
    myRecordChange.Fields("saleprocessed").Value = False
    myRecordChange.Update
    End If
    myRecordChange.Close
    Set myRecordChange = Nothing
    End Sub[/VBA]

    I added [VBA]Me.[saleprocessed].Value = False
    [/VBA]
    in between the else and end if.... and hey presto, it seems to work!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •