Working with functions in module

H

H. Martins

Hi,

I've got the following code in one form's VBA:

Option Compare Database
Option Explicit

Public fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function

Calling it works fine, but as I will call it from other forms I
decided to transfer it to a module - not really sure what I was doing.

Basically I wanted sort of #include a file in the form's VBAs.

I transferred the function do a new Module. Looked like I just had to
define it as Public:

Public fOSUserName() As String

But then I have error message "Invalid Outside Procedure" in:

strUserName = String$(254, 0) ' << in the "254"

I struggled with the helps ans this newsgroup but I didn't find (or
realize I was finding) a way out.

Could I have some help, please?

Thanks,
Henry
 
K

Keith Wilby

H. Martins said:
Hi,

I've got the following code in one form's VBA:

Henry,

Please ensure that the original credit is included in your code:

' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish

Keith.
 
H

Henry

Keyth,
Please ensure that the original credit is included in your code:

I've done that.

Ok. I got it running but I had to make my public function return a
useless long.

I had to do it as:

longUseless = myfunction( vars ...)

in the module:

public function myFunction (vars ... ) as long

end function
 
H

Henry

**** sorry, the above message was sent too soon - I am still writing
****

I will come back soon
 
H

Henry

Keyth,
Please ensure that the original credit is included in your code:

I've done that.

Ok. I got it running.

Then I tried another function and got a surprise. I had to make my
public function return an useless long.

In the form's VBA:

Dim longUseless as long
longUseless = myfunction( vars ...)

In the module:

public function myFunction (vars ... ) as long
... code
myFunction = 0
end function

I had to implement the longUseless and make the function to return
something because the compiler kept saying that an "=" was
missing ...!

From time to time I have surprises. I thought that a function
returning nothing would be trivial. I am almost sure I had already
written functions without returning data before.

Henry.
 
D

Douglas J. Steele

Henry said:
Ok. I got it running but I had to make my public function return a
useless long.

I had to do it as:

longUseless = myfunction( vars ...)

in the module:

public function myFunction (vars ... ) as long

end function

You should be able to avoid that by using

Call MyFunction(vars....)

(of course, if you don't want a value returned, there's no reason to use a
function: you can use a Sub instead)
 
H

Henry

(of course, if you don't want a value returned, there's no reason to use a
function: you can use a Sub instead)

Thanks Doug,

Right. Nevertheless I still have do use Call mySub.

By the way, is there a way to assign the current function or sub name
to a variable?

Me.Name does it to a form. What about to a function or sub?

Thanks
Henry
 
D

Douglas J. Steele

While I consider it good practice to use Call MySub, it's not strictly
necessary.

If you've got parameters, simply leave out the parentheses:

Call MySub(parm1, parm2)

or

MySub parm1, parm2

There's nothing built into VBA to give you the name of the current function
or sub. You can always set a variable in your code. (For help, take a look
at MZ Tools http://www.mztools.com/v3/mztools3.aspx It can do this, and far
more, for you, and it's free!)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


(of course, if you don't want a value returned, there's no reason to use a
function: you can use a Sub instead)

Thanks Doug,

Right. Nevertheless I still have do use Call mySub.

By the way, is there a way to assign the current function or sub name
to a variable?

Me.Name does it to a form. What about to a function or sub?

Thanks
Henry
 

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