How to get computername

anishkgt

Registered User.
Local time
Today, 02:27
Joined
Nov 4, 2013
Messages
384
Hi All,

How can i get the computername and display it in a text field in ms access 2013.
 
Last edited by a moderator:
Create a function that returns the result of the following expression.
Then use that function as the RecordSource of the text box.

CreateObject("wscript.network").ComputerName
 
Hi

environ("computername")
 
environ("computername")

That returns the computername environment variable which can easily be changed from the default.

Better to return the actual computername.
 
Hey thanks Galaxiom !

Was wondering is this can be in a macro format. Am not a programming guy.
 
I am a programming guy. I tried macros once just to learn about how they work and would never again. Macros are so clumsy.

Using VBA is far simpler than most people expect. Try it, you won't look back.

Just open the VB editor. Add a Standard Module and paste this in:

Code:
Public Function ComputerName()
    LoginName = CreateObject("wscript.network").ComputerName
End Function

Now put this in the textbox in Design mode:
Code:
=ComputerName
 
hi,

Just done something :o and didn't seem to be working out.:( I've attached a screenshot.
 

Attachments

  • vb1.jpg
    vb1.jpg
    53.5 KB · Views: 378
  • vb2.jpg
    vb2.jpg
    49.1 KB · Views: 329
Textbox needs to be:

=ComputerName()
 
Just tried that also. The text box is empty in form view
 
It all functions normally on my work computer.

However at home (Windows7 Home Premium) I get a blank textbox too.

Same result if I use Environ("computername"). Yet the computer definitely has a name and no errors are thrown.:confused:
 
This works for me, please note this is NOT my code.
Code:
Private Declare Function GetComputerName Lib "kernel32" _
         Alias "GetComputerNameA" ( _
         ByVal lpBuffer As String, _
         ByRef nSize As Long) As Long
 
 
Function ComputerName() As String
   Dim stBuff As String * 255
   Dim lAPIResult As Long
   Dim lBuffLen As Long
   lBuffLen = 255
   lAPIResult = GetComputerName(stBuff, lBuffLen)
   If lBuffLen > 0 Then
     ComputerName = Left(stBuff, lBuffLen)
   End If
 End Function

And use =computername()
 
Could it be that the code should be:
Code:
Public Function ComputerName()
    [COLOR="Red"][B]ComputerName [/B][/COLOR]= CreateObject("wscript.network").ComputerName
End Function
 
Thanks bob_fitz. your correction works for me on windows 8
 
Could it be that the code should be:
Code:
Public Function ComputerName()
    [COLOR="Red"][B]ComputerName [/B][/COLOR]= CreateObject("wscript.network").ComputerName
End Function

Doh.

I accidentally copied the LoginName function from my code module then edited it (badly).:o
 

Users who are viewing this thread

Back
Top Bottom