Results 1 to 5 of 5
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250

    Check if a form is being opened in "Add" or "Edit" mode

    Hello,

    I have a data entry form which can be opened in 2 ways:

    A) to add a new record

    Code:
    Private Sub cmdAddNew_Click()
    
        DoCmd.OpenForm "frmDE_Suppliers", , , , acFormAdd
        
    End Sub
    B) edit an existing record

    Code:
    Private Sub cmdEdit_Click()
    
        DoCmd.OpenForm "frmDE_Suppliers", , , "SupplierID=" & Me.SupplierID, acFormEdit
    
    End Sub

    I want to check what mode the form is being opened in, and do stuff based on this mode. This is what I have so far:

    Code:
    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo ErrHandler
    
        If Auth(TempVars("EmployeeID"), "Suppliers", "View") = False Then
            Cancel = True
            Exit Sub
        End If
        
        If Me.NewRecord = False Then
            If Auth(TempVars("EmployeeID"), "Suppliers", "Edit") = False Then
                Cancel = True
                Exit Sub
            End If
        Else
            If Auth(TempVars("EmployeeID"), "Suppliers", "Add") = False Then
                Cancel = True
                Exit Sub
            End If
        End If
        
    Exit Sub
    ErrHandler:
    
        Select Case Err.Number
        
            Case 2501
                Resume Next
                
        End Select
            
    End Sub
    But for some reason, "Me.NewRecord" returns False in both cases. Can you please help me fix this? Should I use something different than Me.NewRecord?

    Thank you,


    Tomas

  2. #2
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I don't think in the form open event the new record property would be set yet.
    Can you not check the form's mode instead? (I don't know if this works, and can't easily test at the moment)

    If Me.DataEntry ....
    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 ↓↓

  3. #3
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250
    Superb!

    Changing Me.NewRecord to Me.DataEntry solved my issue.

    Thanks a bunch!

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I would have just passed in the mode as OpenArgs?
    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

  5. #5
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250
    Thanks, but I stand by: If it ain't broke, don't fix it

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

Similar Threads

  1. Simple table relationships ("faces" to "spaces" to "chairs")
    By skydivetom in forum Database Design
    Replies: 36
    Last Post: 07-20-2019, 01:49 PM
  2. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  3. Replies: 1
    Last Post: 07-10-2015, 06:33 AM
  4. Replies: 4
    Last Post: 05-16-2015, 10:59 AM
  5. Replies: 4
    Last Post: 04-08-2014, 09:36 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