Value of Form Variable is Blank

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

Guest

Hello:

I'm new to Access and was trying to figure out how to copy a field value
from Form A to Form B. Klatuu did his best to help me but the expression
didn't work. Now I think I've figured out why the expression didn't work, the
variable vStudentID was null.

But I can't figure out why the variable I created is null. I must be missing
something very obvious to regular Access users.

On a form based upon tblStudents, I created a button. In the on the click
event for the button, I wrote the following code:

Dim vStudentID as String

vStudentId = me.StudentID
I also tried vStudent = me.StudentID.value

In the immediate window I put a question mark before the variable name while
the form was still open, and when I hit enter, there was no value.
?vStudentID - I then hit the Enter Key and the cursor moved the next line.
However, the line was blank.

Please advise what I'm doing wrong.

Thanks,
Robert
 
Your variable vStudentID is known only to the procedure that created it
(the on_click code); even the immediate window won't recognize it. To
test your bit of code, I suggest you use msgbox to display the value:

vStudentId = me.StudentID
msgbox "Student ID = " & vStudentID

in the On-Click code.

If you want to transfer the value to another form, the easiest way is to
have both forms open at the same time (one could be minimized).
Then, if we assume that FormB is the one you are transferring the data
TO, and FormA is still open, then in FormB you could use an expression
something like this:

me![student_ID] = forms!FormA![Student_ID]

You could also make your variable vStudentID a public, or global,
variable - then it will be recognized everywhere.

Hope this helps.

John
 
Hello J. Goddard:

Thanks, that worked! I could see the value of the StudentID in the msgBox.
Everytime I changed records the value changed to the correct one [Current
Event].

So how in the world does one check variables in a private procedure if the
Immediate and Local Windows can't see the variables? Does this make DeBug
irrelevant in Access?

OK, now that I know the variable has a value, I will try your suggestion for
copying a value from From A to Form B.

Robert
 
Hi -

I don't know what you mean by "debug" in this case, but when I am having
problems, I use debug.print, which writes to the immediate window; I
then open that window to see the results, after the procedure has executed.

Change
msgbox "Student ID = " & vStudentID
to
debug.print "Student ID = " & vStudentID

to see what I mean.

John
 
Hello John:

Note: StudentID is a TEXT field

I tried your expression in so called Form B [which is really frmTEST while
I'm testing out this technique] by making it the data source for a text field
and it worked. The text field on Form B reads the value of the current
StudentID on Form A. Now that I have it working, I need to refine it ever so
slightly.

While working in form A, I would like to open Form B and:

[1] First check to see if Form B is already open
[2] If it's not open, then open Form B
[3] Start a NEW record
[4] Enter the current value of StudentID [a text field] from Form A into the
first field of Form B, which is also named StudentID and then continue
entering the rest of the new record on Form B.

Thanks for the info on print.debug, I'll play around with that. I've used
another database program for years, Alpha Five, where all you do is invoke
the debugger and you can do anything you want, including testing the values
of expressions and/or variables. It's very easy and intuitive. I'm somewhat
confused by how this is done in Access. At first glance, it seems far more
difficult than it should, but maybe I'm wrong?

Thanks,
Robert
 
Hi -

You don't really need to go through all that checking. If you open
FormB in "Dialog" window Mode, something like this:

DoCmd.OpenForm FormB,,,,,acDialog

A form opened in acDialog window mode retains the focus until it is
closed. Then, in the On Open event in formB, use

me!StudentID = forms![formA]!StudentID

If formB is used only to add new records, set its Data entry property to
"Yes".

FormB can be called from forms other than FormA as well, modify the
openform command to include the calling form name as an argument:

DoCmd.OpenForm FormB,,,,,acDialog, "FormA"

and change the line in the On Open event of FormB to:

me!StudentId = forms(me.openargs)!StudentID
(assumes that StudentID is the field name on all forms)

I must admit, I don't use the debug features of MS Access much either,
except to find the "current state" of variables when my code stops on
run time errors.

By the way, never leave your final production code to catch runtime
errors with the debug module - use the On Error options to handle them.

Over to you, MVP's, to explain the debug process?

Hope this helps
John
 

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

Back
Top