473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Copyfromrecordset returns no data

Hi

Please help me with the code here.

I am getting nothing returned to excel
Expand|Select|Wrap|Line Numbers
  1. ' Open the connection and execute.
  2.     conn.Open sConnString
  3.     Set rs = conn.Execute("SELECT idEmployee FROM table;")
  4.  
  5.     ' Check we have data. and yes in SQL I have 8 rows
  6.     If Not rs.EOF Then
  7.         ' Transfer result.
  8.        MsgBox "We have Data"
  9.  
  10.        MsgBox rs.RecordCount
  11.         ' returns -1
  12.        MsgBox rs.Fields(0).Value
  13.         ' returns 1
  14.        Sheet4.Range("A1").CopyFromRecordset rs
  15.        ' returns no data to sheet
  16.     ' Close the recordset
  17.         rs.Close
  18.     Else
  19.         MsgBox "Error: No records returned.", vbCritical
  20.     End If
Feb 11 '16 #1
6 2454
zmbd
5,501 Expert Mod 4TB
+ Which version of Office/Excel are you are using.

+ We need the connection string
2: conn.Open sConnString

+ Line 6 will most likely evaluate to true because you are at the Beginning of the record set.

+Line 10 will not return an accurate record count until you have moved to the last record of the set.
Feb 11 '16 #2
Hi

Here is the full vba

Expand|Select|Wrap|Line Numbers
  1. ' Create the connection string.
  2.     sConnString = "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDatabase;User ID=ME;Password=Dunno;"
  3.  
  4.     ' Create the Connection and Recordset objects.
  5.     Set Conn = New ADODB.Connection
  6.     Set rs = New ADODB.Recordset
  7.  
  8. ' Open the connection and execute.
  9.     Conn.Open sConnString
  10.     Set rs = Conn.Execute("SELECT idEmployee FROM Table1;")
  11.  
  12.     ' Check we have data. and yes in SQL I have 8 rows
  13.     If Not rs.EOF Then
  14.         ' Test with msgbox EOF
  15.        MsgBox "We have Data"
  16.  
  17.         ' Test the count - returns -1
  18.        MsgBox rs.RecordCount
  19.  
  20.         ' test the Field Value   - returns 1
  21.        MsgBox rs.Fields(0).Value
  22.  
  23.         ' returns no data to sheet
  24.         Sheet4.Range("A1").CopyFromRecordset rs
  25.  
  26.     ' Close the recordset
  27.         rs.Close
  28.     Else
  29.         MsgBox "Error: No records returned.", vbCritical
  30.     End If
  31. Set Conn = Nothing
  32. Set rs = Nothing
  33.  

Excell is 2013
SQL is 2008 R2
Feb 12 '16 #3
NeoPa
32,556 Expert Mod 16PB
Please be more specific about what it is you're asking for help with. We cannot run your code so you need to tell us exactly what does happen and also , preferably, what you expected, so where it differs from expectation. That way we have something to work with. Line numbers are important. Some people think they can explain adequately without any reference to where in the code the problems occur. It doesn't take a lot of thought to realise they're mistaken.
Feb 14 '16 #4
ADezii
8,834 Expert 8TB
It could be the context in which the Code is being executed, try:
Expand|Select|Wrap|Line Numbers
  1. ActiveWorkbook.Worksheets("Sheet4").Range("A1").CopyFromRecordset rs
Feb 14 '16 #5
Hi There

The problem is that There is data in SQL, and that the data is not returned to Excell as per line 23. I am expecting the result set of 8 lines that is in SQL to be returned.

I have also now skipped the test and I am just trying to return data.
Expand|Select|Wrap|Line Numbers
  1. ' Create the connection string.
  2.     sConnString = "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDatabase;User ID=ME;Password=Dunno;"
  3.  
  4.     ' Create the Connection and Recordset objects.
  5.     Set Conn = New ADODB.Connection
  6.     Set rs = New ADODB.Recordset
  7.  
  8. ' Open the connection and execute.
  9.     Conn.Open sConnString
  10.     Set rs = Conn.Execute("SELECT idEmployee FROM Table1;")
  11.  Sheet4.Range("A1").CopyFromRecordset rs
  12.  
The above is stating there is no data. But there is DATA in SQL 100%. Tested the Query again.
Feb 16 '16 #6
NeoPa
32,556 Expert Mod 16PB
JohnJoeDRC:
I have also now skipped the test and I am just trying to return data.
That's really not a good idea. At least, if you want to do that then please don't expect us to jump around with you after spending energy trying to catch up with where you started. Don't get me wrong. Initiative is great. Try things and report them separately by all means. No one wants to work with anyone who keeps changing where they're at willy-nilly though. It's hard enough understanding what you're saying without it changing just as we work out what you were trying to say before.

Getting back to where we started and where we have some idea of what's going on, your post #3 indicates that the record count is shown as -1. That should be ringing bells. I don't do much with ADODB myself but I know when dealing with SQL Server it's often the case that the whole recordset is not transferred at once and this often leaves the record count not properly set. It may be that this needs to be made to work by going to the end of the recordset before checking the count.

Still with post #3, your checks are actually very helpful - as are the comments in the code. Once the code there has run can you press Ctrl-End in the worksheet. If it stays at cell A1 then you know the transfer has truly failed. If it goes elsewhere then we may need to look at the possibility of data being present but just not being visible. Excel does that sometimes.
Feb 17 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Jarrod Morrison | last post by:
Hi All Im generally a vb programmer and am used to referencing multiple records returned from a query performed on an sql database and im trying to move some functions of my software into sql...
18
by: Metro12 | last post by:
In the <basic_string.h>, I find the implementation of these two functions. But I can't understand the difference between them. Please give me some help! //basic_string::c_str() const _CharT*...
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
2
by: thomasamillergoogle | last post by:
I would like to have a web service continue a little bit of processsing after it returns the data to the consumer. What is the proper way to do this? Please provide an bit of example code on how to...
1
by: Harvest Wind | last post by:
Hi: I have a program in VB 6.0 that connects to an access database and connects to a paradox database (directory)... I query an Access table and it returns the data I need...as the program...
1
by: paddymullaney | last post by:
Hi I have a query that is performing very strangely. I f I put a top statement in it returns rows, so Select top 10 * from .......
0
by: h2reyes | last post by:
I have the following query: select sq.*, p.numero, p.nombre from paf p right outer join dbo.GetListOfSquaresForShippingLot(@lot) sq on sq.number = p.numero and sq.version = p.numero The @lot...
10
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from...
1
by: =?iso-8859-1?Q?Cleber_Sch=F6nhofen?= | last post by:
Hi, I have a page that returns data from other execution of the same page, if it occurs simultaneously consultation. Who knows why? -- Cleber Schönhofen Infobola
1
by: redbaron | last post by:
I stuck in new multiprocessing module (ex. processing). I dont' understand why queue.get_nowait() never returns data, but always raises Empty, even if it is guaranteed that queue is not empty. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.