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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a Module from a Form

Status
Not open for further replies.

bob000

Programmer
Aug 23, 2001
20
US
I have a module function that returns the Microsoft Network Username and Computername. I have tried calling it from a form Load but get the error: Compile error - Expected variable or procedure, not module. Why? Does this code need to be somewhere else?
 
Can you show us the OnLoad code for the form?

[reading] Anthony J. DeSalvo
President - ScottTech Software
"Integrating Technology with Business"
 
Here is the code from the Load:
Private Sub Form_Load()

Dim UserName As String
Dim ComputerName As String

stat = GetUsernameComputerName(UserName, ComputerName)
MsgBox ((UserName + " " + ComputerName))

End Sub

Here is the code from the module:

Option Compare Database
Option Explicit

Public Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long 'i.e. strUserNetworkName = MMC_WNetGetUser "", strUser, 255
Public Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal buffer As String, ByRef nsize As Long) As Boolean

Public Function GetUsernameComputerName(strUserName As String, strHost As String)

Const MAXCHAR As Integer = 50 'Max. no. of chars. in computer name

'Dim strHost As String 'Name of computer returned by function
'Dim strUserName As String 'Network Name of User

'*****************************
'* Get User's Network Name *
'*****************************

strUserName = Space(255)
WNetGetUser "", strUserName, 255
'Debug.Print Trim(strUserName)

'******************************
'* Get User's Computer Name *
'******************************

strHost = String(MAXCHAR, 0)
GetComputerName strHost, MAXCHAR
'Debug.Print Trim(strHost)

End Function

 
I made a few changes in your module, try this:

Public UserName, HostName
Public Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long 'i.e. strUserNetworkName = MMC_WNetGetUser "", strUser, 255
Public Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal buffer As String, ByRef nsize As Long) As Boolean

Public Function GetUsernameComputerName()

Const MAXCHAR As Integer = 50 'Max. no. of chars. in computer name

Dim strHost As String 'Name of computer returned by function
Dim strUserName As String 'Network Name of User

'*****************************
'* Get User's Network Name *
'*****************************

strUserName = Space(255)
WNetGetUser "", strUserName, 255
UserName = Trim(strUserName)

'******************************
'* Get User's Computer Name *
'******************************

strHost = String(MAXCHAR, 0)
GetComputerName strHost, MAXCHAR
HostName = Trim(strHost)

End Function

Then this should work on the load:

Private Sub Form_Load()

GetUsernameComputerName
MsgBox ((UserName + " " + ComputerName))

End Sub


Anthony J. DeSalvo
President - ScottTech Software
"Integrating Technology with Business"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top