Results 1 to 8 of 8
  1. #1
    lccrews is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Location
    Los Angeles
    Posts
    194

    Updating Field 2 Based On Field 1

    Hi and thanks for any help. I am using a subform datasheet for users to update Table1. Field1 is completed first. I'd like to run an update query or SQL statement in VBA to update Field2 based on what's in Field1. I've tried using the After Update Event of Field1 on the subform but to my knowledge, a record must be committed to the database before such events can be fired. So it only works half the time.
    Is there a way to get this working differently?

    Additional Info:
    tbl_Formulas and tbl_RawMaterials are related by field [RawMaterial]. The subform datasheet the users are entering data onto is drawn from tbl_Formulas. Whenever a user enters a new [RawMaterial], I'd like the [MiscInfo] that exists on tbl_RawMaterials to automatically transfer
    to [MiscInfo] on tbl_Formulas.

    tbl_RawMaterial
    [RawMaterial] [MiscInfo]
    123 - Sugar | Domino



    tbl_Formulas
    [RawMaterial] [MiscInfo]
    123 - Sugar | (Automatically inserted)

  2. #2
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    a record must be committed to the database before such events can be fired
    Most data controls have an After Update event, which should fire after every update (NB - I seem to recall that vba updates on a control don't fire this event) so it shouldn't just work some of the time unless maybe you're updating via vba. What you are likely thinking of is the After Update event for a form. Another constraint is that field2 cannot use an expression to accomplish this, and still be bound, so not telling us what's happening when it doesn't work isn't helping to diagnose.

    As for part 2, you need to explain if your form controls are bound or not, and are you OK with doing this via code, because while DLookup can be used to get a value based on another, again, you can't bind that control to anything. The 'issue' seems to be the same as part 1 question - how to update a field based on another.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    lccrews is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Location
    Los Angeles
    Posts
    194
    Hi Micron,
    Thanks for the fast response. I was referring to the After Update Event on the subform, not the main form. I have tried this before where I use VBA in the After Update Event to check the entered value against another. I talked about it in another forum post and June7 helped me figure out why it was only working occasionally and not all the time. According to her, records must be committed to the database before being called upon in VBA.

    I'm okay with code, I'm a beginner, but I'm catching on quickly. Neither the subform nor main form are bound to anything.

    Below is the VBA I wrote for another subform After Update Event. The problem is that it only works sometimes. I believe it has to do with when Access saves your work because as I said, June7 mentioned the record needing to be committed first before code would work. Can you verify that?
    Code:
    Private Sub Weight_AfterUpdate()
    
    DoCmd.Save
    
    
    If DLookup("SumOfWeight", "qry_BP40_BlendingCheck_Pt1") > DLookup("Quantity", "qry_BP40_BlendingCheck_Pt2") Then
    Output = MsgBox("Mix exceeds quantity from formula.", vbCritical, "Error") = vbAbort
    Me.Weight.Value = "0"
    
    
    End If
    
    
    End Sub

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Why are you duplicating data between tables? Why is MiscInfo in both tables?

    You want to create new 'lookup' record on-the-fly during data entry? Use combobox NotInList event.

    DoCmd.Save does not save record, it saves form.

    DoCmd.RunCommand acCmdSaveRecord

    If the form AfterUpdate event is triggered then the record has been saved and code to save is not needed. But if you put code to save in a control's AfterUpdate, that should work.

    Record is committed to table when: 1. close table/query/form; or 2. move to another record; or 3. run code to save
    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
    lccrews is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Location
    Los Angeles
    Posts
    194
    June I have no idea how you do it but every time we converse I have an epiphany....

    I don't need this fix. I have a relationship between RawMaterial on tbl_RawMaterial and tbl_Formulas. I can just call the two tables in a query and get the information displayed that way.

    Another question is raised though. While I can delete [MiscInfo] from tbl_Formulas, tbl_RawMaterial isn't 'complete' some of the Misc Info isn't filled in yet and I rely on recipe formulators to fill in that data. So rather than writing the data in a redundant fashion, I should be trying to figure out how to fill in [MiscInfo] with what the user inputs.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Don't understand. If textbox is bound to MiscInfo then whatever is typed into textbox passes into field MiscInfo.

    What the heck is a 'recipe formulator' - a person doing data entry?
    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.

  7. #7
    lccrews is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Location
    Los Angeles
    Posts
    194
    Yes, a person doing data entry. Because there are two tables providing information I had to use a tmp table "tmp_Formula" to make changes to formulas and write to ones. Data from tmp_Formula is then transferred to respective fields on tbl_RawMaterial and tbl_Formulas. There is no text box. This is a subform datasheet. Whatever is typed into it is typed onto tmp_Formula. I have already figured out how to fix this. I'm just going to write an SQL in VBA to update tbl_RawMaterial.[MiscInfo] = tmp_Formula.[MiscInfo]. I think that will work. Thanks for the epiphany!

  8. #8
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    My whole answer was predicated on updating controls (fields) not forms. You only mentioned fields, so nothing I wrote applies to what you seem to really be doing. Seems you are OK now anyway.

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

Similar Threads

  1. Replies: 5
    Last Post: 10-04-2016, 02:05 PM
  2. Replies: 3
    Last Post: 06-24-2015, 12:57 PM
  3. Replies: 0
    Last Post: 07-03-2014, 12:15 PM
  4. Updating one date field based on another
    By barryg80 in forum Forms
    Replies: 4
    Last Post: 04-19-2013, 03:17 AM
  5. Replies: 2
    Last Post: 07-07-2011, 08:25 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