Help with Function as default value in form

B

BDPIII

I have a module with the code below, the function works fine when ran in the
Immediate window but when I set the default value of a textbox on a field to
the function "=GetUser()" it is not returning anything in the text box.

Option Compare Database

Type WKSTA_USER_INFO_1
wkui1_username As Long
End Type

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _
(lpName As Any, ByVal lpUserName$, lpnLength&)

Function GetUser()
Dim ret As Long, buffer(512) As Byte, i As Integer
Dim cbusername As Long, username As String

' Windows 95 or NT - call WNetGetUser to get the name of the user.
username = Space(256)
cbusername = Len(username)
ret = WNetGetUser(ByVal 0&, username, cbusername)
If ret = 0 Then
' Success - strip off the null.
username = Left(username, InStr(username, Chr(0)) - 1)
Else
username = ""
End If

Debug.Print username
End Function

I have also tried to change another field's after update property to set the
username field to

Private Sub ordernumber_AfterUpdate()
If Me![ordernumber] > 0 Then
Me![username] = GetUser()

End If
End Sub

When I do this I get a runtime error which says username cannot be a zero
length string. I can go back to my VBA screen and see that the function has
run in the immediate widow successfully.

Thanks in advance for any help!

Barry
 
B

BDPIII

That did it!
Endless thank you's Ken!

Barry

Ken Sheridan said:
Barry:

You are not setting the return value of the function anywhere. The reason
it appears to return a value in the debug window is because you are printing
the value of the username variable to the debug window. Change the module as
follows and it should be fine:

Option Compare Database

Type WKSTA_USER_INFO_1
wkui1_username As Long
End Type

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _
(lpName As Any, ByVal lpUserName$, lpnLength&)

Function GetUser()

Dim ret As Long, buffer(512) As Byte, i As Integer
Dim cbusername As Long, username As String

' Windows 95 or NT - call WNetGetUser to get the name of the user.
username = SPACE(256)
cbusername = Len(username)
ret = WNetGetUser(ByVal 0&, username, cbusername)
If ret = 0 Then
' Success - strip off the null.
username = Left(username, InStr(username, Chr(0)) - 1)
Else
username = ""
End If

GetUser = username

End Function

Ken Sheridan
Stafford, England

BDPIII said:
I have a module with the code below, the function works fine when ran in the
Immediate window but when I set the default value of a textbox on a field to
the function "=GetUser()" it is not returning anything in the text box.

Option Compare Database

Type WKSTA_USER_INFO_1
wkui1_username As Long
End Type

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _
(lpName As Any, ByVal lpUserName$, lpnLength&)

Function GetUser()
Dim ret As Long, buffer(512) As Byte, i As Integer
Dim cbusername As Long, username As String

' Windows 95 or NT - call WNetGetUser to get the name of the user.
username = Space(256)
cbusername = Len(username)
ret = WNetGetUser(ByVal 0&, username, cbusername)
If ret = 0 Then
' Success - strip off the null.
username = Left(username, InStr(username, Chr(0)) - 1)
Else
username = ""
End If

Debug.Print username
End Function

I have also tried to change another field's after update property to set the
username field to

Private Sub ordernumber_AfterUpdate()
If Me![ordernumber] > 0 Then
Me![username] = GetUser()

End If
End Sub

When I do this I get a runtime error which says username cannot be a zero
length string. I can go back to my VBA screen and see that the function has
run in the immediate widow successfully.

Thanks in advance for any help!

Barry
 
K

Ken Sheridan

Barry:

You are not setting the return value of the function anywhere. The reason
it appears to return a value in the debug window is because you are printing
the value of the username variable to the debug window. Change the module as
follows and it should be fine:

Option Compare Database

Type WKSTA_USER_INFO_1
wkui1_username As Long
End Type

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _
(lpName As Any, ByVal lpUserName$, lpnLength&)

Function GetUser()

Dim ret As Long, buffer(512) As Byte, i As Integer
Dim cbusername As Long, username As String

' Windows 95 or NT - call WNetGetUser to get the name of the user.
username = SPACE(256)
cbusername = Len(username)
ret = WNetGetUser(ByVal 0&, username, cbusername)
If ret = 0 Then
' Success - strip off the null.
username = Left(username, InStr(username, Chr(0)) - 1)
Else
username = ""
End If

GetUser = username

End Function

Ken Sheridan
Stafford, England
 

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