set txtbox Value to Environ()

Local time
Today, 00:31
Joined
Jun 27, 2007
Messages
246
i've declared a public function as so
Code:
Public Function GetWinUserName()
GetWinUserName = Environ("UserName")
End Function

thinking that i was clever, i set the control source on my form to =getwinusername, and boy oh boy - i open the form and bam theres my login name. still riding on cloud nine i went to save my record.

After checking the table i realized my idiocy - and changed my control source back to the tablefield it was supposed to be...:o

that mistake acknowledged - ive tried setting the default value to =getuser...
and that fails to display anything when i open the form (even if it somehow records, which i doubt, i'd like users to see the majic)

any ideas?
 
Setting the default value will make it display (and save, if bound) for a new record, but not for existing records.

It looks like the default that you put in was not =GetWinUserName() as you quoted =getuser...

But in reality, creating the function is not really necessary as you can set your default to be

=Environ("username")

by itself.
 
ahhh i was wondering if the pre-existing record was my problem. to get around an earlier issue, i have this form opening a semi-blank record. I was putting in the full function =GetWinUserName(), and understand that it was unnecessary (im gonna stick with the PF, as i'll likely be slathering this Db with the username).

So maybe I'll just make a superficial box which displays the username, and force the field in VBA? Like so: ??

Code:
  With DoCmd
        .RunCommand acCmdSaveRecord
        .RunSQL "UPDATE [tbl Modifications] SET [EntryStatus]=(1) WHERE [keyID]= [txtkey]"
.RunSQL "UPDATE [tbl Modifications] SET [Analyst]= GetWinUserName() WHERE [keyID]= [txtkey]"
        End With
        
With CurrentDb.OpenRecordset("tbl Modifications", dbOpenTable)
        .AddNew
          !EntryStatus = 0
          .Update
End With

DoCmd.Close acForm, Me.Name
 
Nevermind that, it works now. just a relationship snafoo.

Is there any advantage to Pbaldy's technique of keeping the environ() in a txtbox on an open form? ie does it save all that much processing in its avoiding repeat function calls?
 
Depending on how many times you will be calling it. For example, if you had it in a query and it was calling the function for every record, it would be more efficient to call it once and save it to either a public variable or a text box on an open form, so you could just refer to the existing item and didn't have to have it calculate each time, that would be best.

If you are just going to be doing it every so often, then it really isn't going to be all that much of a difference in speed.
 
thanks! i'll prolly keep the invisible reference box, and use a little of this and a little of that, depending.
 

Users who are viewing this thread

Back
Top Bottom