vba code to retrieve data from sql database

JannetteChristie

Board Regular
Joined
Dec 14, 2015
Messages
119
Hi,

I am getting the following error message and am not sure how to get rid of this.

'Run-time error - 2147467259 (80004005): Method 'CopyFromRecordset' of object range failed.

The VB code is as follows:

Sub GetDataFromSql()


Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset


Dim StrCon As String
Dim StrSQl As String


Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset


StrCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=Reporting;Data Source=dpsql01"
conn.Open StrCon


StrInputName = ActiveSheet.Range("UserNameId")
StrInput1 = Format(ActiveSheet.Range("DateofRecord"), "yyyy-mm-dd")
StrInputDate = "'" & StrInput1 & "'"




StrSQl = StrSQl & "Select isnull(EQFU,0),isnull(QFU,0),isnull(TQFU1,0),isnull(TQFU2,0),isnull(CO,0),isnull(TELES,0),isnull(MEETING,0),"
StrSQl = StrSQl & "isnull(DEMOVAN,0) , isnull(SPEC,0), isnull(AMEND,0), isnull(QUOTES,0), isnull(INBOX,0), isnull(PRICESUPP,0), ISNULL(Arrange,0),"
StrSQl = StrSQl & "isnull(CPD,0),isnull(BONUS,0),isnull(SDQ,0),isnull(TWENTY4HQ,0),isnull(OTHER,0),'',GFI,START_TIME,Convert(Varchar(10),DATE,103),0,Individual_Ommph_ID "
StrSQl = StrSQl & "from Individual_Oomph where User_Name = " & StrInputName
StrSQl = StrSQl & " and Date=" & StrInputDate


Set rs = conn.Execute(StrSQl)


Sheets("Report").Range("B6").CopyFromRecordset rs


rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing


End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hey JannetteChristie,

I am not really sure what the issue is however, are you sure your SQL query is correct? I have noticed that you don't have single quotes for the USER_NAME assuming they're integer? Also, I have noticed a space within your convert function which I don't know if it works like this in SQL [Convert(Varchar(10),DA TE,103)]

Try the below code in your VBA & copy the SQL query from the immediate window & test if it works in your SQL. If it doesn't work then this is your issue, or first issue. Let me know

Code:
Sub Test_SQL()

StrInputName = ActiveSheet.Range("UserNameId")
StrInput1 = Format(ActiveSheet.Range("DateofRecord"), "yyyy-mm-dd")
StrInputDate = "'" & StrInput1 & "'"

StrSQl = StrSQl & "Select isnull(EQFU,0),isnull(QFU,0),isnull(TQFU1,0),isnull(TQFU2,0),isnull(CO,0),isnull(TELES,0),isnull(MEETING,0),"
StrSQl = StrSQl & "isnull(DEMOVAN,0) , isnull(SPEC,0), isnull(AMEND,0), isnull(QUOTES,0), isnull(INBOX,0), isnull(PRICESUPP,0), ISNULL(Arrange,0),"
StrSQl = StrSQl & "isnull(CPD,0),isnull(BONUS,0),isnull(SDQ,0),isnull(TWENTY4HQ,0),isnull(OTHER,0),'',GFI,START_TIME,Convert(Varchar(10),DA TE,103),0,Individual_Ommph_ID "
StrSQl = StrSQl & "from Individual_Oomph where User_Name = " & StrInputName
StrSQl = StrSQl & " and Date=" & StrInputDate

Debug.Print StrSQl

End Sub
 
Upvote 0
Hey JannetteChristie,

I am not really sure what the issue is however, are you sure your SQL query is correct? I have noticed that you don't have single quotes for the USER_NAME assuming they're integer? Also, I have noticed a space within your convert function which I don't know if it works like this in SQL [Convert(Varchar(10),DA TE,103)]

Try the below code in your VBA & copy the SQL query from the immediate window & test if it works in your SQL. If it doesn't work then this is your issue, or first issue. Let me know

Code:
Sub Test_SQL()

StrInputName = ActiveSheet.Range("UserNameId")
StrInput1 = Format(ActiveSheet.Range("DateofRecord"), "yyyy-mm-dd")
StrInputDate = "'" & StrInput1 & "'"

StrSQl = StrSQl & "Select isnull(EQFU,0),isnull(QFU,0),isnull(TQFU1,0),isnull(TQFU2,0),isnull(CO,0),isnull(TELES,0),isnull(MEETING,0),"
StrSQl = StrSQl & "isnull(DEMOVAN,0) , isnull(SPEC,0), isnull(AMEND,0), isnull(QUOTES,0), isnull(INBOX,0), isnull(PRICESUPP,0), ISNULL(Arrange,0),"
StrSQl = StrSQl & "isnull(CPD,0),isnull(BONUS,0),isnull(SDQ,0),isnull(TWENTY4HQ,0),isnull(OTHER,0),'',GFI,START_TIME,Convert(Varchar(10),DA TE,103),0,Individual_Ommph_ID "
StrSQl = StrSQl & "from Individual_Oomph where User_Name = " & StrInputName
StrSQl = StrSQl & " and Date=" & StrInputDate

Debug.Print StrSQl

End Sub

Hi, I am not sure how a space got in between the sql statement when I copied it to the post as there is not one in the code. The code you provided is working ok, when I run my original code the data is being returned and then I am getting the error message.
 
Upvote 0
I have worked out what was causing this to fail, my worksheet is protected and one of the columns needed to be unprotected to allow the data to be written in. Thanks for your time on this.
 
Upvote 0
I have worked out what was causing this to fail, my worksheet is protected and one of the columns needed to be unprotected to allow the data to be written in. Thanks for your time on this.

Glad you figured out what was causing the error :) Sometimes the tiny things that cause us the troubles. Thanks for reporting back
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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