Solved

Recognize PC Name in VB6

Posted on 2013-01-04
5
273 Views
Last Modified: 2013-01-05
I have an app that was written in vb6 years ago.  I use the app on several different machines and I am wondering if there is a way that I can get it to recognize the name of the pc so I can access the sql server 2008 database regardless of which machine I am running it on (ie: "\PCName\SQLExpress".  Or better yet, get at the local db without making a machine reference at all.

Thanks~
0
Comment
Question by:Bob Schneider
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38743739
This works in Access 2010 VBA.

'Add the below API call to the general declarations section of any code module

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

'Then add this to call the API function.  Modify the naming convention to suit your needs.

Dim str_computer_name As String, lng_buffer_size As Long, lng_ret_code As Long, lng_null_character_position As Long

str_computer_name = Space(80)
lng_buffer_size = Len(str_computer_name)

lng_ret_code = GetComputerName(str_computer_name, lng_buffer_size)

lng_null_character_position = InStr(str_computer_name, Chr(0))
If lng_null_character_position > 0 Then
    str_computer_name = Left(str_computer_name, lng_null_character_position - 1)
Else
    str_computer_name = " "
End If
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 38744659
Can you just put the SQL DB on a network drive and have everyone access the same database?
0
 

Author Comment

by:Bob Schneider
ID: 38745337
We use the software in remote locations that are not on a network.  I will look at the previous solution.  It looks like it might work.  The only way to do something like "App.Path" would be to put the db in the same directory as the compiled application, right?
0
 
LVL 9

Accepted Solution

by:
shorvath earned 500 total points
ID: 38745871
With VB6 you could use SQL DMO
The code below will show you all the installed SQL instances of SQL Server
on the PC (regardless if the server is running or not)

It will also list the PC name as part of the servername.

'Set References to Include Microsoft SQLDMO Object Library
Dim oSQL As SQLDMO.SQLServer2
Dim oDB As SQLDMO.Database2
Dim oNames  As SQLDMO.NameList

Set oSQL = New SQLDMO.SQLServer2
Set oNames = oSQL.ListInstalledInstances

For Each oName In oNames
    Debug.Print oName
Next

Open in new window


You can the make the connection if the server exists and is online.
0
 

Author Closing Comment

by:Bob Schneider
ID: 38747083
Great!  Thanks!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now