Carry value of variable from one Form to another

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is probably a simple question, but how do I keep the defined value for a
variable and use it in another form?

I have a variable named 'PartNoToAdd' that is set in one form when the given
'PartNo' is not in the 'Inventory' Table. (Set PartNoToAdd = Me.PartNo) I
then open a form that asks if the user would like to add the part to
inventory with two buttons 'Yes' and 'No'. The 'Yes' button opens a 'AddInv'
Form where there is a combo box that I want to auto-fill with the given
'PartNoToAdd'. I can get it to set the variable correctly, but when I call
it again in the 'AddInv' Form, it is empty.

I've tried declaring it in the 'general declarations' and tried setting it
with both 'Public' and 'Static' according to the help files, but it always
come up empty.

Any ideas?

Thanks in advance for any help!

Parker
 
Parker said:
This is probably a simple question, but how do I keep the defined value for a
variable and use it in another form?

I have a variable named 'PartNoToAdd' that is set in one form when the given
'PartNo' is not in the 'Inventory' Table. (Set PartNoToAdd = Me.PartNo) I
then open a form that asks if the user would like to add the part to
inventory with two buttons 'Yes' and 'No'. The 'Yes' button opens a 'AddInv'
Form where there is a combo box that I want to auto-fill with the given
'PartNoToAdd'. I can get it to set the variable correctly, but when I call
it again in the 'AddInv' Form, it is empty.

I've tried declaring it in the 'general declarations' and tried setting it
with both 'Public' and 'Static' according to the help files, but it always
come up empty.


You'd better post the relevant code from both forms. I
can't quite make out what you're doing, but your use of SET
.... leads me to think you're doing something different than
what I understood your words to mean.
 
Ok, sorry about the confusion, here are the basics:
'TblInventory' Table - PartNo (Text), Description (Text), UnitOfMeasure
(Text).
'TblPurchases' Table - PONo (Text), Vendor (Text), Date (Date/Time),
PartNo (Text), QtyOrdered (Number - Double).
'QryAddPurchase' - SELECT TblPurchases.PONo, TblPurchases.Vendor,
TblPurchases.Date, TblPurchases.PartNo, TblInventory.Description,
TblInventory.UnitOfMeasure, TblPurchases.QtyOrdered
FROM TblInventory RIGHT JOIN TblPurchases ON TblInventory.PartNo =
TblPurchases.PartNo
WHERE (((TblPurchases.PONo)=[Forms]![FrmAddPurchase].[Combo12]));

'FrmAddPurchase' Form - (Based on 'QryAddPurchase')
Since it looks up the description
Description On Enter:
Private Sub Description_Enter()
If IsNull(Me.Description) Then
Static PartNoToAdd As Variant
Set PartNoToAdd = Me.PartNo
DoCmd.OpenForm ("FrmAddInvFly")
End If
Me.QtyOrdered.SetFocus
End Sub

'FrmAddInvFly' Form is just a simple switchboard with two buttons ('Yes'
and 'No'). On click of 'Yes', opens 'FrmAddInv'

'FrmAddInv' On Open:
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(PartNoToAdd) Then
Me.Combo24 = PartNoToAdd
Else
Me.Combo24 = Null
End If
DoCmd.GoToRecord , , acNewRec

I threw a break point here and the 'PartNoToAdd' is empty.
I just want the PartNo given in the 'FrmAddPurchase' From to carry through
and be automatically entered into the 'FrmAddInv' Form so the user doesn't
have to re-enter it.

Note: The line "Static PartNoToAdd As Variant" is just my last attempt at
it, I tried 'Public' and also putting it into the 'general declarations'
section... All had the same result.

Thanks again!

Parker
 
Parker said:
Ok, sorry about the confusion, here are the basics:
'TblInventory' Table - PartNo (Text), Description (Text), UnitOfMeasure
(Text).
'TblPurchases' Table - PONo (Text), Vendor (Text), Date (Date/Time),
PartNo (Text), QtyOrdered (Number - Double).
'QryAddPurchase' - SELECT TblPurchases.PONo, TblPurchases.Vendor,
TblPurchases.Date, TblPurchases.PartNo, TblInventory.Description,
TblInventory.UnitOfMeasure, TblPurchases.QtyOrdered
FROM TblInventory RIGHT JOIN TblPurchases ON TblInventory.PartNo =
TblPurchases.PartNo
WHERE (((TblPurchases.PONo)=[Forms]![FrmAddPurchase].[Combo12]));

'FrmAddPurchase' Form - (Based on 'QryAddPurchase')
Since it looks up the description
Description On Enter:
Private Sub Description_Enter()
If IsNull(Me.Description) Then
Static PartNoToAdd As Variant
Set PartNoToAdd = Me.PartNo
DoCmd.OpenForm ("FrmAddInvFly")
End If
Me.QtyOrdered.SetFocus
End Sub

'FrmAddInvFly' Form is just a simple switchboard with two buttons ('Yes'
and 'No'). On click of 'Yes', opens 'FrmAddInv'

'FrmAddInv' On Open:
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(PartNoToAdd) Then
Me.Combo24 = PartNoToAdd
Else
Me.Combo24 = Null
End If
DoCmd.GoToRecord , , acNewRec

I threw a break point here and the 'PartNoToAdd' is empty.
I just want the PartNo given in the 'FrmAddPurchase' From to carry through
and be automatically entered into the 'FrmAddInv' Form so the user doesn't
have to re-enter it.

Note: The line "Static PartNoToAdd As Variant" is just my last attempt at
it, I tried 'Public' and also putting it into the 'general declarations'
section... All had the same result.

Since the declaration of the PartNoToAdd variable in inside
a procedure, it is not available outside the procedure (this
is called local scope). To make it available to the
remainder of the same module is thould be declared a Private
at the module level (before the first Sub or Function
statement). And to make it available outside the module,
the declaration needs to be Public at the module level.

Now, you need to be aware that there are two kinds of
modules, Class modules and Standard modules. A module level
Public variable in a standard module is available to all VBA
code in your project and you can use it as you would use any
variable.

A Public, module level variable in a class module is a
Property of the class (in your case the form's class) and
can be used by referencing the class object. Again, in your
case, Forms!theform.variablename.

A different way of achieving your objective is to just get
the value of the combo box without using a variable.

'FrmAddInv' On Open:
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(PartNoToAdd) Then
Me.Combo24 = Forms!FrmAddPurchase.Combo12
. . .
 
Back
Top