Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sportsguy
Starting Member
39 Posts |
Posted - 2012-12-05 : 15:19:08
|
The original Excel VBA code retrieved data from MS Access just fine.I adjusted the connection string, and I am connected to SQL Server 2012, I think, no errors!however the SQL query does not return any records, and so there must be a trick to writing SQL queries in VBA to return records.Any suggestions on how to write the query to return field strDISTRICTS from table dbo.DISTRICTS?Thanks in advance - sportsguyHere is the code:Option ExplicitPublic Const MyConn As String = _ "Provider=SQLNCLI11;" & _ "SERVER=servername;DATABASE=databasename;" & _ "Trusted_Connection=yes"Public mySQL As String Public Cn As ADODB.ConnectionPublic Rs As ADODB.RecordsetPublic Rw As LongPublic Col As LongPublic c As LongPublic MyField As ObjectPublic Destination As RangePublic OperationID As RangePublic FADescription As RangePublic Sub Retrieve_Erosion(ByVal BeginPeriod As Long, ByVal EndPeriod As Long, ByVal FADescription As String)On Error GoTo Err_Retrieve strSQL = "SELECT dbo.DISTRICTS.strDistrict FROM dbo.DISTRICTS;"Debug.Print "SQL Statement reads " & strSQL 'Clear Prior Data Sheets("DATA").Select Range("F8").Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.ClearContents Range("F8").Select Debug.Print "Retrieve Begin " & BeginPeriod Debug.Print "Retrieve End " & EndPeriod Debug.Print "Retrieve Family " & FADescriptionApplication.Calculation = xlCalculationManual 'Set destination Set Destination = [DATA!F8] 'Create RecordSet Set Rs = New ADODB.Recordset Rs.Open strSQL, MyConn, adOpenForwardOnly, adLockReadOnly, adCmdText Rw = Destination.Row Col = Destination.Column c = Col Do Until Rs.EOF For Each MyField In Rs.Fields Cells(Rw, c) = MyField c = c + 1 Next MyField Rs.MoveNext Rw = Rw + 1 c = Col Loop Set Rs = Nothing Application.CalculateFullRebuild Application.Calculation = xlCalculationAutomatic ThisWorkbook.Save MsgBox "Data Imported and Spreadsheet Saved!", vbExclamation, "MYCOMPANY Financial Planning and Analysis" Exit_Retrieve: Exit Sub Err_Retrieve: Debug.Print Err.Number & " - " & Err.Description Resume Exit_RetrieveEnd Sub MS Access 20 years, SQL hack |
|
sportsguy
Starting Member
39 Posts |
Posted - 2012-12-05 : 16:58:47
|
I connect, but the server can't understand the query.strSQL = "SELECT * FROM [dbo].[DISTRICTS]"-2147217865 - Invalid object name 'dbo.DISTRICTS'.-2147217865 - Invalid object name 'dbo_DISTRICTS'.-2147217865 - Invalid object name 'DISTRICTS'.any one know how to write a query through VBA which SQLServer 2012 will understand?ugh!MS Access 20 years, SQL hack |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
sportsguy
Starting Member
39 Posts |
Posted - 2012-12-07 : 07:40:19
|
Good morning tarayep, tried that, still didn't work. . . i am not sure if its that the recordset isn't posting to excel or that the records aren't being returned at all. . .however, the DSN connection string should handle that, after not looking at it for a couple of days, i think that my connection and connection string isn't correct. . . .back to the drawing board.MS Access 20 years, SQL hack |
|
|
sportsguy
Starting Member
39 Posts |
Posted - 2012-12-07 : 14:06:27
|
Got it work, it was the connection string not being initialized!For any Newbies, here is the VBA code and trix to retrieve SQLSERVER data to excelRequired Trix:1) copy working sql code from mgmt studio, do not copy from MS Access2) Excel 2007 excel VBA references are microsoft ADO ext 6.0 microsoft ActiveX data objects 6.0Option ExplicitPublic rngPeriod As BytePublic rngYear As IntegerPublic Const myConn As String = "Provider=SQLNCLI11;" & _ "SERVER=servername;DATABASE=databasename;Trusted_Connection=Yes;"Public BeginPeriod As LongPublic strSQL As StringPublic Cn As ADODB.ConnectionPublic Rs As ADODB.RecordsetPublic Rw As LongPublic Col As LongPublic c As LongPublic MyField As ObjectPublic Destination As RangeSub Retrieve_Finished(BeginPeriod As Long)On Error GoTo Err_Retrieve_FinishedstrSQL = "SELECT * FROM * WHERE * ORDER BY *" Application.Calculation = xlCalculationManual'Clear prior data in range Sheets("FINISHED").Select Range("A8").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select Selection.ClearContents Set Destination = [FINISHED!A8]'Begin Connection and data retrieval Set Rs = New ADODB.Recordset Set Cn = New ADODB.Connection Cn.Open myConn Rs.Open strSQL, Cn, adOpenForwardOnly, adLockReadOnly, adCmdText Rw = Destination.Row Col = Destination.Column c = Col Do Until Rs.EOF For Each MyField In Rs.Fields Cells(Rw, c) = MyField c = c + 1 Next MyField Rs.MoveNext Rw = Rw + 1 c = Col Loop Rs.Close Cn.Close Set Rs = Nothing Set Cn = Nothing Application.CalculateFullRebuild Application.Calculation = xlCalculationAutomatic MsgBox "Finished Data Imported Retrieved!", vbExclamation, "MyCompanyName Financial Planning and Analysis" Exit_Retrieve_Finished: Exit Sub Err_Retrieve_Finished: Set Rs = Nothing Debug.Print Err.Number & " - " & Err.Description Resume Exit_Retrieve_FinishedEnd Sub MS Access 20 years, SQL hack |
|
|
|
|
|
|
|