Need help with VBA/Form

  • Thread starter Thread starter Amit
  • Start date Start date
A

Amit

Hi,

I need some help with VBA code for a form.

I have a form (frmOrg) to enter Organization information.
I also have another form (frmRep) to enter Organization
Representative information. I would like to be able to
open frmRep using a button on "frmOrg", with the
Organization name field on "frmRep" be already filled in
with the value of the Organization name from the "frmOrg".
I'm able to add a button to open "frmRep", but not sure
what code to write so that the Organization name is taken
from "frmOrg". Organization ID is the PK, there is a 1-to-
many relationship between Organization table and
Representative table. I'd like to open "frmRep" at a new
record, but also be able to display existing records.

Do I need to refresh the "frmOrg" before opening
the "frmRep" form?

Will appreciate any help.

Thanks!

-Amit
 
If you have made any changes to the current record in frmOrg, and especially
if it's a new record, it's a good idea to save your changes before you open
frmRep. Refreshing is one way; another is this code:
Me.Dirty=False
If (as you should) you have defined referential integrity between your two
tables, you will HAVE to save a new Organization record before you can add a
Representative for it.

That said - here's one way to do it:
Behind the button on frmOrg, use code like this:
DoCmd.OpenForm "frmRep",,,,acFormAdd,,Me![Organization ID]
This will open frmRep to a new record, and pass it the value of
[Organization ID].
If you have a textbox named txtID on frmRep, you can display the value you
just passed in like this in the Form_Load event procedure for frmRep:
txtID=OpenArgs

HTH
- Turtle
 
Hi Turtle,

Thanks!! That was helpful. One more question- "acFormAdd"
opens the form at a new record. Is there a way to open the
form to a new record AND be able to show the existing
records??

-Amit
-----Original Message-----
If you have made any changes to the current record in frmOrg, and especially
if it's a new record, it's a good idea to save your changes before you open
frmRep. Refreshing is one way; another is this code:
Me.Dirty=False
If (as you should) you have defined referential integrity between your two
tables, you will HAVE to save a new Organization record before you can add a
Representative for it.

That said - here's one way to do it:
Behind the button on frmOrg, use code like this:
DoCmd.OpenForm "frmRep",,,,acFormAdd,,Me! [Organization ID]
This will open frmRep to a new record, and pass it the value of
[Organization ID].
If you have a textbox named txtID on frmRep, you can display the value you
just passed in like this in the Form_Load event procedure for frmRep:
txtID=OpenArgs

HTH
- Turtle

Amit said:
Hi,

I need some help with VBA code for a form.

I have a form (frmOrg) to enter Organization information.
I also have another form (frmRep) to enter Organization
Representative information. I would like to be able to
open frmRep using a button on "frmOrg", with the
Organization name field on "frmRep" be already filled in
with the value of the Organization name from the "frmOrg".
I'm able to add a button to open "frmRep", but not sure
what code to write so that the Organization name is taken
from "frmOrg". Organization ID is the PK, there is a 1- to-
many relationship between Organization table and
Representative table. I'd like to open "frmRep" at a new
record, but also be able to display existing records.

Do I need to refresh the "frmOrg" before opening
the "frmRep" form?

Will appreciate any help.

Thanks!

-Amit


.
 
Yes.
Remove the acFormAdd (but leave the corresponding commas) from
DoCmd.OpenForm.

In the Form_Load event procedure for frmRep, use code like this:
If OpenArgs<>"" Then DoCmd.GoToRecord ,,acNewRec

Put this code before the one where you assign a value to txtID.
If you're wondering why I suggest testing for a blank OpenArgs, that's so
that you'll be able to open this form at other times without automatically
going to a new record.

HTH
- Turtle

Amit said:
Hi Turtle,

Thanks!! That was helpful. One more question- "acFormAdd"
opens the form at a new record. Is there a way to open the
form to a new record AND be able to show the existing
records??

-Amit
-----Original Message-----
If you have made any changes to the current record in frmOrg, and especially
if it's a new record, it's a good idea to save your changes before you open
frmRep. Refreshing is one way; another is this code:
Me.Dirty=False
If (as you should) you have defined referential integrity between your two
tables, you will HAVE to save a new Organization record before you can add a
Representative for it.

That said - here's one way to do it:
Behind the button on frmOrg, use code like this:
DoCmd.OpenForm "frmRep",,,,acFormAdd,,Me! [Organization ID]
This will open frmRep to a new record, and pass it the value of
[Organization ID].
If you have a textbox named txtID on frmRep, you can display the value you
just passed in like this in the Form_Load event procedure for frmRep:
txtID=OpenArgs

HTH
- Turtle

Amit said:
Hi,

I need some help with VBA code for a form.

I have a form (frmOrg) to enter Organization information.
I also have another form (frmRep) to enter Organization
Representative information. I would like to be able to
open frmRep using a button on "frmOrg", with the
Organization name field on "frmRep" be already filled in
with the value of the Organization name from the "frmOrg".
I'm able to add a button to open "frmRep", but not sure
what code to write so that the Organization name is taken
from "frmOrg". Organization ID is the PK, there is a 1- to-
many relationship between Organization table and
Representative table. I'd like to open "frmRep" at a new
record, but also be able to display existing records.

Do I need to refresh the "frmOrg" before opening
the "frmRep" form?

Will appreciate any help.

Thanks!

-Amit


.
 
Back
Top