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 - Sub Runner()
-
-
Dim folderName As String
-
Dim salesPeriod As String
-
Dim Region As String
-
Dim cn As ADODB.Connection
-
Dim cms As ADODB.Command
-
Dim rs As ADODB.Recordset
-
Dim SQL As String
-
-
'Make Output Folder
-
folderName = CStr(Year(DateTime.Now) & "." _
-
& Month(DateTime.Now) & "." _
-
& Day(DateTime.Now) _
-
& " " & Hour(DateTime.Now) _
-
& "." & Minute(DateTime.Now) _
-
& "." & Second(DateTime.Now))
-
-
'Next time, make the username a variable to be accessed on any computer
-
ChDir "C:\Users\E0214206\Desktop"
-
MkDir (folderName)
-
-
'Getting the inputted salesPeriod
-
-
salesPeriod = Range("E1").Value
-
-
'Getting the inputted Region
-
Region = Range("E2").Value
-
-
Application.DisplayAlerts = False
-
'Sheets("Inputs").Delete
-
Application.DisplayAlerts = True
-
-
Set cmd = New ADODB.Command
-
Set cn = New ADODB.Connection
-
-
SQL = "SELECT DistrictCode FROM dbo.Analytics_PerformanceData WHERE DistrictCode IS NOT NULL ORDER BY DistrictCode ASC"
-
-
With cn
-
'timeout
-
.CommandTimeout = 0
-
'set connection string
-
.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"
-
'open connection
-
.Open
-
End With
-
-
With cmd
-
'set activeconnection to command object
-
.ActiveConnection = cn
-
-
'disable time limit
-
.CommandTimeout = 0
-
-
'set commandtype
-
.CommandType = adCmdText
-
-
'set commandtext
-
.CommandText = SQL
-
-
'execute query
-
Set rs = .Execute
-
End With
-
-
-
-
'To loop through the districts
-
Dim count As Integer
-
For count = 0 To 5
-
Dim w As Integer
-
w = count
-
-
Dim DistrictCode As String
-
DistrictCode = rs.Fields(count)
-
-
'Refresh Pivot Tables
-
'Call Refresh.Refresh(salesPeriod, DistrictCode, Region)
-
-
'Create Output Files
-
Call CreateOutputFile.CreateOutputFile(salesPeriod, DistrictCode, Region, folderName)
-
Next count
-
-
rs.Close
-
cn.Close
-
-
Set cn = Nothing
-
Set cmd = Nothing
-
Set rs = Nothing
-
-
-
End Sub
7 1480
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
You never loop through your recordset. All you do is loop through 6 columns in the recordset.
Hi! Thanks for the help, I edited my code into this but it still gives me only the first value from the database? - Sub Runner()
-
-
Dim folderName As String
-
Dim salesPeriod As String
-
Dim Region As String
-
Dim cn As ADODB.Connection
-
Dim cms As ADODB.Command
-
Dim rs As ADODB.Recordset
-
Dim SQL As String
-
-
'Make Output Folder
-
folderName = CStr(Year(DateTime.Now) & "." _
-
& Month(DateTime.Now) & "." _
-
& Day(DateTime.Now) _
-
& " " & Hour(DateTime.Now) _
-
& "." & Minute(DateTime.Now) _
-
& "." & Second(DateTime.Now))
-
-
'Next time, make the username a variable to be accessed on any computer
-
ChDir "C:\Users\E0214206\Desktop"
-
MkDir (folderName)
-
-
'Getting the inputted salesPeriod
-
-
salesPeriod = Range("E1").Value
-
-
'Getting the inputted Region
-
Region = Range("E2").Value
-
-
Application.DisplayAlerts = False
-
'Sheets("Inputs").Delete
-
Application.DisplayAlerts = True
-
-
Set cmd = New ADODB.Command
-
Set cn = New ADODB.Connection
-
-
SQL = "SELECT DistrictCode FROM dbo.Analytics_PerformanceData WHERE DistrictCode IS NOT NULL ORDER BY DistrictCode ASC"
-
-
With cn
-
'timeout
-
.CommandTimeout = 0
-
'set connection string
-
.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"
-
'open connection
-
.Open
-
End With
-
-
With cmd
-
'set activeconnection to command object
-
.ActiveConnection = cn
-
-
'disable time limit
-
.CommandTimeout = 0
-
-
'set commandtype
-
.CommandType = adCmdText
-
-
'set commandtext
-
.CommandText = SQL
-
-
'execute
-
Set rs = .Execute
-
-
End With
-
-
'To loop through the districts
-
If rs.EOF = False And rs.BOF = False Then
-
Do While Not rs.EOF
-
Dim i As Integer
-
For i = 0 To (rs.Fields.count - 1)
-
Dim DistrictCode As String
-
DistrictCode = rs.Fields(i).Value
-
-
'Refresh Pivot Tables
-
'Call Refresh.Refresh(salesPeriod, DistrictCode, Region)
-
-
'Create Output Files
-
Call CreateOutputFile.CreateOutputFile(salesPeriod, DistrictCode, Region, folderName)
-
Next i
-
rs.MoveNext
-
Loop
-
End If
-
-
rs.Close
-
cn.Close
-
-
Set cn = Nothing
-
Set cmd = Nothing
-
Set rs = Nothing
-
-
-
End Sub
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.
Oh okay, so how should I fix this? Or how must I approach it to fix it?
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.
Okay, I already got it. Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |