"Declare Function" err - "not allowed as Public members of object modules"

hi all - trying to use code from
http://www.experts-exchange.com/Databases/MS_Access/Q_21757133.html (below) and get error:

The expression On Open you entered as the event property setting produced the following error: Constants, Fixed-lenght string, arrays, user-defined types and Declare statements not allowed as Public members of object modules.

on db or form open UNLESS I comment out the "declare function" 3 lines right below Option Compare.

What am i doing wrong??  would really like it to work!

thanks, wes




*********************

Option Compare Database
Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer

'Return the user's name.
Private Function UserName() As String
Const UNLEN = 256   ' Max user name length.
Dim user_name As String
Dim name_len As Integer

    user_name = Space$(UNLEN + 1)
    name_len = Len(user_name)
   
    If GetUserName(user_name, name_len) = 0 Then
        UserName = "<unknown>"
    Else
        UserName = Left$(user_name, name_len - 1)
    End If
End Function
new_wesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flavoCommented:
Hi new_wes,
Is that all the code in the module?
have you added this to a form class?  It needs to be in a module, not a form class.
Dave :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmantha709Commented:
Go in modules, create a new module, NOT IN A FORM, a module by itself in the modules tab and put this code in it:

Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer
flavoCommented:
Glad to help!

Good luck with the rest of your project(s)

Just out of curiosity, why the 'B' Grade?
Dave
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

new_wesAuthor Commented:
yup that did it.

why can't the declar be in a form class (that's the only place it will be called)?  
what's the rule for putting functions / subs in Form Class vs Module??

thanks for speedy reply!
new_wesAuthor Commented:
was that not right?  usually i put 'B' "Good!" for answers with code, and save excellent "A" for code and  explainnig why something works - what do they use the letter grades for??
flavoCommented:
Nope, that's all I was after..
>what's the rule for putting functions / subs in Form Class vs Module??
Personally, I don't know of the exact reason why it's can't be in a class, it just can't.
Dave
jmantha709Commented:
You can declare it in the form class if you declare it as Private :

Private Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer
new_wesAuthor Commented:
ahhhh - there we go.

thanks!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.