Server Prinicipal Security Error

RunTime91

Active Member
Joined
Aug 30, 2012
Messages
290
Office Version
  1. 365
Greetings
I am trying to extract data from SQL Server using Excel VBA.
Below is the code which errors out @ the Rs.Open StrSQL, Con
Code:
Sub DbConnection()
Dim Con As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Fld As ADODB.Field
Dim StrSQL As String
Dim Wb As ThisWorkbook
Dim Ws As Worksheet

Dim StrCon As String
Set Wb = ThisWorkbook
Set Ws = Wb.Sheets("Sheet2")
Set Con = New ADODB.Connection
Set Rs = New ADODB.Recordset
StrCon = "ODBC;Data Source=ABCD23NNP_LIST\SQL2014,1433;DSN=RAOC_SQLTST;APP=Microsoft Excel;DATABASE=RAOCStatsTrain;UID=;PWD=;Trusted_Connection=yes;"

StrSQL = "SELECT [ReportDate] ,[WorkType] ,[TotalCases]" & _
         "FROM [RAOCStats].[tri].[tblCRTimeliness]" & _
          "WHERE BatchID >= '20181219_050000' AND BatchID <= '20181219_051500'"


'''''StrCon = "ODBC;Data Source=AGUTIL14P_LIST\SQL2014,1433;DSN=RAOC_SQLPRD;DATABASE=RAOCStats;UID=;PWD=;Trusted_Connection=yes;"
Con.ConnectionString = StrCon
Con.Open
Rs.Open StrSQL, Con<-----Errors on this line

As far as having any security issues -
I have admin rights and opening and connecting through either Excel or Access work fine.
I can run SQL queries all day from Access but for this project I need to use Excel

Thanks for any help you can provide.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
First, it looks space is necessary to be used before FROM and WHERE.

Code:
strSQL = "SELECT [ReportDate] ,[WorkType] ,[TotalCases]" & _
         " FROM [RAOCStats].[tri].[tblCRTimeliness]" & _
         " WHERE BatchID >= '20181219_050000' AND BatchID <= '20181219_051500'"

or for better readability I would put spaces in previous lines:

Code:
strSQL = "SELECT [ReportDate] ,[WorkType] ,[TotalCases] " & _
         "FROM [RAOCStats].[tri].[tblCRTimeliness] " & _
         "WHERE BatchID >= '20181219_050000' AND BatchID <= '20181219_051500'"

However, even this is fixed, BatchID is trying to be compared numerically with string values in WHERE.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top