Passing Values Between Open Forms

R

Ryan

I have created an inventory system in MS Access. On the main form (Form A),
I have a button that allows the user to assign another PC unit to the
current person. When the button is clicked, another form pops up (Form B)
that shows a list of all available PC units. The user can then select which
of the available units he/she wants and click a button. I want this to then
populate Form A with the unit selected in Form B. My problem is that I
don't know how to make the forms interact with eachother.

Thanks for any assistance,
Ryan
 
D

Dirk Goldgar

Ryan said:
I have created an inventory system in MS Access. On the main form
(Form A), I have a button that allows the user to assign another PC
unit to the current person. When the button is clicked, another form
pops up (Form B) that shows a list of all available PC units. The
user can then select which of the available units he/she wants and
click a button. I want this to then populate Form A with the unit
selected in Form B. My problem is that I don't know how to make the
forms interact with eachother.

The basic syntax is

Form!NameOfControlOnFormA = Me!NameOfControlOnFormB

You may want to check first to make sure that FormA is loaded -- in
Access 2000 or later, you can use

If CurrentProject.AllForms("FormA").IsLoaded Then

' ...

End If
 
R

Ryan

This is not working. I'm getting an error that says "object required". The
other form is most definitely loaded. Here is my code.

Dim stName As String
Dim stDept As String
Dim stDivision As String

stName = frmMain!txtIssuedTo
stDept = frmMain!cmbDept
stDivision = frmMain!cmbDivision
 
R

Ryan

The Syntax required is
stName = Forms!frmMain!txtIssuedTo

On to the next problem.
In the code for FormB, I'm trying to have FormA look up the record that I am
wanting to manipulate. Here is my code:


Forms!frmMain DoCmd.FindRecord stSerial,,,,,,

NOTE:
(Form A = frmMain)

I'm getting a compile error: "Expected ="
 
D

Dirk Goldgar

Ryan said:
This is not working. I'm getting an error that says "object
required". The other form is most definitely loaded. Here is my
code.

Dim stName As String
Dim stDept As String
Dim stDivision As String

stName = frmMain!txtIssuedTo
stDept = frmMain!cmbDept
stDivision = frmMain!cmbDivision

Oops. That should have been

Forms!NameOfForm!NameOfControlOnFormA = _
Me!NameOfControlOnFormB

Sorry.
 
D

Dirk Goldgar

Ryan said:
The Syntax required is
stName = Forms!frmMain!txtIssuedTo

Sorry. I was typing too fast.
On to the next problem.
In the code for FormB, I'm trying to have FormA look up the record
that I am wanting to manipulate. Here is my code:


Forms!frmMain DoCmd.FindRecord stSerial,,,,,,

NOTE:
(Form A = frmMain)

I'm getting a compile error: "Expected ="

FindRecord only works on the active form or datasheet, so at best you
have to do something like this:

Forms!frmMain.SetFocus
DoCmd.FindRecord stSerial

But I find it's much more reliable if I conduct my own search, using
code along these lines:

With Forms!frmMain.RecordsetClone
.FindFirst "Serial='" & stSerial
If .NoMatch Then
MsgBox "Couldn't find the serial number you entered. " & _
"Please try again."
Me!txtSerial.SetFocus
Else
Forms!frmMain.Bookmark = .Bookmark
End If
End With

Now, that's not as comprehensive as FindRecord. Among other things,
it's just searching a particular field, and it's only going to find the
first matching record (if there are more than one). But when that's all
you want, it's more reliable than FindRecord.
 
R

Ryan

Hi Dirk,

Thanks for your help. What I ended up doing is calling a public subroutine
of FormA in the code for FormB (event - buttonCmd pressed) and passing the
values from FormB to this function. This is working great.

Now the only thing I have left to do is close FormB in my code, which I'm
not having any luck with yet.
I tried using "Close" within the FormB code (before calling the subroutine)
as well as
Forms!frmB.Close from within FormA code
Neither of these work.

Ryan
 
D

Dirk Goldgar

Ryan said:
Hi Dirk,

Thanks for your help. What I ended up doing is calling a public
subroutine of FormA in the code for FormB (event - buttonCmd pressed)
and passing the values from FormB to this function. This is working
great.

Now the only thing I have left to do is close FormB in my code, which
I'm not having any luck with yet.
I tried using "Close" within the FormB code (before calling the
subroutine) as well as
Forms!frmB.Close from within FormA code
Neither of these work.

You need to learn more about the Access object model and how to carry
out Access actions in code. Access forms don't have a Close method.
The help file is far from perfect, but it can help you with this. For
example, if you search for "close form", you are pointed to the help
topic for the Close macro action, which isn't actually what you're
looking for, but which says at the bottom, "To run the Close action in
Microsoft Visual Basic, use the Close method of the DoCmd object."

The DoCmd object provides methods for carrying out most of the actions
that can be performed from the user interface. In this case, you would
write something like

DoCmd.Close acForm, "FormB"

If the code is to be executed in the module of FormB itself, you could
use

DoCmd.Close acForm, Me.Name
 
R

Ryan

Thanks Dirk,

This solved the problem.

Dirk Goldgar said:
You need to learn more about the Access object model and how to carry
out Access actions in code. Access forms don't have a Close method.
The help file is far from perfect, but it can help you with this. For
example, if you search for "close form", you are pointed to the help
topic for the Close macro action, which isn't actually what you're
looking for, but which says at the bottom, "To run the Close action in
Microsoft Visual Basic, use the Close method of the DoCmd object."

The DoCmd object provides methods for carrying out most of the actions
that can be performed from the user interface. In this case, you would
write something like

DoCmd.Close acForm, "FormB"

If the code is to be executed in the module of FormB itself, you could
use

DoCmd.Close acForm, Me.Name

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Top