Username & ComputerName With Access2003

J

jay

Hi All,

I've been using the environ("username") and environ("computername")
functions in Access2000 however having migrated to 2003 they now seem to
return #name

Any help in returning the username (i.e. the user logged in to the
workstation) and computername (i.e. the local workstation name) would be
greatly appreciated.

Kind regards,

Jay Hallsworth
 
A

Albert D.Kallal

The environ is not a reliable way, and in a2000 you likely where lucky you
did not have problems.
(this is not really a access issue, but a windows, and how your particular
machine is setup).


You can get the current network logon name with:

http://www.mvps.org/access/api/api0008.htm

And, the current computer name with:

http://www.mvps.org/access/api/api0009.htm

And, if using ms-access security, then currentuser() will return the
ms-access logon.

I often log all 3 of the above values in some applications.
 
G

Guest

I have this exact problem with the username, but using your suggestion
yielded the same results. That is, I still receive #Name? in my name field
on my form.

I created a module as you suggested and called it modfOSUserName, and copied
the code to it.

My vba code in my form looks like this:

Private Sub Form_Open(cancel As Integer)
Me.RequesterName.DefaultValue = fOSUsername()
End Sub

The result is #Name? in the RequesterName field on the form.

I want to populate this field with the username only when a new form is
being created.

Am I on the right track? All help is appreciated.
 
D

Douglas J. Steele

I believe you need quotes around the value:

Private Sub Form_Open(cancel As Integer)
Me.RequesterName.DefaultValue = """" & fOSUsername() & """"
End Sub

or

Private Sub Form_Open(cancel As Integer)
Me.RequesterName.DefaultValue = Chr$(34) & fOSUsername() & Chr$(34)
End Sub
 
A

Albert D.Kallal

I want to populate this field with the username only when a new form is
being created.

Then use the before insert. You don't want to use the default value of a
control, since likely you not going to allow the user to "edit" that
field..right?

So, simply put the code in the before insert event.

Me.RequesterName = fOSUsername()

Further, as a rule, you want to use the on-load event, as the on-open event
is too soon to modify controls, and their values/settings

You could also use the "after" insert event, but then while a user types,
they would not see the "username" field set.

And, the reason why we use the on-insert is that if the user decodes to NOT
type anything, then no data is written, and the user can exit without
accident adding a blank record.

I suppose you could use the "default" value of the control on the form. When
you do this, you do need to provide the correct format, and that means the
default value setting needs "quotes" around the value before you stuff it
into the controls default value (and, you would move that code to the
on-load, since as mentioned the on-open is for canceling a form load).

And, note that you actually put the function name in the controls default
setting, and use no code at all

=fOsUserName()

Just put the above in the controls default setting, and that will work.....

(I think the last choice is the most easy).
 
G

Guest

Albert,

I truly appreciate the help, but you completely confused me. I am not a VB
programmer, and I have no idea what you are saying. I don't know where the
Before Insert event is located, I only see Before Update. And you are
correct in thinking that I don't want the user to edit the name, but how do I
protect the field so it cannot be changed?

Additionally, if the user decides to exit out of the form without saving
anything entered, i.e. changes his/her mind, how is this done? Currently I
have two buttons, Exit and Save Form. If the user intends to save the form,
they should press Save Form. If the user wants to exit without saving the
form, they should press Exit. However, when pressing Exit, a record is
created in the database. How do I handle this situation?

I am not a VB/Access professional, and I do appreciate any and all help.

Kindest regards.
 
A

Albert D.Kallal

I truly appreciate the help, but you completely confused me. I am not a
VB
programmer, and I have no idea what you are saying. I don't know where
the
Before Insert event is located, I only see Before Update. And you are
correct in thinking that I don't want the user to edit the name, but how
do I
protect the field so it cannot be changed?

The before insert only exists for the whole form, not a particular control.
Note that the controls, and even the forms header, and even the "detail"
section of a form has all kinds of properties. If you click on a blank area
in the forms section (in design mode), you have selected the detail section.
To correct see the "forms" properties, then you need to select the form. I
usually with the mouse click on the upper left "square" of the form, but you
can also go edit->select form.

The above is quite easy to mess up, and get confused over. When I talked
about the forms "before insert" event, you must select the "form" as
outlined above to get to these events. As I mentioned, using the before
insert is a good event, since then if the user exits the form, a blank
record will NOT be crated. Do note that if you set the "default" for a
record, then again you should be able to "exit" without a record being
added.
Additionally, if the user decides to exit out of the form without saving
anything entered, i.e. changes his/her mind, how is this done? Currently
I
have two buttons, Exit and Save Form. If the user intends to save the
form,
they should press Save Form. If the user wants to exit without saving the
form, they should press Exit. However, when pressing Exit, a record is
created in the database.

If the user has done nothing, then a blank record should NOT get added. It
is possible that by programmatic setting the default for a control, then you
might "dirty" a record. Remember, when you exit a form, no prompts occur,
and saving is automatic. So, as a general rule, if a user enters some data,
and close the form, then data is saved. However, if the user is on a new
blank record, and does not edit (we call this "dirty") the form, then
closing should not add the blank record (however, code that runs and sets
values can often cause the record to be dirtied, and that means exiting will
write the record).

however, note that I did say in place of trying to use that code in the
form, that you can simply put the function name DIRECTLY into the controls
default value.

that means you select the "username" control (I don't know the name you
used). and then on the properties sheet for that control, you can put the
function name as the default as follows:

I quote from my past post:

<quote>
And, note that you actually put the function name in the controls default
setting, and use no code at all

=fOsUserName()
<quote>

And, did mention that this would be likely the most easy approach here. So,
the above is now talking about NOT using the forms before insert event, but
simply setting the controls default.

Get the above working, and then start the next task of NOT allowing the user
to edit that control....

(hint: you should be able to set the controls "locked" setting on the "data"
tab of the properties sheet)
However, when pressing Exit, a record is
created in the database. How do I handle this situation?

As mentioned, if the user edited the record, then it will be saved for you
do NOT need a save button. You could start writing code to handle this, but
no code at all is really needed here (this issue of save prompts is a
different issue then setting up the default values.

Virtually all commercial databases for the last 20 years don't prompt to
save. If you don't want to save the data, either delete the record, or go
edit->undo.

You can start writing code to override this behavior, but it is work that
generally gives little, or no benefits to your application...
 
G

Guest

Albert,

Thanks for the detailed explanation, I appreciate it.

While I'm very new to VB/Access coding, I have a Mainframe/Midrange
background and I want to be able to control when a record gets updated.

If the user enters data into a form and subsequently changes his/her mind, I
don't want a new record to get created or an existing record to get updated
until they click the Save Form button. If they click the Exit button, a
record should NOT get created or an existing record should NOT get updated.
This doesn't seem like it should be a difficult thing to accomplish...in my
world it's not. How would I go about accomplishing this? Your insight is
appreciated!
 
A

Albert D.Kallal

Gee, my newsreader missed your question....

Your problem here is that you have not thought out how navigation will work.

If a person edits a record, and then moves to the next one, do you prompt
them?

You mean I can hit the exit key, and nothing gets saved? You going to drive
uses nuts here.....

If you want to exit a form without saving, then go

me.undo
docmd.Close

If you want to exit with saving, go

docmd.Close

The above will accomplish what you want. However, the above setup will not
work for sub-forms....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top