API calls for user name 
Author Message
 API calls for user name

Hi, I'm trying to set up an SQL SELECT query that uses the
username of the current user (obtained through an API call
to advapi32.dll. The problem I'm having is that no matter
how I set it up, it seems that I cannot add a single
quotation symbol (or anything else, for that matter) to
the end of the variable string.

Here is the code I'm using to access the user name:
====================================================
Private Declare Function GetUserNameAPI Lib _
     "advapi32.dll" Alias "GetUserNameA" _
     (ByVal lpbuffer As String, nSize As Long) As Long

Public userName As String

Public Sub GetUserName()
  Dim sBuffer As String
  Dim lSize As Long

  ' Space for dll parameters
  sBuffer = Space$(255)
  lSize = Len(sBuffer)

  Call GetUserNameAPI(sBuffer, lSize)

  If lSize > 0 Then
    ' Remove empty spaces
    userName = Left$(sBuffer, lSize)
  Else
    ' Return empty if no user is found
    userName = vbNullString
  End If

End Sub
================================================

This code is in a Basic Module along with the code to
query the database:

================================================
Public Sub GetUserLevel()
    Dim sUser As String
    Dim X As Integer

    Dim rs As New ADODB.Recordset

    Dim sSql As String
    Dim sSELECT As String
    Dim sWHERE As String
    Dim sName As String

    Dim FindInDB As Integer

    Dim TableName As String

    Dim sUserID As Integer
    Dim sUserLevel As Integer
    Dim msgPrompt As String

    sUser = userName

    TableName = "tblUsers"
    sSELECT = "SELECT * FROM " & TableName & " "
    sWHERE = "WHERE userName = '" & sUser & "' "
    sSql = sSELECT
    msgPrompt = "SQL Statement: " & sSql
    MsgBox msgPrompt

    Set rs = dbConn.Execute(sSql)
    X = 0
    If (rs.EOF) And (rs.BOF) Then
        FindInDB = 0
        MsgBox "You are not an authorized user. Please see
your Network Administrator for Authorization Clearance. "

    Else
        Do Until rs.EOF
            sUserID = rs!userID
            sUserLevel = rs!userLevel
            X = X + 1
            rs.MoveNext
        Loop
        If rs.EOF Then
            FindInDB = 1
            If X > 1 Then
                MsgBox "There are more than one user with
your userID. Please see your Network Administrator to
clear this error."
            End If
            If X = 0 Then
                MsgBox "No user record found! "
                End
            End If
        Else
'            sSql = sCOUNT
'            Set rs = dbConn.Execute(sSql, , adCmdText)
'            FindInDB = rs(0)
        End If
    End If

    Set rs = Nothing
    If sUserLevel > 0 Then
        userLevel = sUserLevel
    Else
        userLevel = -1
    End If

End Sub
======================================================

The problem is with this line:
sWHERE = "WHERE userName = '" & sUser & "' "

no matter what combination I use, the last "' " does not
get added on.

For example, if the GetUserName sub returns "vbfan" for
the username, then the value of sWHERE looks like:
      WHERE userName = 'vbfan

Also, I've tried to remove the WHERE clause from the sql
statement and simply compare the returned username from
the recordset with the value for userName, but it cuts off
everything proceding the instance of userName.

For example, if I use:
   If rs!userName = userName Then
       ....
   End If

The comparison is never made.

Is this a bug? if so, is there a workaround? If not, can
anyone spot what I'm doing wrong?

Thanks,

Charlie



Mon, 28 Jun 2004 04:58:21 GMT
 API calls for user name

Try userName = Left$(sBuffer, lSize-1)

I think you'll find the lSize character is a null (Chr$(0))

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele


Quote:
> Hi, I'm trying to set up an SQL SELECT query that uses the
> username of the current user (obtained through an API call
> to advapi32.dll. The problem I'm having is that no matter
> how I set it up, it seems that I cannot add a single
> quotation symbol (or anything else, for that matter) to
> the end of the variable string.

> Here is the code I'm using to access the user name:
> ====================================================
> Private Declare Function GetUserNameAPI Lib _
>      "advapi32.dll" Alias "GetUserNameA" _
>      (ByVal lpbuffer As String, nSize As Long) As Long

> Public userName As String

> Public Sub GetUserName()
>   Dim sBuffer As String
>   Dim lSize As Long

>   ' Space for dll parameters
>   sBuffer = Space$(255)
>   lSize = Len(sBuffer)

>   Call GetUserNameAPI(sBuffer, lSize)

>   If lSize > 0 Then
>     ' Remove empty spaces
>     userName = Left$(sBuffer, lSize)
>   Else
>     ' Return empty if no user is found
>     userName = vbNullString
>   End If

> End Sub
> ================================================

> This code is in a Basic Module along with the code to
> query the database:

> ================================================
> Public Sub GetUserLevel()
>     Dim sUser As String
>     Dim X As Integer

>     Dim rs As New ADODB.Recordset

>     Dim sSql As String
>     Dim sSELECT As String
>     Dim sWHERE As String
>     Dim sName As String

>     Dim FindInDB As Integer

>     Dim TableName As String

>     Dim sUserID As Integer
>     Dim sUserLevel As Integer
>     Dim msgPrompt As String

>     sUser = userName

>     TableName = "tblUsers"
>     sSELECT = "SELECT * FROM " & TableName & " "
>     sWHERE = "WHERE userName = '" & sUser & "' "
>     sSql = sSELECT
>     msgPrompt = "SQL Statement: " & sSql
>     MsgBox msgPrompt

>     Set rs = dbConn.Execute(sSql)
>     X = 0
>     If (rs.EOF) And (rs.BOF) Then
>         FindInDB = 0
>         MsgBox "You are not an authorized user. Please see
> your Network Administrator for Authorization Clearance. "

>     Else
>         Do Until rs.EOF
>             sUserID = rs!userID
>             sUserLevel = rs!userLevel
>             X = X + 1
>             rs.MoveNext
>         Loop
>         If rs.EOF Then
>             FindInDB = 1
>             If X > 1 Then
>                 MsgBox "There are more than one user with
> your userID. Please see your Network Administrator to
> clear this error."
>             End If
>             If X = 0 Then
>                 MsgBox "No user record found! "
>                 End
>             End If
>         Else
> '            sSql = sCOUNT
> '            Set rs = dbConn.Execute(sSql, , adCmdText)
> '            FindInDB = rs(0)
>         End If
>     End If

>     Set rs = Nothing
>     If sUserLevel > 0 Then
>         userLevel = sUserLevel
>     Else
>         userLevel = -1
>     End If

> End Sub
> ======================================================

> The problem is with this line:
> sWHERE = "WHERE userName = '" & sUser & "' "

> no matter what combination I use, the last "' " does not
> get added on.

> For example, if the GetUserName sub returns "vbfan" for
> the username, then the value of sWHERE looks like:
>       WHERE userName = 'vbfan

> Also, I've tried to remove the WHERE clause from the sql
> statement and simply compare the returned username from
> the recordset with the value for userName, but it cuts off
> everything proceding the instance of userName.

> For example, if I use:
>    If rs!userName = userName Then
>        ....
>    End If

> The comparison is never made.

> Is this a bug? if so, is there a workaround? If not, can
> anyone spot what I'm doing wrong?

> Thanks,

> Charlie



Mon, 28 Jun 2004 07:44:24 GMT
 API calls for user name
That worked like a charm!

Thanks!

I'm still not sure what the API call is doing, but it
works correctly now. Thanks again.

Charlie

Quote:
>-----Original Message-----
>Try userName = Left$(sBuffer, lSize-1)

>I think you'll find the lSize character is a null
(Chr$(0))

>--
>Doug Steele, Microsoft Access MVP
>http://I.Am/DougSteele


message

>> Hi, I'm trying to set up an SQL SELECT query that uses
the
>> username of the current user (obtained through an API
call
>> to advapi32.dll. The problem I'm having is that no
matter
>> how I set it up, it seems that I cannot add a single
>> quotation symbol (or anything else, for that matter) to
>> the end of the variable string.

>> Here is the code I'm using to access the user name:
>> ====================================================
>> Private Declare Function GetUserNameAPI Lib _
>>      "advapi32.dll" Alias "GetUserNameA" _
>>      (ByVal lpbuffer As String, nSize As Long) As Long

>> Public userName As String

>> Public Sub GetUserName()
>>   Dim sBuffer As String
>>   Dim lSize As Long

>>   ' Space for dll parameters
>>   sBuffer = Space$(255)
>>   lSize = Len(sBuffer)

>>   Call GetUserNameAPI(sBuffer, lSize)

>>   If lSize > 0 Then
>>     ' Remove empty spaces
>>     userName = Left$(sBuffer, lSize)
>>   Else
>>     ' Return empty if no user is found
>>     userName = vbNullString
>>   End If

>> End Sub
>> ================================================

>> This code is in a Basic Module along with the code to
>> query the database:

>> ================================================
>> Public Sub GetUserLevel()
>>     Dim sUser As String
>>     Dim X As Integer

>>     Dim rs As New ADODB.Recordset

>>     Dim sSql As String
>>     Dim sSELECT As String
>>     Dim sWHERE As String
>>     Dim sName As String

>>     Dim FindInDB As Integer

>>     Dim TableName As String

>>     Dim sUserID As Integer
>>     Dim sUserLevel As Integer
>>     Dim msgPrompt As String

>>     sUser = userName

>>     TableName = "tblUsers"
>>     sSELECT = "SELECT * FROM " & TableName & " "
>>     sWHERE = "WHERE userName = '" & sUser & "' "
>>     sSql = sSELECT
>>     msgPrompt = "SQL Statement: " & sSql
>>     MsgBox msgPrompt

>>     Set rs = dbConn.Execute(sSql)
>>     X = 0
>>     If (rs.EOF) And (rs.BOF) Then
>>         FindInDB = 0
>>         MsgBox "You are not an authorized user. Please
see
>> your Network Administrator for Authorization
Clearance. "

>>     Else
>>         Do Until rs.EOF
>>             sUserID = rs!userID
>>             sUserLevel = rs!userLevel
>>             X = X + 1
>>             rs.MoveNext
>>         Loop
>>         If rs.EOF Then
>>             FindInDB = 1
>>             If X > 1 Then
>>                 MsgBox "There are more than one user
with
>> your userID. Please see your Network Administrator to
>> clear this error."
>>             End If
>>             If X = 0 Then
>>                 MsgBox "No user record found! "
>>                 End
>>             End If
>>         Else
>> '            sSql = sCOUNT
>> '            Set rs = dbConn.Execute(sSql, , adCmdText)
>> '            FindInDB = rs(0)
>>         End If
>>     End If

>>     Set rs = Nothing
>>     If sUserLevel > 0 Then
>>         userLevel = sUserLevel
>>     Else
>>         userLevel = -1
>>     End If

>> End Sub
>> ======================================================

>> The problem is with this line:
>> sWHERE = "WHERE us{ w f ?l tzY"?? ^*erName = '" &
sUser & "' "

>> no matter what combination I use, the last "' " does not
>> get added on.

>> For example, if the GetUserName sub returns "vbfan" for
>> the username, then the value of sWHERE looks like:
>>       WHERE userName = 'vbfan

>> Also, I've tried to remove the WHERE clause from the sql
>> statement and simply compare the returned username from
>> the recordset with the value for userName, but it cuts
off
>> everything proceding the instance of userName.

>> For example, if I use:
>>    If rs!userName = userName Then
>>        ....
>>    End If

>> The comparison is never made.

>> Is this a bug? if so, is there a workaround? If not, can
>> anyone spot what I'm doing wrong?

>> Thanks,

>> Charlie

>.



Mon, 28 Jun 2004 08:10:33 GMT
 
 [ 3 post ] 

 Relevant Pages 

1. Calling OpenIngres API users

2. API Novell User Name

3. Changing from direct Btrieve API calls to SQL calls

4. NT User Name blank or SQL User Name blank

5. Name Calling Conflict - Don't Call Me That (VCX)

6. Oracle Names Servers and Net8 and 8i - What to call the Service Name

7. Get NT users login name? Is there an api or vb call?

8. calling access querydef that calls a user defined access function

9. How to retrieve a computer name and user name of a computer

10. Full name from user name

11. Calling Stored Proc as another user other than the logged in user

12. Accessing another user's table without qualifying table name with the owner-name


 
Powered by phpBB® Forum Software