Microsoft Office 2010 (32-bit or 64-bit)

Sorry People here!

I know you answered this question somehow some , but now there is one bit not clear according to Microsoft instructions below:

To write code that can work in both new and older versions of Office you can use a combination of the new VBA7 and Win64conditionalCompiler Constants. The Vba7 conditional compiler constant is used to determine if code is running in version 7 of the VB editor (the VBA version that ships in Office 2010). The Win64 conditional compilation constant is used to determine which version (32-bit or 64-bit) of Office is running.

#if Vba7 then
'  Code is running in the new VBA7 editor
     #if Win64 then
     '  Code is running in 64-bit version of Microsoft Office
     #else
     '  Code is running in 32-bit version of Microsoft Office
     #end if
#else
' Code is running in VBA version 6 or earlier

Does it means that us who are using Ms access 2016

Then full code should be as follow:

#If VBA7 Then
'* My 64-bit declarations
Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
#Else
'* My 32-bit declarations
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
#End If
#IF Win64 THEN
  Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal _
   lpBuffer As String, nSize As Long) As LongPtr
#Else
  Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal _
   lpBuffer As String, nSize As Long) As Long

#End If


Public Function GetUser() As String

   Dim strBuffer As String
   Dim lngSize As Long, lngRetVal As Long
   
   lngSize = 199
   strBuffer = String$(200, 0)
   
   lngRetVal = GetUserName(strBuffer, lngSize)
   
   GetUser = Left$(strBuffer, lngSize - 1)

End Function


Public Function GetFullName()

    Const MESSAGETEXT = "The current user is not recorded in the Users table."
    Dim strCriteria As String
    Dim varFullName As Variant
   
    strCriteria = "LoginName = """ & GetUser & """"

    varFullName = DLookup("FullName", "Users", strCriteria)
   
    If Not IsNull(varFullName) Then
        GetFullName = varFullName
    Else
        MsgBox MESSAGETEXT, vbExclamation, "Warning"
    End If
       
End Function

Public Function AddNewUser()

    Dim strCriteria As String
    Dim strLoginName As String
    Dim strFullName As String
    Dim strSQL As String
   
    strLoginName = GetUser()
    strCriteria = "LoginName = """ & strLoginName & """"
   
    If IsNull(DLookup("LoginName", "Users", strCriteria)) Then
        strFullName = InputBox("Enter new user's full name:")
        strSQL = "INSERT INTO Users(LoginName,FullName) " & _
            "VALUES(""" & strLoginName & """,""" & strFullName & """)"
   
        CurrentDb.Execute strSQL, dbFailOnError
    End If

End Function
 
I just want to be clear before trying this!

IS THE COMBINATION THE BEST WAY TO GO FOR US USING MS ACCESS 2016

Regards

Chris
LVL 2
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAsked:
Who is Participating?
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
That code should work in 2010 or higher. I'm not entirely sure you need the VBA7 conditions, but you may.

As John said, there are other ways to get the UserName, but the API calls are the most reliable (at least in my opinion).
0
 
John TsioumprisSoftware & Systems EngineerCommented:
I am not sure ...because i don't use 64 Access....but in your code i see code that doesn't need special API calls so not all this  and you substitute it like this
Public Function GetUser() As String
GetUser=Eniviron("USERNAME")
End Function

Open in new window

0
 
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAuthor Commented:
Okay , the reasons why I think will be required is that , I want to track the following:

(1) The person who have created the voucher whether journal or invoice ( This mean all voucher will have a control called Created By: and this control will be on Before insert event )

Example

Before insert..............
Me.[Created by] =  GetUser
End Sub

The same will be done on the authorizer

Example

After update...........
Me.[Authorized By] =  GetUser
End Sub

Regards

Chris
0
 
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAuthor Commented:
I think now I do agree with Scott , the way to for this API maths is to use

#IF Win64 THEN
  Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal _
   lpBuffer As String, nSize As Long) As LongPtr
#Else
  Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal _
   lpBuffer As String, nSize As Long) As Long
#End If
Public Function GetUser() As String

   Dim strBuffer As String
   Dim lngSize As Long, lngRetVal As Long
   
   lngSize = 199
   strBuffer = String$(200, 0)
   
   lngRetVal = GetUserName(strBuffer, lngSize)
   
   GetUser = Left$(strBuffer, lngSize - 1)

End Function


Public Function GetFullName()

    Const MESSAGETEXT = "The current user is not recorded in the Users table."
    Dim strCriteria As String
    Dim varFullName As Variant
   
    strCriteria = "LoginName = """ & GetUser & """"

    varFullName = DLookup("FullName", "Users", strCriteria)
   
    If Not IsNull(varFullName) Then
        GetFullName = varFullName
    Else
        MsgBox MESSAGETEXT, vbExclamation, "Warning"
    End If
       
End Function

Public Function AddNewUser()

    Dim strCriteria As String
    Dim strLoginName As String
    Dim strFullName As String
    Dim strSQL As String
   
    strLoginName = GetUser()
    strCriteria = "LoginName = """ & strLoginName & """"
   
    If IsNull(DLookup("LoginName", "Users", strCriteria)) Then
        strFullName = InputBox("Enter new user's full name:")
        strSQL = "INSERT INTO Users(LoginName,FullName) " & _
            "VALUES(""" & strLoginName & """,""" & strFullName & """)"
   
        CurrentDb.Execute strSQL, dbFailOnError
    End If

End Function

Regards

Chris
0
 
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAuthor Commented:
Thank you so much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.