Results 1 to 3 of 3
  1. #1
    Ganymede is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    102

    Opening Form to Specific Record Issue


    I'm attempting to use a DoCmd.OpenForm code to open a form to a specific record. However, the form that I'm attempting to open has an On Open event that automatically puts the form to a new record when opened.

    Code:
    DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
    As a result, the DoCmd.OpenForm doesn't work properly. Basically, the form goes to a new record instead of the specific record I want it to go to.

    I could fix this problem by removing the OnOpen event. But I have that OnOpen event there for a reason. Most of the time, when users are opening this form, they are looking to add a new record. I have the OnOpen event to save them the trouble of hitting "add record." But occasionally the user will be opening this form to access a specific record. That is why I'm trying to write this DoCmd.OpenForm code.

    Any ideas what I can do to work around this problem?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    See this for info re DoCmd.OpenForm method

    and this youtube video

    Review your logic. Make sure what you are using(Access-wise) addresses the issue you are trying to resolve.

    Don't overlook google to help you find info.

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Don't know how, exactly, you're currently telling the secondary Form which Record to go to, but if you use the OpenArgs parameter of the OpenForm command to do this

    Code:
    DoCmd.OpenForm "SecondaryFormName", , , , , , Me.RecordID
    you can then check, in the secondary Form, to see if OpenArgs is populated, or not. If it is populated, you go to the appropriate Record; if it isn't populated, you go to a New Record.

    Notice that I use the Form_Load event, rather than the Form_Open event; this is always preferable when dealing with Record data, as the data is often not available in the Form_Open event.
    Code:
    Private Sub Form_Load()
    
    Dim rst As DAO.Recordset
    
    If Not IsNull(Me.OpenArgs) Then
     
     Set rst = Me.RecordsetClone
     
     rst.FindFirst "[RecordID] = '" & Me.OpenArgs & "'"
     'rst.FindFirst "[RecordID] = " & Me.OpenArgs ' Use this for a Numeric ID
      
      If Not rst.NoMatch Then
          Me.Bookmark = rst.Bookmark
       Else
        DoCmd.GoToRecord , , acNewRec
       End If
    
    rst.Close
    
    Set rst = Nothing
    
    End If
    
    End Sub

    The rst.FindFirst "[RecordID] line in black is the correct syntax if the unique identifying Field is Text.

    If the unique identifying Field (RecordID, in the example) is Numeric, you'd use the line in red, instead.

    Linq ;0)>

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

Similar Threads

  1. Replies: 5
    Last Post: 01-24-2015, 12:59 AM
  2. Replies: 2
    Last Post: 07-08-2014, 11:15 AM
  3. Replies: 4
    Last Post: 12-13-2013, 04:50 PM
  4. Opening Form to a Specific Record
    By PPat in forum Forms
    Replies: 9
    Last Post: 04-24-2013, 08:47 PM
  5. Help with opening form to specific record
    By manic in forum Programming
    Replies: 7
    Last Post: 09-18-2012, 08:44 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