Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > PC based Database Applications > Microsoft Excel > ADO Connection from Excel to SQL Server

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-12-08, 08:13
JezLisle JezLisle is offline
Registered User
 
Join Date: Apr 2007
Location: Stalybridge, Manchester
Posts: 240
ADO Connection from Excel to SQL Server

I am trying to connect via ADO to an SQL Server database, the code below is what I'm using as the connection but keep getting an error message...

The following error has occured-
[Microsoft][ODBC SQL Server Driver][SQL Server] loging failed for user 'myusername'

I dont understand why this would be, I have created an ODBC Driver. The only difference is that the Username and Password are taken from the authenticity of the Network Login, but I have used these when putting in the code. I dont know where am going wrong...

Code:
Sub GetData() On Error GoTo Err: strDWFilePath = "Driver={SQL Server};Server=SQL1;Database=INFO;User Id=mynetworkusername;Password=mynetworkpassword" Set cnnDW = New ADODB.Connection Set rsDW = New ADODB.Recordset Sheet1.Range("B4:C9").ClearContents cnnDW.Open strDWFilePath sQRY = "SELECT TOP (100) PERCENT [Weeks Range Title] AS WeeksWaiting, COUNT(*) AS Count " & _ "FROM jez.PodAll " & _ "GROUP BY [Weeks Range], [Weeks Range Title] " & _ "ORDER BY [Weeks Range]" rsDW.CursorLocation = adUseClient rsDW.Open sQRY, cnnDW, adOpenDynamic, adLockOptimistic, adCmdText Application.ScreenUpdating = False Sheet1.Range("B4").CopyFromRecordset rsDW rsDW.Close Set rsDW = Nothing cnnDW.Close Set cnnDW = Nothing Exit Sub Err: MsgBox "The following error has occured-" & vbCrLf & vbCrLf & VBA.Error, vbCritical, "SQL Connection" MsgBox VBA.Err End Sub
Reply With Quote
  #2 (permalink)  
Old 09-12-08, 08:18
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,132
Sounds to me like you're using a trusted logon (Windows Auth) yet supplying the username and password - which is a little contradictory.

Try using the trusted connection connection string here
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #3 (permalink)  
Old 09-12-08, 08:18
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,132
Also, any reason why you're selecting TOP 100 PERCENT?
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #4 (permalink)  
Old 09-12-08, 08:29
JezLisle JezLisle is offline
Registered User
 
Join Date: Apr 2007
Location: Stalybridge, Manchester
Posts: 240
I'm using that so that it will return only 1 value for the Weeks Range Title. Would you have written the SQL in another way?
Reply With Quote
  #5 (permalink)  
Old 09-12-08, 08:33
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,132
Think about it; top 100% is the whole resultset!

TOP 1 limits it to a single record...
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On