Full Version: default value of text box not working anymore
UtterAccess Forums > Microsoft® Access > Access Forms
wideawake
I have an Access 2000 DB. The data entry form has a bound object on it (text box) named loggedinby. The default value for this object is Environ("UserName"). When a user starts to data enter a new record, this field automatically populates with the Windows ID (whom ever is logged onto the PC) of the user. Works great.
My company is upgrading to Access 2003, and in testing this, the value of this object is now #Name?, instead of the Windows logon ID of the user. I have verified that the References checked in the MS VB window are the same on this test PC as they are on the user's PCs.
Why do you suppose this isn't working anymore?
cheekybuddha
Hi there,
irst try putting in the default value:

=Environ("UserName")
If that doesn't work then remove it completely and in the OnCurrent event of your form put:
CODE
    If Me.NewRecord Then
        Me.txtYourTextBox = Environ("UserName")
    End If

hth,
d
wideawake
Hi,
Just want to clarify this, are you saying that by simply adding an = before Environ("UserName") will fix my problem? I didn't need the = for it to work properly in Access 2000, but maybe it is now needed in Access 2003?
cheekybuddha
Yes, that's what I'm saying - but no guarantees that it will work. If it doesn't then you will have to go with the other method.
I'm slightly confused as to this discrepancy in why they work differently in the different versions.
In A2003 I *think* that the Default Value property of a control expects a Value and might not be able to deal with a function. I haven't got Access open at the moment to test.
Maybe someone reading will give a more definitive answer.
hth,
d
wideawake
Alrighty, I'll give that a try. I was thinking Microsoft may have changed the Environment Variables from 2000 to 2003, and maybe "Environ" wasn't correct to use anymore in 2003. But I couldn't find anything in the Access 2003 Help file on Environment Variables...
cheekybuddha
OK, that's weird.
just tested with an A2003 db and I simply put 'Environ("UserName")' as the default value of a textbox (ie what you had originally - and it worked fine shrug.gif
(It worked with '=Environ("UserName")' too.)
So, I'm not sure what is causing your problem.
d
wideawake
Do you think I may need another or a different reference checked in VB? Right now, I have four checked, VB for Applications, MS Access 9.0 Object Library (11.0 on the test PC), OLE Automation, and MS ActiveX Data Objects 2.1 Library. Can you maybe check to see which ones are checked on your PC?
cheekybuddha
They seem fine. I have the same.
Try opening the VBA editor (Alt + F11). In the Immediate window (Ctrl + G if it's not already visible) type:
?Environ("UserName")
Do you get the result you expect when you hit enter?
d
schroep
Cheeky -
Just a note; if you put that in OnCurrent, it will overwrite every record, new or not. I think you would want to only run that in BeforeInsert on the form.
schroep
Are any of your references marked as MISSING on the machine where this is not working? Can you do a compile in the VBE without any errors?
cheekybuddha
Peter,
ill OnCurrent not work if I tested for a new record?
However, BeforeInsert would be a better choice, no need to test then.
d
cheekybuddha
Having said that, if you use BeforeInsert would it not leave an empty textbox when you come to a new record?
d
schroep
Hmm. I thought I had replied, but I don't see my post. I'll try again.
Yes, if you are checking for NewRecord in Current, it would work, but that comparison would now have to run for each and every record, whereas BeforeInsert would not.
You are correct that the box would initially be empty using BeforeInsert, until the user typed something in to one of the other fields (thus creating the record). However, doing it in Current would mean the instant a new record was navigated to, a record would physically be inserted, since a field just got populated. This could be an issue if the user didn't mean to create a new record.
Obviously, the best solution would be to get the Default Value working again. Even though Environ is not the recommended way to pull the current user's login name (easy to spoof and such), it should work, so something is going on.
cheekybuddha
>
Oquite agree - better to get to the root of that problem rather than worrying about workarounds!
wink.gif
d
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.