UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Apigetusername Doesn't Work In 2010    
 
   
corrieann
post Sep 21 2011, 03:44 PM
Post #1

UtterAccess Guru
Posts: 569



This used to work in 2003 and 2007. After converting to 2010 I get an error. Here is the code:

CODE
Function FOSUserName() As String
On Error GoTo fOSUserName_Err

Dim lngLen As Long, lngX As Long
Dim strUserName As String

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)

If lngX <> 0 Then
FOSUserName = Left$(strUserName, lngLen - 1)
Else
FOSUserName = ""
End If


fOSUserName_Exit:
Exit Function

fOSUserName_Err:
MsgBox Error$
Resume fOSUserName_Exit
End Function


Any help?


--------------------
"I have to stop saying, 'How stupid can you be.' I think people are starting to take it as a challenge."
Go to the top of the page
 
+
Doug Steele
post Sep 21 2011, 07:10 PM
Post #2

UtterAccess VIP
Posts: 18,477
From: St. Catharines, ON (Canada)



Since you seem to be talking about the code from Get Login name at "The Access Web", I can attest to the fact that it still works in Access 2010: I just tested it.

What's the exact error you're getting?


--------------------
Doug Steele, Microsoft Access MVP
http://www.accessmvp.com/DJSteele/AccessIndex.html
Co-author: Access Solutions: Tips, Tricks, and Secrets from Microsoft Access MVPs, published by Wiley
Technical Editor: Access 2010 Bible, published by Wiley, Access 2013 Bible, published by Wiley.
Go to the top of the page
 
+
DanielPineault
post Sep 21 2011, 08:43 PM
Post #3

UtterAccess VIP
Posts: 2,659



Did you include the declarations in your module?

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


--------------------
Daniel Pineault
2010-2013 Microsoft MVP
Professional Help: http://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: http://www.devhut.net

Vir sapit qui pauca loquitur
Go to the top of the page
 
+
datAdrenaline
post Sep 22 2011, 01:58 PM
Post #4

UtterAccess Editor
Posts: 16,705
From: Northern Virginia, USA



Are you using A2010 64bit? If so, you need to Declare your API calls differently ...

CODE
Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


As a matter of fact EVERY API declaration in Access 2010/64bit will need the PtrSafe keyword injected into it. If you are running in a mixed mode (A2003/A2010) you will have to use conditional compilation to accommodate the different environments.


--------------------
Brent Spaulding | datAdrenaline | Access MVP
It's all very well to tell us to forgive our enemies; our enemies can never hurt us very much. But oh, what about forgiving our friends? - Willa Cather; As always - Pay it Forward!
Go to the top of the page
 
+
corrieann
post Sep 26 2011, 03:29 PM
Post #5

UtterAccess Guru
Posts: 569



No, we are using 32 bit.


--------------------
"I have to stop saying, 'How stupid can you be.' I think people are starting to take it as a challenge."
Go to the top of the page
 
+
datAdrenaline
post Sep 26 2011, 03:55 PM
Post #6

UtterAccess Editor
Posts: 16,705
From: Northern Virginia, USA



I would suggest you decompile your application:

MSACCESS.EXE /excl /decompile "thedatabaseFullPathAndName.accdb"


Also, if it still does not work, consider using the following instead:

Environ("USERNAME")

This will return the same information, although Environ() has been accused of being easier to spoof, so in your situation it may not be a valid alternative.


--------------------
Brent Spaulding | datAdrenaline | Access MVP
It's all very well to tell us to forgive our enemies; our enemies can never hurt us very much. But oh, what about forgiving our friends? - Willa Cather; As always - Pay it Forward!
Go to the top of the page
 
+
corrieann
post Sep 26 2011, 04:23 PM
Post #7

UtterAccess Guru
Posts: 569



Ok, here is the message I get:

The expression On Click you entered as the event property setting produced the following error: The object doesn't contain the Autmation object 'AbbAndFullDescStorage.'.
*The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
*There may have been an error evaluating the function, event, or macro.

If I click on Show Help:
This erroroccurs when an event has failed to run because the location of the logic for the event cannot be evaluated. For example, if the OnOpen property of a form is set to =[Field], this error occurs because a macro or event name is expected to run when the event occurs.

For the OnClick value behind the SAVE control, I have =[AbbAndFullDescStorage]

The code for the Public Sub [AbbAndFullDescStorage] is as follows:

CODE
Public Sub AbbAndFullDescStorage()
AbbreviatedDescriptionForMasterTable = Month.Column(0) & "-" & Year.Column(0) & "-" & Project.Column(0) & "-" & Activity.Column(0) & _
("-" + TypeCode.Column(0)) & ("-" + Type2Code.Column(0)) & ("-" + ContactCode.Column(0)) & ("-" + City.Column(0)) & ("-" + State.Column(0)) & ("-" + Country.Column(0)) & ("-" + DayInstance.Column(0)) & ("-" + [EA1])

FullDescriptionForMasterTable = Month.Column(1) & "-" & Year.Column(1) & "-" & Project.Column(1) & "-" & Activity.Column(1) & _
("-" + TypeCode.Column(1)) & ("-" + Type2Code.Column(1)) & ("-" + ContactCode.Column(1)) & ("-" + City.Column(1)) & ("-" + State.Column(1)) & ("-" + Country.Column(1)) & ("-" + DayInstance.Column(1)) & ("-" + [EA2])

LastUpdate = Now()
UserName = GetUserName()


Me.PCode.BackColor = vbWhite
Me.PCode.ForeColor = vbBlack
Me.PCode.FontWeight = 400

If Not Nz(Me.PCode, "") = "" Then
strsql = "UPDATE tblPCodes SET tblpcodes.OldSourceCode = [SourceCode] WHERE tblPCodes.[P-Code] = " & [PCode]
Exit Sub
End If

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End Sub



I wasn't sure where the error was, so I compiled the code through the Debug Menu (Compile Master Codes) and this is what comes up:

CODE
Function FOSUserName() As String
On Error GoTo fOSUserName_Err

Dim lngLen As Long, lngX As Long
Dim strUserName As String

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)

If lngX <> 0 Then
FOSUserName = Left$(strUserName, lngLen - 1)
Else
FOSUserName = ""
End If


fOSUserName_Exit:
Exit Function

fOSUserName_Err:
MsgBox Error$
Resume fOSUserName_Exit
End Function


I have no idea what I am doing here.....UGH!!!


--------------------
"I have to stop saying, 'How stupid can you be.' I think people are starting to take it as a challenge."
Go to the top of the page
 
+
Doug Steele
post Sep 26 2011, 04:26 PM
Post #8

UtterAccess VIP
Posts: 18,477
From: St. Catharines, ON (Canada)



Let me repeat Daniel's question, since you still haven't answered it:


--------------------
Doug Steele, Microsoft Access MVP
http://www.accessmvp.com/DJSteele/AccessIndex.html
Co-author: Access Solutions: Tips, Tricks, and Secrets from Microsoft Access MVPs, published by Wiley
Technical Editor: Access 2010 Bible, published by Wiley, Access 2013 Bible, published by Wiley.
Go to the top of the page
 
+
datAdrenaline
post Sep 26 2011, 04:48 PM
Post #9

UtterAccess Editor
Posts: 16,705
From: Northern Virginia, USA



Or in a standard module as Public ...


--------------------
Brent Spaulding | datAdrenaline | Access MVP
It's all very well to tell us to forgive our enemies; our enemies can never hurt us very much. But oh, what about forgiving our friends? - Willa Cather; As always - Pay it Forward!
Go to the top of the page
 
+
datAdrenaline
post Sep 26 2011, 04:52 PM
Post #10

UtterAccess Editor
Posts: 16,705
From: Northern Virginia, USA



>> For the OnClick value behind the SAVE control, I have =[AbbAndFullDescStorage] <<

First off, the AbbAndFullDescStorage() procedure should be declared as a FUNCTION. If you keep it a Sub, IIRC, there is a bug that will call the code twice.

CODE
Public Function AbbAndFullDescStorage() As Byte  'I like to return an explicit type, even if the result is not used


Now, in your OnClick property you will want to specify the call to a function in this manner:

CODE
=AbbAndFullDescStorage()


Making those changes should get you passed the initial error.

----

Also, I noticed in your code you have:

CODE
UserName = GetUserName()


BUT ... in the code that did not compile, your function is named ...
CODE
FOSUserName()


So .... is there a chance that you have the core code in your database twice? or possibly the api declaration in your code twice? If so, you will get a compile message indicating amibiguity.


--------------------
Brent Spaulding | datAdrenaline | Access MVP
It's all very well to tell us to forgive our enemies; our enemies can never hurt us very much. But oh, what about forgiving our friends? - Willa Cather; As always - Pay it Forward!
Go to the top of the page
 
+
corrieann
post Sep 26 2011, 05:38 PM
Post #11

UtterAccess Guru
Posts: 569



datAdrenaline, you are my hero!
I made the changes to the AbbAndFullDescStorage procedure by replacing "Public Sub" with "Public Function" and adding "As Byte". Then I changed the OnClick Event from =[AbbAndFullDescStorage] to =AbbAndFullDescStorage(). It all works perfectly.

As a bonus, you were right that I had two similar fucntions. One called FOSUserName and one called GetUserName. It seems that ages ago, as I was trying out different suggested solutions to my issue, I never deleted the old module (FOSUserName). I just now deleted this module and ran the script behind the SAVE control. It works just fine at capturing the user name because it is calling on the GetUserName function, which was clean (duh).

Thank you a million times over for breaking out and explaining this to me in English---and for going the extra mile to catch the ambiguous code!!


--------------------
"I have to stop saying, 'How stupid can you be.' I think people are starting to take it as a challenge."
Go to the top of the page
 
+
datAdrenaline
post Sep 27 2011, 08:38 AM
Post #12

UtterAccess Editor
Posts: 16,705
From: Northern Virginia, USA



You're welcome! I am glad to hear that you got things sorted! Good luck on your project! thumbup.gif


--------------------
Brent Spaulding | datAdrenaline | Access MVP
It's all very well to tell us to forgive our enemies; our enemies can never hurt us very much. But oh, what about forgiving our friends? - Willa Cather; As always - Pay it Forward!
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Search  ·  Go to Top  ·  Lo-Fi Version Time is now: 29th July 2014 - 07:44 AM