Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Connection From Excel

Status
Not open for further replies.

nirs

IS-IT--Management
Apr 4, 2003
37
IL
hi

can i open connection from excel to a sql server
without using ODBC ?
i mean using IP address
 
Using a DSN less ADO connection its possible to connect to SQL Server from Excel (or other Office applications).
Something like the following will do:

Code:
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.RecordSet
Dim cmd As New ADODB.Command
 
Dim strConnect As String
 
strConnect = "Provider=sqloledb;Data Source=server name or IP;Network Library=DBMSSOCN;Initial Catalog=databasename;User ID=username;Password=pwd;App=YourAppname"
 
cnn.Open strConnect
 
cmd.CommandText = "select * from mytable"
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdUnknown
Set rst = cmd.Execute
 
Do While Not rst.EOF
  ' or do whatever you want with the data in here
  Debug.Print rst.Fields(0).Value
  rst.MoveNext
Loop
 
rst.Close
cnn.Close

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top