Solved

Recognize PC Name in VB6

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Webservices in T-SQL 3 34
Query group by data in SQL Server - cursor? 3 48
Master DB with Masterkey 1 34
any adverse effect when modifying data type on a busy table. 4 16
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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
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…

829 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