Wrox Programmer Forums
|
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2004, 10:49 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default dynamicly name a record set

I am having trouble generating a name on demand for a record set, would very much appreciate some help. My objective: generate multiple record sets with different names - mmm seems easy.

I have a commer delimited array of id's
I then split the array. For each id I need to write an sql statement, execute it and populate a record set. There is no way of telling how many ids there are going to be so i have:

(my array is called id)
(I have the ubound of my array in a variable called 'numberOfIds')
count = 0
recordSetCount = 0
do while count < numberOfIds
  sql = "select blah blah........where id = id(count)
  count = count + 1
  recordSetCount = recordSetCount + 1
  values = "values" & recordSetCount
  set values = conn.execute(sql)
loop

Can this be done??
Let say I have three ids in my array, I should have three record sets values1, values2, values3. According to the above loop they should be executed and contain records (all vars have been dimmed and there are matching records)

So i should be able to:
do until values1.EoF
    response.write values1(0) & "===" & values1(1) & "<bR>"
    values1.moveNext
loop

do until values2.EoF
    response.write values2(0) & "===" & values2(1) & "<bR>"
    values2.moveNext
loop

do until values3.EoF
    response.write values3(0) & "===" & values3(1) & "<bR>"
    values3.moveNext
loop

However neither of the three loops above work, but the following does:
do until values.EoF
    response.write values3(0) & "===" & values(1) & "<bR>"
    values.moveNext
loop

Thanking ou in advance
Matt


Wind is your friend
Matt
__________________
Wind is your friend
Matt
 
Old March 15th, 2004, 04:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Every time you execute your SQL statement you overwrite the previous variable. You have to preserve the old values by doing something like this:
Code:
  Dim i
  Dim arrRS()
  i = 0
  do while count < numberOfIds
  sql = "select blah blah........where id = id(count) 
  count = count + 1
  set values = conn.execute(sql)
  recordSetCount = values.Count
  ReDim arrRS(recordSetCount)
  Do Until values.EOF
    arrRS(i) = values(0) & "===" & values(1) &  "<bR>"
    i = i+1
    values.MoveNext
  Loop
loop
For intCounter=0 To UBound(arrRS)
  Response.Write(arrRS(intCounter))
Next
Variable names can not be dynamically dimensioned.
Please notice that this isn’t a copy/paste code snippet.

 - mega
 
Old March 15th, 2004, 04:52 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

It looks like you are trying to create dynamic variables, which is not going to work.

values = "values" & recordSetCount
set values = conn.execute(sql)

In the above code, you are creating a variable (the second values) by appending a number to a string, which is not allowed. Variables need to be defined at development time.

What you need is an array. You can store each recordset in an array, like this:

Dim myArray(1)
Set myArray(0) = MyFirstRecordset
Set myArray(1) = MySecondRecordset

What's important with this scenario is that you disconnect the recordset from the initial datasource. For this to work, you'll need to set the ActiveConnection of the recordset to Nothing. And that, in turn, requires a client side cursor on the connection.

Here's a quick example that demonstrates what I mean. Basically this code loops through an array of table names, executing a SELECT * FROM for each table. The recordset is disconnected from the connection, and saved in the array using Set. At the end, the code loops through the array of recordset. For each recordset, all the first fields for all records are displayed at the page:
Code:
<%
    ' Modify previous line, so it points to a valid adovbs.inc file
    Dim arrRecordsets(2)
    Dim oField ' As ADODB.Field
    Dim oRecordset
    Dim oConn
    Dim iLoop
    Dim sSQLBase

    Dim arrTableNames(2)
    arrTableNames(0) = "Events"
    arrTableNames(1) = "Users"
    arrTableNames(2) = "Categories"

    sSQLBase = "SELECT * FROM "

  ' Create connection
    Set oConn = Server.CreateObject("ADODB.Connection")
    oConn.Open "YourConnectionString"
    ' Explicitly set a cllient side cursor. Required to disconnect the recordset    
    oConn.CursorLocation = adUseClient

    For iLoop = 0 To UBound(arrTableNames)
        ' Create the recordset
        Set oRecordset = oConn.Execute(sSQLBase & arrTableNames(iLoop))
        ' Disconnect Recordset from source
        oRecordset.ActiveConnection = Nothing
        ' Put the Recordset in the array
        ' The sett command is very important here; otherwise
        ' you'll just assign the default property of the recordset
        ' to the array
        Set arrRecordsets(iLoop) =  oRecordset
    Next

    ' Clean up
    oConn.Close
    Set oConn = Nothing

  ' At this point, arrRecordset should contain three recordsets. 
    ' Let's see if that is true.
    For iLoop = 0 To UBound(arrTableNames)
        Set oRecordset = arrRecordsets(iLoop)
        If Not oRecordset.EOF Then
            Response.Write("<h1>Recordset " & iLoop & "</h1>")
            Do While Not oRecordset.EOF
                    Response.Write("First field is " & _
                            oRecordset.Fields(0).Value & "<br />")
                    oRecordset.MoveNext()
            Loop
        Else
            Response.Write("Recordset is empty")
        End If
    Next
%>
To test this code, you'll need to modify the parts in bold. I.e.: change the array with table names so it holds the names of some tables in your database, and modify the connection string.

As I put in my comments, it's important to use Set when you assign the recordset to the array.

Although this scenario will work pretty well, it may not be recommended in terms of performance. What may be better is to store the contents of the recordset in its own array using GetRows, and store that in the main array. Not really sure if that works or not. It could work, but I am not sure how VBScript handles jagged arrays.

A third alternative may be to change your business logic. Not really sure what you're trying to accomplish with this code, but maybe you can skip it altogether, changing things so all this looping isn't really necessary.

Hope this helps,

Imar




---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 15th, 2004, 06:38 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Imar

Thank you very much for your time, your posts always exceeds expectations (I have seen and learnt alot from your posts)
To explain why I have this need:
Once each individual uniquely named record set get executed (yes, i then run getRows), they get passed into an object (a custom object that renders bar, line and pie graphs) that takes as many 2 dimentional arrays as it has been given (how many ids were passed to the page) and creates a graph. This one in particular renders a line graph - a line appears on the graph for each 2 dimentional array

I am going to impliment your snazy piece of code now, nice one!!

After I have executed my queries I run - (this is a hard coded test version asuming there are 3 queries) line 5,6,7 of this code is my next mission, placing a loop there for as many queries as your above code produces. (the page all this code gets placed on is called inside an <img> tag on another page)

  Set chartObj = Server.CreateObject("FrameChart.LineChart")
  chartObj.Title = "Number of Incidents by Type (financial yearly)"
  chartObj.Width = request.queryString("width")
  chartObj.Height = request.queryString("height")
  chartObj.AddSeries values1.GetRows, "Re-active", &hFF0000 ' Red
  chartObj.AddSeries values2.GetRows, "Pro-active", &h00FF00 ' Green
  chartObj.AddSeries values3.GetRows, "Near miss", &h0000FF ' Blue
  chartObj.maxYValue = 100
  chartObj.minYValue = 0
  chartObj.ShowLegend = true
  chartObj.XAxisCaption = "Months"
  chartObj.YAxisCaption = "Number of Incidents"
  response.Binarywrite(chartObj.GetImage)
  set chartObj=nothing
  Set values1 = nothing
  Set values2 = nothing
  Set values3 = nothing
  conn.close
  set conn = nothing

Not that you probably need it, you sound like you could create a graphing object in your sleep. Would you like it? it takes many properties for customization and works well

Cheers
Matt

Wind is your friend
Matt
 
Old March 15th, 2004, 07:27 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Are you a sailer / yachtsman??

I am glad you like the stuff I post here.
I try to program when I am working, and do other things when I sleep. It would be a really scary idea If I could come up with bar graph engine in my sleep.

So yeah, I am interested. What language is it in? Send me an e-mail through my user profile if you want. I'll reply, so you'll know my e-mail address and ne able to send me an attachment.
Alternatives (posting online?) are fine by me as well.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 15th, 2004, 08:02 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Kitesurfing -- totally sick, sick, sick - slighlty dangerous, must get lessons!!
I have sent you an email thru your profile

Wind is your friend
Matt
 
Old March 16th, 2004, 03:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Ah, cool.

I am a fanatic sailor and surfer myself, so I figured you might be into the same kind of stuff, having the wind as you friend....

It not always is, though. I remember that a while ago the wind was my friend as well, so I was able to surf to the other side of a lake in no time. As soon as I got there, the wind dropped. It took me over three hours to paddle pack.... Nice friend.... ;)

I'll check my e-mail and reply to you.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
help about record set shankhan Classic ASP Databases 1 June 5th, 2005 10:51 PM
Possible Emty Record Set englandera Classic ASP Databases 2 November 24th, 2004 01:37 PM
Inserting A Record Set nikotromus Access VBA 3 September 14th, 2004 01:23 PM
set record in the recordset to be unmodified benmics ADO.NET 0 August 23rd, 2004 11:33 AM
record set problem spraveens Classic ASP Databases 6 September 12th, 2003 06:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.