Solved

Recognize PC Name in VB6

Posted on 2013-01-04
5
271 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

20 Experts available now in Live!

Get 1:1 Help Now