Results 1 to 12 of 12
  1. #1
    degras is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    72

    ...related record required...

    If I go to add a new invoice in the form, but do not enter any data (so the record is not yet saved) and then I go to enter an invoice item (in the subform) I get the following message:
    "You cannot add or change a record because a related record is required in the table 'Invoice'"

    Is there a way in VBA to save the invoice before entering the invoice items subform so this problem does not happen?

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The act of moving the focus from a MainForm to a SubForm will attempt to save the MainForm record. The same is true for the reverse: moving from the SubForm to the MainForm will attempt to save the SubForm record.

  3. #3
    degras is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    72
    Quote Originally Posted by RuralGuy View Post
    The act of moving the focus from a MainForm to a SubForm will attempt to save the MainForm record. The same is true for the reverse: moving from the SubForm to the MainForm will attempt to save the SubForm record.
    I put a "Me.Dirty = False" in the on enter event of the subform now I get the error message when running: "Me.Dirty = False" in the subform. So it's still not working correctly.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    What you added is redundant. The save *will* take place when you move the focus to the SubForm. What do you have as the PrimaryKey field of the MainForm RecordSource, and what are you using as the LinkChild/MasterFields?

  5. #5
    degras is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    72
    Quote Originally Posted by RuralGuy View Post
    What you added is redundant. The save *will* take place when you move the focus to the SubForm. What do you have as the PrimaryKey field of the MainForm RecordSource, and what are you using as the LinkChild/MasterFields?
    The following code is needed on the subform to allow focus to progress (otherwise it sticks on one of the fields on the subform) :
    Me.Dirty = false

    ...in its full context in the subforms VBA it looks like:
    Private Sub DoCalc()
    Me.Totals = Me.UnitPrice * Me.Qty
    Me.Dirty = False
    End Sub

    Private Sub Qty_Exit(Cancel As Integer)
    DoCalc
    End Sub

    Private Sub UnitPrice_Exit(Cancel As Integer)
    DoCalc
    End Sub

    The main MainForm record source is set to the Invoice table. I don't see where the PrimaryKey field is set, is this a property? If I click the record source Access asks me if I want to invoke the query builder. In the subform control the link master field is ID and the link child field is Invoice.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The PrimaryKey field is set and viewed in the table in design view. The LinkChildField is usually the Primarykey field of the RecordSource of the MainForm and a ForeignKey field of the RecordSource of the SubForm and the two fields *must* be the same FieldType. Is this true in your case?

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    It is also a good idea to name identical fields in different table the same name as a form of documentation.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I guess you have resolved this issue, right?

  9. #9
    degras is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    72
    Quote Originally Posted by RuralGuy View Post
    I guess you have resolved this issue, right?
    Not really.

    I tried setting the query to include all the fields on the main table and I did the same for the subtable setting the query to include all fields on the subtable e.g. instead of just having the record source as "invoice". It did not seem to make any difference to the tab problem I was having before.

    Note: The senario is adding a new record on the main form.

    I'm in a dilema, without the "Me.Dirty = false" command the tabbing gets stuck on one of the fields in the subtable. with "Me.Dirty = false" I get the following error if I try to enter the subform first:

    "You cannot add or change a record because a related record is required in the table 'Invoice'"

    Just to recap the code is:
    Private Sub DoCalc()
    Me.Totals = Me.UnitPrice * Me.Qty
    Me.Dirty = False
    End Sub

    Private Sub Qty_Exit(Cancel As Integer)
    DoCalc
    End Sub

    Private Sub UnitPrice_Exit(Cancel As Integer)
    DoCalc
    End Sub

    This error does not occur if I first edit any fields in the main form before entering the subform, which is a work around for the time being.

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I generally do those sort of calculations in the RecordSource query. Have you tried that?

  11. #11
    degras is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    72
    Quote Originally Posted by RuralGuy View Post
    I generally do those sort of calculations in the RecordSource query. Have you tried that?
    How do you put in the calculation?

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    In the query design grid in an empty column put:
    Total: =[UnitPrice] * [Qty]

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

Similar Threads

  1. Replies: 12
    Last Post: 09-09-2011, 11:14 AM
  2. Replies: 1
    Last Post: 07-13-2011, 11:30 AM
  3. Replies: 2
    Last Post: 05-11-2011, 02:58 PM
  4. Add Record based on related value
    By top1hat19 in forum Access
    Replies: 0
    Last Post: 03-08-2011, 12:45 PM
  5. Foce new record in related table?
    By thekruser in forum Access
    Replies: 1
    Last Post: 11-08-2010, 02:00 PM

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