Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183

    Button to open form and link IDs


    So I want to create a pop-up form where my user clicks a button, it opens the form and it's linked to the record they're currently on.

    My code is opening the form but it's not linking the two records.

    For example, let's say I have a "notes" table. I want all notes to be linked to the specific record. I could just create a subform but it's not as asthetic as I want the form to be. So, I wanted to open up a beautiful form to enter notes.

    In the popup form, the for has these controls : ContactID, Subject, Message

    Here is the code I tried in the button click:

    Code:
    Private Sub Notes_Click()
    
    CurRec = Me.ID.Value
    
    
    DoCmd.OpenForm "SMS Activity", , , "ContactID =" & CurRec
    
    
    End Sub
    I know I'm an idiot so I won't feel bad if you laugh at my own code. LOL. Relearning VBA

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Nothing is glaringly wrong with this. The issue is either in the value you are passing - did you check that the value is correct prior to opening the form? Or else that field doesn't not exist on the form you are trying to open, something in the record source? Or there is an OnLoad event that is changing it?

  3. #3
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Quote Originally Posted by aytee111 View Post
    Nothing is glaringly wrong with this. The issue is either in the value you are passing - did you check that the value is correct prior to opening the form? Or else that field doesn't not exist on the form you are trying to open, something in the record source? Or there is an OnLoad event that is changing it?
    No, this is why I'm baffled. It looks like it should be passing record ID of the form I'm currently on to the pop up form.

    Wonder if I can do it directly without creating the variable. I.E. : DoCmd.OpenForm "SMS Activity",,,"ContactID=" & Me.ID

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Yes, you don't need the middle-man there. Do you know how to debug code? Put a stop on the openform and see exactly what value you are passing.

  5. #5
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Quote Originally Posted by aytee111 View Post
    Yes, you don't need the middle-man there. Do you know how to debug code? Put a stop on the openform and see exactly what value you are passing.
    I just know that if there is an error it will open the debugger and highlight errors in yellow. Lol.


    Sent from my iPhone using Tapatalk

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Put a stop on the openform line - click on the margin to the left of the line and it will put a dot there. Then when the code executes it will stop there and wait for you. At that point, hold your mouse over the ID and it will show you what value is in that field. Then either stop the code, let it continue (f5) or go line by line (f8).

  7. #7
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Quote Originally Posted by aytee111 View Post
    Put a stop on the openform line - click on the margin to the left of the line and it will put a dot there. Then when the code executes it will stop there and wait for you. At that point, hold your mouse over the ID and it will show you what value is in that field. Then either stop the code, let it continue (f5) or go line by line (f8).
    Okay, first - Thanks for showing me how to do that.

    Second, I have this line of code now in the OnClick event:

    DoCmd.OpenForm "SMS Activity",,,"ContactID=" & Me.ID

    Using the Stop, I hover over the Me.ID and it DOES give me the appropriate ID of the record I'm on when I click the button. It does NOT enter that ID into the pop-up form in the ContactID control.

    Maybe I need to make it "ContactID.value=" ??

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    Are you talking about/do any of these scenarios ever occur:

    1. Being on an existing Record, in the primary Form, and going to an existing Record in the secondary Form?
    2. Being on an existing Record, in the primary Form, and going the secondary Form that doesn't currently have an existing, matching Record?
    3. Being on a New Record, in the primary Form, and going to a New Record in the secondary Form?

    Also, is ContactID defined as a Number Datatype or as a Text Datatype?

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  9. #9
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    What you have discovered so far - you are opening a form with a filter and the filter is correct. So now move on to the next step - the actual opening of the form, that is where the problem lies. Either in the record source, that that field does not exist, or something else is going on as that form opens.

  10. #10
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Maybe my issue is that I'm expecting it to open the form with the ContactID already entered? Will it update after the record is saved so I won't physically see the ID updated until after the record is saved??

  11. #11
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Quote Originally Posted by Missinglinq View Post
    Are you talking about/do any of these scenarios ever occur:

    1. Being on an existing Record, in the primary Form, and going to an existing Record in the secondary Form?
    2. Being on an existing Record, in the primary Form, and going the secondary Form that doesn't currently have an existing, matching Record?
    3. Being on a New Record, in the primary Form, and going to a New Record in the secondary Form?

    Also, is ContactID defined as a Number Datatype or as a Text Datatype?

    Linq ;0)>
    Option 2 is what I'm doing.

    It's a contact form. Then there is a button on the contact form that opens the pop-up form that has three fields.
    ContactID
    Subject
    Message

    Each entry will be a new record for that contact. The pop-up will ONLY create new entries.

  12. #12
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Filters only work on existing records.

  13. #13
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Quote Originally Posted by dniezby View Post
    Maybe my issue is that I'm expecting it to open the form with the ContactID already entered? Will it update after the record is saved so I won't physically see the ID updated until after the record is saved??
    Nope, this wasn't the issue. LOL. It just assigned a 0 to every ContactID

    Quote Originally Posted by aytee111
    What you have discovered so far - you are opening a form with a filter and the filter is correct. So now move on to the next step - the actual opening of the form, that is where the problem lies. Either in the record source, that that field does not exist, or something else is going on as that form opens.
    I have no code at all in this pop-up form. So I don't know what's happening to the variable as it's passed to the opening form.

  14. #14
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    If you have no code when the form opens, how are you ensuring that only new records are added?

  15. #15
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    For Option #2 here's an example...I believe I've got your names correct:

    In the Primary Form:

    Code:
    Private Sub Notes_Click()
    
     If Nz(Me.ID,"") <> "" Then
      DoCmd.OpenForm "SMS Activity", , , , , , Me.ID
     Else
      MsgBox "The ID Field Must Be Entered First!"
     End If
    
    End Sub


    In the Secondary Form:

    Code:
    Private Sub Form_Load()
    
        DoCmd.GoToRecord , , acNewRec
        
        If Nz(Me.OpenArgs,"") <> "" Then
          Me.ContactID = Me.OpenArgs
        End If
    
    End Sub

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-25-2016, 04:09 PM
  2. Open a form based on a link in Subreport
    By RapidRepairArnold in forum Reports
    Replies: 6
    Last Post: 12-20-2012, 03:55 PM
  3. Open Form and Link To Specific Record
    By burrina in forum Forms
    Replies: 2
    Last Post: 12-17-2012, 11:09 PM
  4. Replies: 4
    Last Post: 12-21-2010, 11:32 AM
  5. Create an RDP link button on my form
    By BDP in forum Access
    Replies: 1
    Last Post: 09-14-2010, 02:49 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