If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
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, 07:13
JezLisle JezLisle is offline
Registered User
 
Join Date: Apr 2007
Location: Stalybridge, Manchester
Posts: 273
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, 07:18
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
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
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 09-12-08, 07:18
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Also, any reason why you're selecting TOP 100 PERCENT?
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 09-12-08, 07:29
JezLisle JezLisle is offline
Registered User
 
Join Date: Apr 2007
Location: Stalybridge, Manchester
Posts: 273
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, 07:33
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Think about it; top 100% is the whole resultset!

TOP 1 limits it to a single record...
__________________
George
Twitter | Blog
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

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