Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Computer Name using VBA 2

Status
Not open for further replies.

mlstewart

Instructor
Aug 20, 2004
58
US
I've got a database where I want to extract the Computer Name of the computer the database is being used on. I am looking for some VBA code that will do this. Any help on this will be greatly appreciated.

Thanks,

ML
 
I looked at the thread you posted where you wrote:

"Try the Environment variables:
Msgbox VBA.Environ("COMPUTERNAME")"

but I am new to the VBA side of Access and I don't know where to put it or what to do with it.

Thanks for your help.
ML
 
If you are that new, you should be informed that it has nothing to do with Access, specifically.

Essentially, the line you asked about is a line of code. Code is written into procedures, and procedures into modules. You look at the modules, and write procedures in the VBE - Alt-F11.

You should try Help. Record a macro and find it in the Visual Basic Editor. You are going to have to start there. Look at your macro and learn how to make it fire.

Gerry
 
This is what I want to do.

My database opens up to the "Main Form" and there will be a command button on that form that, when clicked on, will tell the user what the computer name is. For example, if my computer's name is "Office Computer" then when I click on the command button I want it to display "Your computer name is Office Computer." on a small pop-up. How difficult will it be to do this?

I know how to set up the forms and command buttons. I just don't know how to get the computer name.

Thanks for your help.
ML

 
You would likely place this code:

[tt]Msgbox VBA.Environ("COMPUTERNAME")[/tt]

in your CommandButton's click event.

*cLFlaVA
----------------------------
When will I be able to see what other members can see about myself? It's been like, a freakin' month already!
 
If you
know how to set up the forms and command buttons. I just don't know how to get the computer name.

then you should know how to put in a single line of code. "Setting up" a command button has as its primary event, the Click event. Which is why cLFaVa informed you that, well, it should go in the Click event.

Study some of the FAQ on this forum.

For example, you do not need the result of computername to take up resources (and require the user to click OK on the message box). You could simply GET the computername and display it on a label on the form.

In other words, as the forms loads, it automatically gets the user's computername - with NO action required from the user, they do not have to click a command button. That name could be automatically written to a label (not a textbox) with your statement ""Your computer name is Office Computer BW56TH8900", or whatever.

Although I think I would state "You are logged in as (login username, using the computer computer name.

In that case, assuming a Lable named Label1 (and remember this is just a snippet of code, you may need much more stuff in here). If you do not know what the difference between a label anda textbox, look up Help. if you are just displaying text, use labels. The label.caption is what is displayed, and it can be changed whenever, and to whatever you want, on the fly. Just use Labelname.Caption = (the string you want it to be). Like this below.

Code:
UserForm_Initialize()
Label1.Caption = "You are logged in as " & ENVIRON("UserName") & _
", using the computer " & ENVIRON("ComputerName") & "."

This display the information, with no action needd by the user.

Gerry
 
I really appreciate the suggestion fumei. I didn't use in this situation because there was a reason for using the command button to get the computer name. However, I did use your suggestion to perform different task. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top