Full Version: can I use generated parameters directly in a table?
UtterAccess Discussion Forums > Microsoft® Access > Access Tables + Relationships
tpd15
Hi

I have a start-up form that grabs the user's WindowsID and puts it into a global variable. That is then used to track what they do around the database. Here's the code:

CODE
Global strUserName As String

Public Function GetUserPrivilegeLevel() As Variant
On Error GoTo Err_GetUserPrivilegeLevel

strUserName = Environ("Username")
etc..

I'm still learning how to do all this, and I probably should make it so that each time, say, a record is created, then the form where it's created imprints the value of strUserName into the "CreatedBy" field of my table.

But up to now I've been using a different system. In the table I have the default value for "CreatedBy" set to =Environ("Username") so that the info is grabbed without any problems. It works fine, but techincally isn't correct as I should be using strUserName.

If I change the field's default value to =strUserName() then it faults out with "Unknown Function in 'strUserName'...".

Can I make the table pick up this value, or MUST I do this at the form level?
(The reason I ask is that eventually, strUserName may be grabbed from a login form and may no longer be the pc's current Username).
fkegley
I think you're going to have to do this at the form level. I just developed a custom function to return the value of a global variable, however, the default value property of the table didn't recognize the function.
ScottGem
First I would use the function found here instead of Environ(). Environ is too easily spoofed.

However, if you anticipate using a different login ID for your app then the network login, then you will have to assign that value to strUsername. The reason your code doesn't work, is that strUsername is a variable, not a function. I don't think you can set the default value on the table level to a global variable, so you have to do it on the form. Just user =strusername. If that doesn't work, use a function like:

Public Function GetUsername() As String
GetUsername = strUsername
End Function

Then you can use = GetUsername() as your default, but still on the form level.
tpd15
Hi Scott,

Funnily enough, I used to use that API, then I learned of the Environ call from *you* in this thread. wink.gif But I wasn't aware that it had any shortcomings, some of which are mentioned here.

I really liked the Environ call because it was simpler that the API, but I can easily change back. That said, security is not an issue. This is a very limited release thing and no-one is really gonna try to hack it. I just use the call as a convenience so that the user doesn't have to key-in their username.

I seem to recall that back then I was able to use the API call in the table / field's default value, but I just tried it and it doesn't work. Maybe I was only able to use it at the form level....?

The next step, if the dbase gets more widely used, will be to force a login & password, in which case the user will key in the username (which will get parsed directly into strUserName).

As soon as you said I'd been using a variable, not a function, I thought up the code you posted that returns the vaule into a function, so I must be learning. sad.gif

OK, I think I'm good here. Will do it at form level.
ScottGem
Glad to assist
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.