Solved

Recognize PC Name in VB6

Posted on 2013-01-04
5
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 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