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.
Printable View
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.
G'day,
Where/when/how did you name your computer, and what system are you running?
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
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
Wow! I like this forum. :bigyello:
Your reply so fast that I still cant belived it!
@Supremus
Does it really matters to know Where/when/how I name the computer??
Anyway, I'm using WinXP home.
@sciguyryan & @THEROB
Thanks. I will go through you code now.
Thanks again for that fast response. :thumb:
Quote:
Originally Posted by igmeou
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
@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 :SQuote:
Originally Posted by igmeou
And simple, just click the "edit" (http://www.vbforums.com/images/buttons/edit.gif) button by your frst post and edit the information :)
Cheers,
RyanJ
Since I'm already a little off track must well I ask 1 more question.
What doesdo due to I don't see anything happen with I used it?Code:Debug.Print
Welcome to the forums.
Here is another wayVB Code:
Private Sub Command1_Click() MsgBox Environ("computername") End Sub
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
Thank you guys! I should learn to closed this thread now! :wave: