User Identifier Approach

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

User Identifier Approach

Post by burrina »

It was recently suggested to me that it would be better to use WScriptNetwork than Environ UserName
Is WScrript just for a network environment or is better or what?

User avatar
HansV
Administrator
Posts: 78465
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: User Identifier Approach

Post by HansV »

Using WScript, or the WNetGetUserA Winows API function, has the advantage that the user cannot alter it, unlike Environ("username").
So the former are more secure.
Best wishes,
Hans

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: User Identifier Approach

Post by burrina »

Thanks, that's what I though but wanted to confirm.

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: User Identifier Approach

Post by burrina »

Just to be sure, I modified the code to use this. Is this correct?
CurrentDb.Execute "INSERT INTO tbllogintime (logintime) VALUES ('" & WScriptNetwork("username") & "')", dbFailOnError

User avatar
HansV
Administrator
Posts: 78465
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: User Identifier Approach

Post by HansV »

No, it doesn't work like Environ. WScript is a VBA library, Network is an object in that library, and UserName is a method of that object. You have to create the WScript.NetWork object:

CurrentDb.Execute "INSERT INTO tbllogintime (logintime) VALUES ('" & CreateObject("WScript.Network").UserName & "')", dbFailOnError
Best wishes,
Hans

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: User Identifier Approach

Post by burrina »

Ok, I understand now.
Thanks,