Results 1 to 12 of 12

Thread: Easilest Way to get computer name [Resolved]

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Wink 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.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: Easilest Way to get computer name

    G'day,


    Where/when/how did you name your computer, and what system are you running?

  3. #3
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: Easilest Way to get computer name

    This is the way I do it:
    VB Code:
    1. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print CurrentMachineName
    5. End Sub
    6. Private Function CurrentMachineName() As String
    7.    Dim Buffer As String
    8.    Dim nLen As Long
    9.    Const CNLEN = 15          ' Maximum computer name length
    10.    
    11.    Buffer = Space(CNLEN + 1)
    12.    nLen = Len(Buffer)
    13.    If GetComputerName(Buffer, nLen) Then
    14.       CurrentMachineName = Left$(Buffer, nLen)
    15.    End If
    16. 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]

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Easilest Way to get computer name

    Try this:

    VB Code:
    1. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal sBuffer As String, lSize As Long) As Long
    2.  
    3. Private Function NameOfPC(strMachineName As String) As Long
    4.     Dim lngNameSize As Long
    5.     Dim lngX As Long
    6.     strMachineName = Space$(16)
    7.     lngNameSize = Len(strMachineName)
    8.     lngX = GetComputerName(strMachineName, lngNameSize)
    9. End Function
    10.  
    11. Dim strPCName As String
    12. Dim lngP As Long
    13. lngP = NameOfPC(strPCName)
    14. MsgBox (strPCName)

    The above works fine for me

    Cheers,

    RyanJ
    Last edited by sciguyryan; Apr 11th, 2005 at 05:31 AM.
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: Easilest Way to get computer name

    Wow! I like this forum.
    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.
    Will the laziest programmers produced you the easliest way to code?

  6. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Easilest Way to get computer name

    Quote 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
    My Blog.

    Ryan Jones.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    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?

  8. #8
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Easilest Way to get computer name

    Quote 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
    My Blog.

    Ryan Jones.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: Easilest Way to get computer name

    Since I'm already a little off track must well I ask 1 more question.

    What does
    Code:
    Debug.Print
    do due to I don't see anything happen with I used it?
    Will the laziest programmers produced you the easliest way to code?

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Easilest Way to get computer name

    Welcome to the forums.

    Here is another way
    VB Code:
    1. Private Sub Command1_Click()
    2. MsgBox Environ("computername")
    3. End Sub

  11. #11
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    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]

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    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
  •  



Click Here to Expand Forum to Full Width