Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 9th, 2004, 01:55 PM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default Finding out the computer name

What code can I use in VBA to find out the computer name on which the application is running?

[LGuzman]
__________________
[LGuzman]
 
Old July 9th, 2004, 03:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Option Compare Database

Private Declare Function api_GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
                        (ByVal lpBuffer As String, nSize As Long) As Long

Public Sub atCName()
    On Error Resume Next

    Dim NBuffer As String
    Dim Buffsize As Long
    Dim Wok As Long
    Dim strComputerName As String

    Buffsize = 256
    NBuffer = Space$(Buffsize)

    Wok = api_GetComputerName(NBuffer, Buffsize)
    strComputerName = Trim$(NBuffer)

    Debug.Print strComputerName

End Sub

HTH,

Bob

 
Old July 12th, 2004, 12:56 PM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default

Hi Bob.

Thanks for your answer. However I am unable to get it to work. Where exactly do I put the code you provided me? In the Main Module or within the code that can be activated by a Command Button? I tried both places but it just doesn't seem to work.

Thanks again.

LG

[LGuzman]
 
Old July 12th, 2004, 01:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi LG,

Just for the sake of keeping it all in one place, I'll declare everything as private and put it all in a form module with a command button (cmdGetComputerName) and a textbox (txtComputer). Pressing the command button loads my computer name into the text box.

The only restriction on code location is that you can't have public Declare statments in object modules (e.g., forms).

Option Compare Database

Private Declare Function api_GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
                        (ByVal lpBuffer As String, nSize As Long) As Long

Private Sub cmdGetComputerName_Click()
    Dim strComputerName As String

    strComputerName = ComputerName
    Me.txtComputer = (strComputerName)

End Sub

Private Function ComputerName() As String
    On Error Resume Next

    Dim NBuffer As String
    Dim Buffsize As Long
    Dim Wok As Long
    Dim strComputerName As String

    Buffsize = 256
    NBuffer = Space$(Buffsize)

    Wok = api_GetComputerName(NBuffer, Buffsize)
    strComputerName = Trim$(NBuffer)

    ComputerName = (strComputerName)

End Function


What type of error message, if any, do you get?

Bob



 
Old July 12th, 2004, 02:16 PM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default

Hi Bob.

I'm getting: "The expression On Open you entered as the event property setting produced the following error: Only comments may appear after End Sub, End Function, or End Property."

I think the problem is with the following part:

Option Compare Database

Private Declare Function api_GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
                        (ByVal lpBuffer As String, nSize As Long) As Long.

It doesn't seem to get a section for it self. It remains in the previous section under the End Sub line.

LG

[LGuzman]
 
Old July 12th, 2004, 04:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi LG,

The Declare statement should be placed in your module's General Declaration section.

The first two lines of your module are probably:

Option Compare Database
Option Explicit

Place the Declare statement immediately after these two lines and before any other code.

Does that get it? Also, watch out for duplicate End Sub statements when pasting.

Bob





 
Old July 13th, 2004, 08:09 AM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default

Thanks for your guidance Bob. It seems to be working much better now, except that in the text box I don't seem to get the computer name. I get "True". It should be "MtlAccounts".

Thanks again for your help.


LG

[LGuzman]
 
Old July 14th, 2004, 01:27 PM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default

Bob,

Thank you very much for all your help. It works now. I found my mistake. I had entered:

             ComputerName = strComputerName = strComputerName

when it should had been

                       ComputerName = strComputerName

ComputerName is the name of the text box I use to display the computer name.

Once again thank you.


LG

[LGuzman]
 
Old July 14th, 2004, 02:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

My pleasure, LG. Glad you got it going OK.

Bob






Similar Threads
Thread Thread Starter Forum Replies Last Post
Ping another computer Phreaky Intro Programming 2 January 23rd, 2006 01:31 PM
Ping another computer Phreaky .NET Web Services 0 January 18th, 2006 11:08 AM
how do i get computer name shankhan Javascript How-To 0 May 28th, 2005 03:43 PM
computer architecture mole384 C++ Programming 1 February 2nd, 2005 08:24 AM
Ping another computer nvillare .NET Web Services 0 November 30th, 2003 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.