I understand the concept with the Public Vars but I do have one question. I
seem to have it working but every time I open Form2 it automatically creates
a new record and assigns an ID to it. I just want this data to be carried
over as the default for certain controls. I don't want it to generate a new
record unless I start typing data into controls on that form. Do you know
what I mean?
:
Yup. Just keep your filter setup, but before you open Form2, populate a set
of PUBLIC vars with the data you want available to you. Once Form2 is open
you can use the PUBLIC fields to populate the controls of your choice.
HOWEVER, this method DOES NOT allow the PUBLIC fields to be used as
additional filter criteria. The PUBLIC vars are just holders, and not
associated with ANYTHING.
You could pass those values as parameters to a QUERY (for example) or
populate a report with them, or anything else you wanted to do. Maybe I'm not
explaining this well. Your best bet is to create a couple of small test forms
(not linked to any tables) and play with the concept. Once you see it in
action, it becomes crystal clear.
:
Well what I'm trying to do is carry over some data from a record that I have
opened in Form1 and put it in a specific field on Form2. The data is carried
over and is using the RecordID from Form1 as the filter. Does that make sense?
:
PUBLIC vars are not "anything"-specific. Think of them as holding-areas for
values. Once you set them, ANY form can have its controls populated by them.
And their values DO NOT CHANGE until you do it. Or, you can use them in an
ADO record-add, or as a key in a SELECT statement to lookup other records.
It's really up to you, how you use those PUBLIC vars once you have them.
If that doesn't help, perhaps I misunderstood your last question...?
:
Dennis, Does it matter if this data is record specific or will it carry the
data that is in the current record when I open Form2?
:
Create a new "module". Always add "option explicit" to any VBA
form/report/etc that you code up.
In that new module...
PUBLIC MyVar1 as string
PUBLIC MyVar2 as integer
PUBLIC MyVar3 as date
(etc, just like DIM statements)
SAVE the module as some name. I usually use "globalDefs".
Then, in the code for Form1 (in the correct event):
MyVar1 = me.MyForm1VarX
MyVar2 = me.MyForm1VarY
(etc)
Then, in Form2 (in the correct event - usually in the FormOpen event):
me.MyForm2VarX = MyVar1
me.MyForm2VarY = MyVar2
(etc)
That's it.
:
Dennis, Can you give me an example of how you would do this?
:
Personally, I define PUBLIC variables in a module, then set those values in
Form1. When I open Form2, I populate it's controls from those values. I don't
have to "pass" anything. It's all there, and available to every form I open.
:
Hi Ken,
I did what you said but it's not populating the "Customer" control on the
'frmCorrectiveAction". Am I missing something?
:
Concatenate the values into a text string for the OpenArgs string, then
split the string in the new form. In the example below, I am using the pipe
( | ) character as the delimiter.
Main Form:
DoCmd.OpenForm "frmCorrectiveAction", , , "[RMA#]=" & Chr(34) & Me![RMA] &
Chr(34), , , Me![RMA] & "|" & Me!Customer
frmCorrectiveAction:
Dim varOpenArgs As Variant
Dim strCustomer As String
If Len(Me.OpenArgs) > 0 Then
varOpenArgs = Split(Me.OpenArgs, "|")
Me![RMA#].DefaultValue = Chr(34) & varOpenArgs(0) & Chr(34)
strCustomer = varOpenArgs(1)
End If
--
Ken Snell
<MS ACCESS MVP>
message I have the following code to carry over my RecordID from one form to
another
but now I want to carry over other data as well. The field I want to also
carry over is called "Customer". How would I re-write this code to add
another data field to be carried over?
Main Form:
DoCmd.OpenForm "frmCorrectiveAction", , , "[RMA#]=" & Chr(34) & Me![RMA] &
Chr(34), , , Me![RMA]
frmCorrectiveAction:
If Len(Me.OpenArgs) > 0 Then
Me![RMA#].DefaultValue = Chr(34) & Me.OpenArgs & Chr(34)
End If