Best way to link popup form?

G

Guest

I have a main form Form1 with another form Form2 that gets opened as a popup
from a button on Form1. I want to know the best way to link these in a
one-to-one relationship, so the data in Form2 relates to the data in Form1
through a shared key (ID).

The way I've tried so far is to put a button on Form1 that opens Form2 and
filters by ID. But I don't know how to manually set Form2.ID to the same as
the current Form1.ID.

I've also thought about making Form2 a subform of Form1, but I don't know
how to open a subform as a popup through a button.

Any suggestions?
 
G

Guest

I would normally declare a varaible that would then have the current record
ID value assigned to it:

Dim lngCurID as long

lngCurID = Me.NameOfControlWithIdValue"


Then open your form with:
Docmd.openform "YourFormName",,, "NameOfIdField = " & lngCurID & ""

As for the subform idea, you can use a sub form, but sub forms are inserted
into the original form, not opened independently. You can use the Master and
Child properties of the subform to link the subform to the main form and they
will stay in sync.
 
G

Guest

Thanks for your reply.

I have what you suggested for the popup form already, where it assigns
Form1.ID to a variable, then opens Form2 with:
DoCmd.OpenForm "Form2", , , "ID=" & Var

That works to filter the records in Form2 to the one with the matching ID,
but it doesn't insert that ID into Form2.ID if such a record doesn't already
exist.

My question is if ID #12 exists in Form1 but not in Form2, I want to pop up
Form2 and have Form2.ID automatically set to 12.

Thanks,

- Joel
 
G

Guest

Well that's a little different.

In your line that opens the form you need to pass the current ID to Form 2
using the OpenArgs argument.

DoCmd.OpenForm "Form2", , , "ID=" & Var, var

then place code like the following in the On Current event of your form 2:

if me.NameOfIDContol = 0 then
Me.NameOfICcontrol = me.openargs
end if
 
G

Guest

Yes, that was my first thought. I tried the following things:

Me.ID = var
(error: You can't assign a value to this object)

Me.ID.ControlSource = var
(no error, but no effect)

Me.ID.Value = var
(error: You can't assign a value to this object)

I also tried all the above using Me![ID] instead of Me.ID, with the same
results.

What's going on here?

Thanks,
- Joel
 
G

Guest

Whoops, I got it.

I neglected to point out that I was doing this in the On Open event handler.
My guess is that when On Open is fired, the form hasn't loaded yet, so the
value can't be set the normal way.

I changed it to Me!ID.DefaultValue = var and it's working fine.

Thanks for your help.

- Joel
 
G

Guest

Well, it's a little difficult to know exactly without being able to see what
you are doing, however, it sounds like you are attempting to assign the ID
value from your form 1 to the Id value for form 2 records.

I have been assuming that form 1 has one recordset and form 2 has a
different recordset. Form 2's recordset would have a field that would serve
as a foreign Key to the records in Form 1. It is to that field or control
that I was trying to direct you to assign the value. You will never be able
to directly assign a value to the ID (an autonumber type field) field of the
active recordset, but you can assing the value from one recordset to the FK
field in the second recordset.

I hope this really helps.

Mr B


joeljkp said:
Yes, that was my first thought. I tried the following things:

Me.ID = var
(error: You can't assign a value to this object)

Me.ID.ControlSource = var
(no error, but no effect)

Me.ID.Value = var
(error: You can't assign a value to this object)

I also tried all the above using Me![ID] instead of Me.ID, with the same
results.

What's going on here?

Thanks,
- Joel


Mr B said:
Well that's a little different.

In your line that opens the form you need to pass the current ID to Form 2
using the OpenArgs argument.

DoCmd.OpenForm "Form2", , , "ID=" & Var, var

then place code like the following in the On Current event of your form 2:

if me.NameOfIDContol = 0 then
Me.NameOfICcontrol = me.openargs
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top