Access Cookbook - Ken Getz [249]
Most of the functions either return or set an error value, indicating the outcome of the function call. Though there are too many possible errors to list them all here, Table 11-15 lists most of the common ones that you'll receive when making these function calls.
Table 11-15. Common networking errors
Value
Constant
Description
0
NO_ERROR
No error occurred.
5
ERROR_ACCESS_DENIED
Access is denied.
66
ERROR_BAD_DEV_TYPE
The network resource type is not correct.
67
ERROR_BAD_NET_NAME
The network name cannot be found.
85
ERROR_ALREADY_ASSIGNED
The local device name is already in use.
86
ERROR_INVALID_PASSWORD
The specified network password is not correct.
170
ERROR_BUSY
The requested resource is in use.
234
ERROR_MORE_DATA
More data is available.
1200
ERROR_BAD_DEVICE
The specified device name is invalid.
1201
ERROR_CONNECTION_UNAVAIL
The device is not currently connected, but it is a remembered connection.
1202
ERROR_DEVICE_ALREADY_REMEMBERED
An attempt was made to remember a device that had previously been remembered.
1203
ERROR_NO_NET_OR_BAD_PATH
No network provider accepted the given network path.
1204
ERROR_BAD_PROVIDER
The specified network provider name is invalid.
1205
ERROR_CANNOT_OPEN_PROFILE
Unable to open the network connection profile.
1206
ERROR_BAD_PROFILE
The network connection profile is corrupt.
1208
ERROR_EXTENDED_ERROR
An extended error has occurred.
1222
ERROR_NO_NETWORK
The network is not present or not started.
1223
ERROR_CANCELED
The user canceled a dialog.
2250
ERROR_NOT_CONNECTED
This network connection does not exist.
Retrieving information
To retrieve the current user's name, call the acbGetUser function:
Public Function acbGetUser(Optional varErr As Variant) As String
Dim strBuffer As String
Dim lngRetval As Long
Dim lngSize As Long
lngSize = conMaxPath
Do
strBuffer = Space(lngSize)
lngRetval = WNetGetUser(0&, strBuffer, lngSize)
Loop Until lngRetval <> ERROR_MORE_DATA
If lngRetval <> NO_ERROR Then
acbGetUser = ""
Else
acbGetUser = TrimNull(strBuffer)
End If
varErr = lngRetval
End Function
The acbGetUser function calls the Windows API to retrieve the currently logged-in user's name. Note that there are several ways for the Windows API and Access to communicate the length of data to be returned. In this case, the code sets up a buffer of arbitrary length and calls the Windows API. If the buffer was large enough, it fills it in with the requested name. If not, it returns the value ERROR_MORE_DATA, indicating that it needs more space. It then passes back in the lngSize variable the actual number of characters it does need, and the code loops around, trying again with the specified size.
If you want to know the exact error that occurred in the attempt to retrieve the current user's name, you can pass a variant variable in as a parameter to acbGetUser. It's optional, but if you supply the value, the function will pass back the error code to you in that variable. For example:
Dim varErr as Variant
' If you care about the error:
Debug.Print acbGetUser(varErr)
Debug.Print "The error was: "; varError
' If you don't care about any errors:
Debug.Print acbGetUser( )
To retrieve the current computer's name, call the acbGetComputerName wrapper function. Windows stores the current computer's name in the registry database and reads it from there when necessary. To shield your code from having to know exactly where that piece of information is stored, Windows provides the GetComputerName API function.
The following function, acbGetComputerName, handles the passing of data between Access and Windows for you:
Public Function acbGetComputerName( ) As String
' Retrieve the network name of the current computer.
Dim strBuffer As String
Dim lngSize As Long
Dim blnOK As Integer
lngSize = conMaxComputerNameLength+ 1
strBuffer = Space(lngSize)
blnOK = GetComputerName(strBuffer, lngSize)