Pass data from one form to another

Louise Wallen

Registered User.
Local time
Today, 10:46
Joined
Jun 20, 2017
Messages
17
Hello,

I am a beginner so if anyone is able to help I was hoping for a clear explanation please :-)

I have two forms and a button

[1.] frm_view_project (includes a project ID, project name and some details, and a button: [btn_add_comment])

When I click this button it loads:
[2.] frm_add_edit_comment

When I choose to add a comment by clicking [btn_add_comment] I would like it to auto populate a combo box I have in this form called [cmb_select_project] (this is storing the project ID).

Please can someone explain to me how I can pass the [project_ID] stored in form 1 to populate the [project_ID] stored in the frm_add_edit_comment.

Thanks so much in advance!
 
Hello RuralGuy,

Firstly, thank you so much for your quick reply - I've never used this forum before and I was so shocked at how fast your message came in!
I'm sorry to sound ungrateful, but I have looked at your example... and I don't understand :-(
I'm way too beginner at the moment to see how I can switch out that example with my own names. I did have a go at it but it didn't work.

If you are able to show me a real example using my query this would really help me get my head around it! I'm hoping to apply the same logic to many other forms and should be able to replicate it once I see a clearer example. At the moment I have only used wizards like the macro builder so when I see me.example, I don't understand what parts to leave in and leave out...

Please help if you can.
kind regards,
 
I completely understand but frankly I don't use macros, only code so I may be of limited help here. If you would like to learn how to do it with code, I would be glad to help.
 
If you could post a clear instruction then I would be happy to paste in code. Like
1. open first form , F7 to open code, paste this:
2. open second from, F7, paste this:
Something like that, easy to follow, I will be glad to work with! Then I can use this example for the rest of the database.
 
Okay, let's give it a shot! First create the [btn_add_comment] button again but select code rather than macro. When it is created, post the code and I'll show you how to modify it.
 
What version of Access are you using? Does it let you create code?
 
Hello,

I've added the button but cannot see how to open the code for this. I'll see if I can read up on the help guide again.

Thanks,
 
It's the middle of the night on Colorado, so I guess RuralGuy is either partying or sleeping (it's always useful to include your location in your Profile, Louise)
In form design view, select the button. Make sure you have the Property Sheet displayed. Click into the On Click field and you will get two buttons - a down arrow and one with 3 dots. Click on the three dots and then choose "Code Builder".
 
Hi, thanks for that. I'm in the UK and don't mind waiting for a response - I'm just thankful people are!
 
Welcome back. Can you post the code the wizard created for us?
 
Hello, I only see this in there...

Private Sub btn_add_comment_Click()

End Sub
 
Okay, we'll fill in something then. Hang on a second.
 
Let's give this a try for starters:
Code:
Private Sub btn_add_comment_Click()
On Error GoTo Err_cmdOpen_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "cmb_select_project"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdOpen_Click:
    Exit Sub

Err_cmdOpen_Click:
    MsgBox Err.Description
    Resume Exit_cmdOpen_Click

End Sub
 
It is not neat but it should do the same thing as the existing button, but no more.
 
That didn't work, it says the form name is spelt wrong...
Just to check, this code is being applied to the button in the first form?
It needs to open the form frm_add_edit_comment
 
Oops...yes it is on the first form. Okay, just change this line:

stDocName = "cmb_select_project"

to...

stDocName = "frm_add_edit_comment"
 
Yay that worked! So far so good, so that opens the form. Now I just need to set the combo box in the second form to the project_ID that was stored in the original form. thanks so much for your help.
 
Okay, lets pass the project_ID to the 2nd form in the OpenArgs argument.

Change this line:

DoCmd.OpenForm stDocName, , , stLinkCriteria

yo...

DoCmd.OpenForm stDocName, , , stLinkCriteria, , , Me.project_ID

This assumes the value is in a control named [project_ID]
 

Users who are viewing this thread

Back
Top Bottom