"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?
 
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 :-)
0
 
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
0
 
flavoCommented:
Glad to help!

Good luck with the rest of your project(s)

Just out of curiosity, why the 'B' Grade?
Dave
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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!
0
 
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??
0
 
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
0
 
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
0
 
new_wesAuthor Commented:
ahhhh - there we go.

thanks!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.