Getting Values out of a recordset

theracer

New Member
Joined
May 3, 2010
Messages
9
Hi,

I need to get values out of a recordset in order to filter a second sqlstring

I constructed a filter query and get the records out of it in the first SQLstring in order to use the values to filter the second SQLstring. Unfortunately, when I set CountryCurveSelect = recordset.Fields(C).Values, there are no items to be found...I also tried
with recordset.getrows but that also gives problems probably due to the
multidimensionality of the variant.

Here is what I set up

Code:
' Loop through the countries until predefined Number of Countries (NC) is
reached
For C = 1 To NC
   sqlstr = "Select [_Country Filter]![ID COUNTRY] from [_Country Filter]"
   Set rsread = db.OpenRecordset(sqlstr)
   rsread.MoveFirst
   CountryCurveSelect = rsread.Fields(C).Values
   rsread.Close

' Original curvesmoothing procedure   
   sqlstr = "Select * from [myTable] where [ID Country] = " &
CountryCurveSelect & ";"
   Set rsread = db.OpenRecordset(sqlstr)
   If rsread.BOF Then Resume Next   ' No records retrieved
   rsread.MoveLast
   ValueCount = rsread.RecordCount

etc...

Next C

What am I doing wrong?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Denis

I don't think I've had to do the parameter thing with DAO or ADO.

Always seemed to find it was just some SQL syntax error.

That's one reason I tend to 'build' the SQL like this.
Code:
Dim strSQL As String
 
     strSQL = strSQL & "SELECT Field1, Field2, Field2 "
     strSQL = strSQL & "FROM tblTheTable "
     strSQL = strSQL & "WHERE Field1 = '" & strCrit & "'"
Kind of helps when debugging, eg you can just comment out lines, replace bits quite easily.:)
 
Upvote 0
Code:
    sSQL = "SELECT * FROM [My Table] WHERE [CountryID] In(SELECT TOP " & NC & " [CountryID] FROM [My Table])"
    Set qdf = DBEngine(0)(0).CreateQueryDef("", sSQL)
    For Each prm In qdf.Parameters
        prm.Value = Eval(prm.Name)
    Next prm

Does this qdf have actual parameter objects in it? It looks like straight sql to me after the string value is assigned to sSQL. Interesting. What's being evaluated with Eval() here?
 
Upvote 0
Norie

I tend to build longer SQL strings in rows like you said. Maybe I get carried away with it sometimes; today I had a sample that had too may line breaks, and the compiler wouldn't let me continue...
As for the syntax errors, yep, I get them too. But I have fixed run-time errors with that loop too many times to count now.

Xenou

No parameters, just criteria that are fed through as variables. I have had so many of these sorts of queries generate run-time errors that I tend to automatically evaluate them before opening the recordset.
It may be overkill, but it doesn't cause any slowdown that I can detect so I just do it.

Denis
 
Last edited:
Upvote 0
Denis

I suppose if it works then that's the important thing.

I think I'd probably try and find what was causing the errors, then a few hours later give up as usual.

I'll file it away under the heading 'need too sort that out' - top priority of course.:)
 
Upvote 0
I'll file it away under the heading 'need too sort that out' - top priority of course.

This is where I have this too, i think :rolleyes:
It's good to know, though Thanks Denis - good tips.

ξ
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top