Using a variable as a default value in a form?

S

sigma

I have defined an integer variable (dim recnum as integer) and would like
to use it as the default value in one of the numeric fields in my form.

What syntax do I use in the default value field of the form as I always get
the #Name? error in the field.

Thanks

Terry
 
N

Nikos Yannacopoulos

Terry,

You cannot reference a VBA variable from within a form; what you need to
do is put a public function in a general module that returns the
variable value:

Public Function Get_recnum() As Integer
Get_recnum = recnum
End Function

Then in your form (or any other object!) you can get the value by
calling the function like:
= Get_recnum()

Note: dim recnum as integer implies recnum is a local variable in some
procedure; to make it available throughout your project, you need to
declare it as Public in a general module declarations section (before
the start of the first procedure).

HTH,
Nikos
 
N

ng

Hello everyone.

This ia wonderful tecnique that I don't think most people use. Maybe they
do and I don't know they do, but not only can use use it to fill in boxes on
a form, but you can use it as a way to gather data for criteria for a query,
or about 1/2 dozen other things I can think of. The public variable, and
ReturnVariable() are essential to getting anything done in access!

Oh, one thing I was told about the return proceedure is that it should have
some processing for null values. Oh, I use my Get_alldata() functions all
the time!:

Public Function Get_recnum() as integer

if nz(recnuum,0) = 0 then
get_recnum= 0
else
get_recnum = recnum
end if

end functrion

-B
 
V

Van T. Dinh

Did you compile the code before you posted?

BTW, are you aware that the Public Variables are reset to their default
values (e.g. zero for numeric P.V., zero-length String for String P.V.) if
an unhandled error occurs in your code.

I actually avoid P.V.s for the above reason. If I use P.V.s, I make sure
that I have error-trapping for every bit of code in the database.
 

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

Top