Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a field to a Public Sub

Status
Not open for further replies.

tarekm

Technical User
Oct 25, 2000
7
CA
Hi:

How can I pass a specific field on a form from its event procedure to a Public Sub in another module and change the value of this field?

My objective is to use a public Sub that can be used by different forms.

I found no problem in changing the field value in a Sub within the same form module.

Thanx

Tarek
 
Create a module and put it there. Then all forms can share it.

Is this what you are looking for?

Mary :)
 
I think what you want to use is a function. A function will return a value, where as a sub won't. You can pass a value to the function, then perform operations on it, and the function will return a new value. In the code on the form, you assign the control to the value of a function, i.e.

me.textbox1 = myfunction(somevaluetopass)

Remember in the code of the function that you have to assign a value to the function:

Public Function sqr(somevalue as integer)
sqr = somevalue * somevalue
end function

Mike Rohde
rohdem@marshallengines.com
 
Hmmmmmm,

Mike,

You do not HAVE to assign a value to the function. Ms. Access will always assign a default value to the function (return), You only need to assign the value if you want the Function to return the value to the calling procedure. If the function assigns a value to a variable within it's scope, youy can achive the general effect, without a specific return value assignment. It is, however, "Good Practice".



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
I stand corrected, however in Tarek's case, the function will have to return a value to the calling procedure.

Mike Rohde
rohdem@marshallengines.com
 
Hi,

Thank you very much for your advices. they are very helpfull.

I developed a function with the following declaration:

Public Function updatedate(fdate As Date) As Date


It gives me an error when I pass a NULL value. the error message reads as following: Run time error 94. Invalid use of Null.

Could you please help me in solving that problem.

Thanx

Tarek






 
Look into using the Nz function

Mike Rohde
rohdem@marshallengines.com
 
Yes, but the check need to be done in the calling procedure, as passing null as the argument which is exxpecting date is the problem. Also, you need to define the rule for what to do on invalid dates, as well as determine what (else) constitutes an invalid date for your app.

In your function declaration, you are passing a date and returning a date, so the 'assumption' here it that you are doing some date checking/arithmatic calc. It may be possible to redo the declaration to avoid the need to handle your Null externally. Consider the following:

Code:
Public Function UpdateDate(Optional FDate As Variant) As Date

    If (IsDate(FDate)) Then
        UpdateDate = DateAdd("m", 1, FDate)
     Else
        UpdateDate = DateAdd("m", 1, Now)
    End If

End Function

The combination of the change to variant type and the use of the optional keyword permits a wider range of inputs. The date calcls, of course. are just to demo the capability and need to be replaced with your specific needs, however is does show the capability to handle different situations based on the 'validity' of the input argument(s).



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top