|
-
Apr 11th, 2005, 04:51 AM
#1
Thread Starter
New Member
Easilest Way to get computer name [Resolved]
Hi, this is my 1st post here and I'm new to VB.
I would like to know is there an macro or code in VB that can get your own Computer Name at a go?
If not what is the simpliest way to include your computer name in VB?
Thanks.
Last edited by igmeou; Apr 11th, 2005 at 05:47 AM.
-
Apr 11th, 2005, 04:55 AM
#2
Hyperactive Member
Re: Easilest Way to get computer name
G'day,
Where/when/how did you name your computer, and what system are you running?
-
Apr 11th, 2005, 04:56 AM
#3
Fanatic Member
Re: Easilest Way to get computer name
This is the way I do it:
VB Code:
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Debug.Print CurrentMachineName
End Sub
Private Function CurrentMachineName() As String
Dim Buffer As String
Dim nLen As Long
Const CNLEN = 15 ' Maximum computer name length
Buffer = Space(CNLEN + 1)
nLen = Len(Buffer)
If GetComputerName(Buffer, nLen) Then
CurrentMachineName = Left$(Buffer, nLen)
End If
End Function
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
-
Apr 11th, 2005, 04:57 AM
#4
Re: Easilest Way to get computer name
Try this:
VB Code:
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal sBuffer As String, lSize As Long) As Long
Private Function NameOfPC(strMachineName As String) As Long
Dim lngNameSize As Long
Dim lngX As Long
strMachineName = Space$(16)
lngNameSize = Len(strMachineName)
lngX = GetComputerName(strMachineName, lngNameSize)
End Function
Dim strPCName As String
Dim lngP As Long
lngP = NameOfPC(strPCName)
MsgBox (strPCName)
The above works fine for me 
Cheers,
RyanJ
Last edited by sciguyryan; Apr 11th, 2005 at 05:31 AM.
-
Apr 11th, 2005, 05:06 AM
#5
Thread Starter
New Member
-
Apr 11th, 2005, 05:10 AM
#6
Re: Easilest Way to get computer name
 Originally Posted by igmeou
Does it really matters to know Where/when/how I name the computer??
Anyway, I'm using WinXP home.
Not really as these use API calles they should work on most computers - I use Win 98 and it works for mme so it should work with all Operating Systems above that 
Cheers,
RyanJ
-
Apr 11th, 2005, 05:24 AM
#7
Thread Starter
New Member
Re: Easilest Way to get computer name
@sciguyryan
I can't actually get your code to compile. It gives me a complie error.
Therefore, I decide to used the one given by THEROB and modify a little to suit my program.
Another thing, how do I close this post with the [resolved] word like others?
Will the laziest programmers produced you the easliest way to code? 
-
Apr 11th, 2005, 05:29 AM
#8
Re: Easilest Way to get computer name
 Originally Posted by igmeou
@sciguyryan
I can't actually get your code to compile. It gives me a complie error.
Therefore, I decide to used the one given by THEROB and modify a little to suit my program.
Another thing, how do I close this post with the [resolved] word like others?
Interesting - the code works for me :S
And simple, just click the "edit" ( ) button by your frst post and edit the information 
Cheers,
RyanJ
-
Apr 11th, 2005, 05:34 AM
#9
Thread Starter
New Member
Re: Easilest Way to get computer name
Since I'm already a little off track must well I ask 1 more question.
What does do due to I don't see anything happen with I used it?
Will the laziest programmers produced you the easliest way to code? 
-
Apr 11th, 2005, 05:34 AM
#10
Re: Easilest Way to get computer name
Welcome to the forums.
Here is another way
VB Code:
Private Sub Command1_Click()
MsgBox Environ("computername")
End Sub
-
Apr 11th, 2005, 05:36 AM
#11
Fanatic Member
Re: Easilest Way to get computer name
Dubug.print prints a string to the immediate window (press Ctrl + G) to see it.
It is very handy to see variables during code.
R
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
-
Apr 11th, 2005, 05:47 AM
#12
Thread Starter
New Member
Re: Easilest Way to get computer name
Thank you guys! I should learn to closed this thread now!
Will the laziest programmers produced you the easliest way to code? 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|