• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1084
  • Last Modified:

"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
0
new_wes
Asked:
new_wes
  • 3
  • 3
  • 2
2 Solutions
 
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now