Click here<\/strong><\/a> to read and understand this Method.<\/p>\n2. Reading the whole RecordSet Object using Loop<\/h3>\n RecordSetObject(0).Value<\/strong> gives you the value of the 1st Column value of the 1st Record of your RecordSet Object. .MoveNext<\/strong> is the method of your RecordSet Object which takes you to the Next Record of your recordSet. Below is the VBA code snippet which reads all the Records of your RecordSet and put it in your Excel Sheet named Data<\/i><\/strong>.<\/p>\n\r\n xlRow = 1 ' Set it for your Excel Sheet Starting Row\r\n Do While Not RecordSetObject.EOF 'to traverse till last record\r\n 'This for loop will display the values\r\n 'of all column values in one Single record\r\n xlCol = 1 'Every next Record Should start from Starting Column\r\n For DBCol = 0 To RecordSetObject.Fields.Count - 1\r\n Worksheets(\"Data\").Cells(xlRow, xlCol).Value = RecordSetObject(DBCol).Value\r\n xlCol = xlCol + 1 'move to next column in same row\r\n Next\r\n RecordSetObject.MoveNext 'This moves the loop to next record from the record set\r\n xlRow = xlRow + 1 'Move to next row in Excel\r\n Loop\r\n<\/code><\/pre>\n3. Reading RecordSet Values by Passing Column Names<\/h3>\n Above VBA code reads all the column values from the RecordSet by passing the Column Index – 0 to Total Columns-1.<\/strong> Suppose, If you want to read values of a particular column which Name is known to you but it could be 1st Column or 2nd Column or any nth column of your RecordSet Object. I will explain you the method of accessing the value of a column by passing the column name rather Column Index as passed in above example.<\/p>\nSyntax is very much same as above. Instead of passing the Index Number of the Column, you need to pass the Column Name in Double Quotes (” “). RecordSetObject(“Column_Name”).Value<\/strong><\/p>\n\r\n\r\n xlRow = 1 ' Set it for your Excel Sheet Starting Row\r\n Do While Not DBrs.EOF 'to loop till last record of the recordSet\r\n Worksheets(\"Data\").Cells(xlRow, 1).Value = RecordSetObject(\"Column_NAME_1\").Value\r\n Worksheets(\"Data\").Cells(xlRow, 2).Value = RecordSetObject(\"Column_NAME_2\").Value\r\n Worksheets(\"Data\").Cells(xlRow, 3).Value = RecordSetObject(\"Column_NAME_3\").Value\r\n \r\n DBrs.MoveNext 'This reads the next record from the record set\r\n xlRow = xlRow + 1 'Move to next row in Excel\r\n Loop\r\n<\/code><\/pre>\nI prefer accessing column values by passing the column name. There are two benefits of using this method:<\/p>\n
\n1. In case of Select * from…<\/strong> queries you do not need to check the position of your column to access it. No matter at what position your column is..can be accessed correctly from this method.<\/p>\n2. Easy to debug:<\/strong> While debugging your code – Statement RecordSetObject(“Column_NAME_1”).Value<\/i> is clearly understood without referring the actual database table as compared to RecordSetObject(5).Value<\/i>. To know which column is being referred in this statement I need to check the NAME proprty of the 5th Column of your RecordSet\n<\/p>\n<\/blockquote>\nAre you facing any difficulty in accessing your RecordSet Object?? Put your queries here in comment. We will try to get back on that ASAP \ud83d\ude42 <\/h3>\n<\/span>","protected":false},"excerpt":{"rendered":"Dear Friends, In my previous article, I emphasized mainly on how to execute different kind of queries in database using Excel VBA. I covered one example How to display or spread a RecordSet in excel Sheet. But in this article I am going to focus on different ways of reading RecordSet Object. How to get […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[5203,1246,1675],"tags":[],"class_list":["post-4166","post","type-post","status-publish","format-standard","hentry","category-database","category-macro","category-excel-macro-for-beginners"],"yoast_head":"\n
Different ways of Reading RecordSet Object in Excel Macro - Let's excel in Excel<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n