473,386 Members | 1,798 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,386 software developers and data experts.

Why is my RecordSet containing only 1 value and RecordCount = -1?

Why does my RecordSet only get the first value in the database? There are supposed to be 41 districts but only the first one is returned

Here is my code, help me please. Thank you

Expand|Select|Wrap|Line Numbers
  1. Sub Runner()
  2.  
  3.     Dim folderName As String
  4.     Dim salesPeriod As String
  5.     Dim Region As String
  6.     Dim cn As ADODB.Connection
  7.     Dim cms As ADODB.Command
  8.     Dim rs As ADODB.Recordset
  9.     Dim SQL As String
  10.  
  11.     'Make Output Folder
  12.     folderName = CStr(Year(DateTime.Now) & "." _
  13.                     & Month(DateTime.Now) & "." _
  14.                     & Day(DateTime.Now) _
  15.                     & " " & Hour(DateTime.Now) _
  16.                     & "." & Minute(DateTime.Now) _
  17.                     & "." & Second(DateTime.Now))
  18.  
  19.     'Next time, make the username a variable to be accessed on any computer
  20.     ChDir "C:\Users\E0214206\Desktop"
  21.     MkDir (folderName)
  22.  
  23.     'Getting the inputted salesPeriod
  24.  
  25.     salesPeriod = Range("E1").Value
  26.  
  27.     'Getting the inputted Region
  28.     Region = Range("E2").Value
  29.  
  30.     Application.DisplayAlerts = False
  31.     'Sheets("Inputs").Delete
  32.     Application.DisplayAlerts = True
  33.  
  34.     Set cmd = New ADODB.Command
  35.     Set cn = New ADODB.Connection
  36.  
  37.     SQL = "SELECT DistrictCode FROM dbo.Analytics_PerformanceData WHERE DistrictCode IS NOT NULL ORDER BY DistrictCode ASC"
  38.  
  39.     With cn
  40.         'timeout
  41.         .CommandTimeout = 0
  42.         'set connection string
  43.         .ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=SLP;Data Source=mnlsapp02;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=MNLL1033;Use Encryption for Data=False;Tag with column collation when possible=False"
  44.         'open connection
  45.         .Open
  46.     End With
  47.  
  48.     With cmd
  49.         'set activeconnection to command object
  50.         .ActiveConnection = cn
  51.  
  52.         'disable time limit
  53.         .CommandTimeout = 0
  54.  
  55.         'set commandtype
  56.         .CommandType = adCmdText
  57.  
  58.         'set commandtext
  59.         .CommandText = SQL
  60.  
  61.         'execute query
  62.         Set rs = .Execute
  63.     End With
  64.  
  65.  
  66.  
  67.     'To loop through the districts
  68.     Dim count As Integer
  69.     For count = 0 To 5
  70.         Dim w As Integer
  71.         w = count
  72.  
  73.         Dim DistrictCode As String
  74.         DistrictCode = rs.Fields(count)
  75.  
  76.         'Refresh Pivot Tables
  77.         'Call Refresh.Refresh(salesPeriod, DistrictCode, Region)
  78.  
  79.         'Create Output Files
  80.         Call CreateOutputFile.CreateOutputFile(salesPeriod, DistrictCode, Region, folderName)
  81.     Next count
  82.  
  83.     rs.Close
  84.     cn.Close
  85.  
  86.     Set cn = Nothing
  87.     Set cmd = Nothing
  88.     Set rs = Nothing
  89.  
  90.  
  91. End Sub
Apr 29 '14 #1
7 1480
jimatqsi
1,271 Expert 1GB
c,
Welcome to Bytes. Please use Code tags when posting code. Click the [Code/] button and paste your code between the links.

Where is the information about recordcount coming from? I don't see any reference to it in the code. Before querying recordcount you should advance to the last record in your recordset or it may not be accurate.

I would begin by adding error handling to your routine.

Jim
Apr 29 '14 #2
Rabbit
12,516 Expert Mod 8TB
You never loop through your recordset. All you do is loop through 6 columns in the recordset.
Apr 29 '14 #3
Hi! Thanks for the help, I edited my code into this but it still gives me only the first value from the database?

Expand|Select|Wrap|Line Numbers
  1. Sub Runner()
  2.  
  3.     Dim folderName As String
  4.     Dim salesPeriod As String
  5.     Dim Region As String
  6.     Dim cn As ADODB.Connection
  7.     Dim cms As ADODB.Command
  8.     Dim rs As ADODB.Recordset
  9.     Dim SQL As String
  10.  
  11.     'Make Output Folder
  12.     folderName = CStr(Year(DateTime.Now) & "." _
  13.                     & Month(DateTime.Now) & "." _
  14.                     & Day(DateTime.Now) _
  15.                     & " " & Hour(DateTime.Now) _
  16.                     & "." & Minute(DateTime.Now) _
  17.                     & "." & Second(DateTime.Now))
  18.  
  19.     'Next time, make the username a variable to be accessed on any computer
  20.     ChDir "C:\Users\E0214206\Desktop"
  21.     MkDir (folderName)
  22.  
  23.     'Getting the inputted salesPeriod
  24.  
  25.     salesPeriod = Range("E1").Value
  26.  
  27.     'Getting the inputted Region
  28.     Region = Range("E2").Value
  29.  
  30.     Application.DisplayAlerts = False
  31.     'Sheets("Inputs").Delete
  32.     Application.DisplayAlerts = True
  33.  
  34.     Set cmd = New ADODB.Command
  35.     Set cn = New ADODB.Connection
  36.  
  37.     SQL = "SELECT DistrictCode FROM dbo.Analytics_PerformanceData WHERE DistrictCode IS NOT NULL ORDER BY DistrictCode ASC"
  38.  
  39.     With cn
  40.         'timeout
  41.         .CommandTimeout = 0
  42.         'set connection string
  43.         .ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=SLP;Data Source=mnlsapp02;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=MNLL1033;Use Encryption for Data=False;Tag with column collation when possible=False"
  44.         'open connection
  45.         .Open
  46.     End With
  47.  
  48.     With cmd
  49.         'set activeconnection to command object
  50.         .ActiveConnection = cn
  51.  
  52.         'disable time limit
  53.         .CommandTimeout = 0
  54.  
  55.         'set commandtype
  56.         .CommandType = adCmdText
  57.  
  58.         'set commandtext
  59.         .CommandText = SQL
  60.  
  61.         'execute
  62.         Set rs = .Execute
  63.  
  64.     End With
  65.  
  66.     'To loop through the districts
  67.     If rs.EOF = False And rs.BOF = False Then
  68.         Do While Not rs.EOF
  69.             Dim i As Integer
  70.             For i = 0 To (rs.Fields.count - 1)
  71.                 Dim DistrictCode As String
  72.                 DistrictCode = rs.Fields(i).Value
  73.  
  74.                 'Refresh Pivot Tables
  75.                 'Call Refresh.Refresh(salesPeriod, DistrictCode, Region)
  76.  
  77.                 'Create Output Files
  78.                 Call CreateOutputFile.CreateOutputFile(salesPeriod, DistrictCode, Region, folderName)
  79.             Next i
  80.             rs.MoveNext
  81.         Loop
  82.     End If
  83.  
  84.     rs.Close
  85.     cn.Close
  86.  
  87.     Set cn = Nothing
  88.     Set cmd = Nothing
  89.     Set rs = Nothing
  90.  
  91.  
  92. End Sub
Apr 29 '14 #4
Rabbit
12,516 Expert Mod 8TB
It shouldn't even give you one value. Your line 67 should prevent any of the code from running. Since you haven't progressed through any of the recordset yet, the BOF will always be true.
Apr 30 '14 #5
Oh okay, so how should I fix this? Or how must I approach it to fix it?
Apr 30 '14 #6
Rabbit
12,516 Expert Mod 8TB
I assume you're doing that check to see if there's at least one record, in which case, you need to change the BOF check from false to true because if there's at least one record, then it will be at the BOF but not at the EOF.
Apr 30 '14 #7
Okay, I already got it. Thanks!
Apr 30 '14 #8

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

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
12
by: scott | last post by:
In LISTING 2, I have a SPROC that returns a recordset and a recordcount in SQL QA. I can access the Recordset with no problem. How can I grab the Recordcount with ASP code at the same time I'm...
0
by: CFW | last post by:
I thought this was going to be easy but I'm missing something . . . I need to open an ADODB recordset using the recordset source for a list box on my for. When my form opens, the list box ADODB...
1
by: JMCN | last post by:
hello- i have created a tabular form using records from a specific query. then users will filter out the specific data. the next step is to take the count of the current records (daily open...
4
MMcCarthy
by: MMcCarthy | last post by:
The following code is simply an example of some code that processes through two recordsets. It can be helpful for anyone curious as to how to start processing with recordsets (Which objects to refer...
2
by: Tom Clavel | last post by:
Scratching my head. I am using the filter property on a combo box .AfterUpdate event to get to a single client record. This is causing a some strange behavior: As I enter a subform, I get a no...
3
by: mark_aok | last post by:
Hi all, All I am trying to do is open a table, edit it, and then close it. But I am having the strangest error. Here is my code Dim i as integer Dim rs as adodb.recordset Set rs = new...
8
by: BerkshireGuy | last post by:
Hello everyone. I have a ADODB recordset that uses a connection string to connect and grab data from our mainframe. Had worked great in the past. Then - I ran the same module the other day...
11
by: questionit | last post by:
Dlookup() gives an error if the searchkey (the field i am looking for) is not found in the table. How to check before Dlookup() statement whether the table contains the required value. ...
2
by: hackmagic | last post by:
Hi, i have a form that normally has a Recordset containing only one record bound to it. i have replaced the navigation buttons with my own and the 'New Record' button assigns an empty Recordset...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.