Results 1 to 11 of 11
  1. #1
    dbhokie is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    12

    Saving a Parent form, while a relational subform is open on the Parent form.

    (The record cannot be deleted or changed because 'tblinvoice' includes related records.

    I have a form "Transactions", It has a sub-form "Invoices".

    Originally I locked all the fields on transactions so the user could enter in invoices in a datasheet view in the "invoices' sub-form and attach them to the current transaction displayed above in the Transaction form. Then they wanted a lock unlock button in case they wanted to edit the transaction data in the Transaction form so I entered in:



    So....
    I did:

    Code:
    Private Sub cmdLockUnlock_Click()
    
    
       Dim ctrl As Control
        Dim strControl As String
        
        strControl = Me.ActiveControl.Name
        
        On Error Resume Next
        For Each ctrl In Me.Controls
            With ctrl
                If .Name <> strControl Then
                    .Locked = Not .Locked
                End If
            End With
        Next ctrl
    End Sub
    Now what is going on is the following. If you make the form "dirty" then try to close out:





    I can't think of how to deal with this. Do I need to save the subform, then the form with an if dirty?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,969
    Makes no sense to me to lock controls then give user ability to unlock.

    If the controls are not locked in design so this code is not necessary, is there an error when editing transaction?
    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
    dbhokie is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    12
    So when the open the form, the Parent form "Transactions" is locked to prevent them accidentally changing the record while adding invoices to the associated "transaction". (It is a one to many relationship). They requested an unlock button in case while adding invoices they notice there is an error in the transaction. This allows them to unlock it and edit the transaction data on that form but only if they purposefully do so by unlocking it (by clicking the button). All of the controls are locked when the form opens, this code "unlocks" them. I did manage to do that for them, but it won't work to save a record because the invoice subform is open on the same form and bound relationally to the transactions table:

    Click image for larger version. 

Name:	transactionsformexample.jpg 
Views:	19 
Size:	90.9 KB 
ID:	46953

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,969
    I don't understand how locking/unlocking controls would interfere with saving data.

    Didn't answer my question.
    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
    dbhokie is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    12
    Well June, you helped me realize what the problem was even without me asking the proper question.
    I was having an error when I tried to close the form if I had so much as typed in "a" field on the main form:
    The error was:
    The record cannot be deleted or changed because 'tblinvoice' includes related records.
    Well I just tried to re-create it and couldn't. That was because the field named "Contract Number" is the field on the "Transactions" table that links the "Invoice" table (which the subform is based off of).
    I am an idiot, naturally "THAT FIELD" cannot be changed because it is the link to the below records that are open. (I don't want that field to be changed anyways), I think I need to change my code to not unlock that particular field does not unlock but the rest do.

    In summary..when I was getting the error was when I typed anything into the related field which it can't change due to it being the many to one relationship field between that table and the subform's table.

    To prevent this error from happening by user mistake (not my mistake) I just need to always keep that field locked.

  6. #6
    dbhokie is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    12
    Quote Originally Posted by June7 View Post
    I don't understand how locking/unlocking controls would interfere with saving data.


    Didn't answer my question.
    Seems I can be sure that if June7 sees my question (second time now), it is a stupid error on my part. I apologize. (albeit it still would have been a dumb error on my part without you seeing it) I don't know when trying to test it why I was typing and deleting in the linked field. Ugh

  7. #7
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,573
    Looks like you are using the Contract Number as the Link Parent/Link Child value ??
    This looks wrong to me.
    I agree with June. It is pointless to have a Look/Unlock button.
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  8. #8
    dbhokie is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    12
    Quote Originally Posted by mike60smart View Post
    Looks like you are using the Contract Number as the Link Parent/Link Child value ??
    This looks wrong to me.
    I agree with June. It is pointless to have a Look/Unlock button.
    It is, and that was the problem when unlocking I typed into that field like an idiot.
    It works, are you saying better practice may be to have a button to open the related transaction in a popup so they can fix whatever it is they see when entering invoices? If the user enters the data right the first time then they don't really need the unlock button, but they said while they enter an invoice (tied to the contract number) they may see where they entered something erroneously on the original transaction and wish to fix it.

  9. #9
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    There is no problem in editing the parent data at all on the same form, with the exception of the main PK linking field.
    So your solution is fine just keep the linked ID field locked at all times.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  10. #10
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,573
    On the form shown you have an AssetID which I am assuming is a PrimaryKey?

    Do you then have a Subform for displaying the Rental Detals?

    Is this Rental Subform using AssetID as the Foreign Key ?
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,969
    Not using autonumber field as PK?
    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.

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

Similar Threads

  1. Replies: 6
    Last Post: 01-14-2021, 04:54 PM
  2. Replies: 20
    Last Post: 05-13-2020, 02:49 PM
  3. Replies: 3
    Last Post: 07-03-2013, 01:20 PM
  4. Problem using Parent/Child Form/Subform
    By EvanRosenlieb in forum Forms
    Replies: 4
    Last Post: 06-27-2011, 05:25 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