Results 1 to 7 of 7
  1. #1
    theracer06 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    7

    Getting Values out of a recordset

    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?

  2. #2
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    What are _Country Filter and ID COUNTRY? If they're Fields in a Table/Query, you should use dot notation instead of bang notation:

    Code:
    sqlstr = "Select [_Country Filter].[ID COUNTRY] from [_Country Filter]"
    Also, is this a Linked Table (in a Split database) or a Local Table? Because you're not specifying a Recordset type in the .OpenRecordset Method, you may be getting a type that doesn't support .MoveFirst or .MoveLast (like dbOpenTable which uses .Seek instead). If all else fails, try changing your OpenRecordset line to the following:

    Code:
    Set rsread = db.OpenRecordset(sqlstr, dbOpenDynaset)

  3. #3
    NTC is offline VIP
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    2,392
    My design would be to use DLookUp method to set a value. Something like:
    Dim Whatever As String
    Whatever = DLookUp, "etc etc" etc

    then you have a value for Whatever that you can use in another vb or sql statement.

    you might want to explore the DLookUp method in vb help.

    Hope this helps.

  4. #4
    theracer06 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    7
    Hi Rawb & NTC,

    thanks for the suggestions! Tried the dot notation and the dbOpenDynaset, unfortunately Access is still not able to determine the item on that particular line...

    Also looked into DLookup but I did not figure out how to use it in a loop which gets out the value of the record in first row first column then move on to second row first column then third and so on...

    Help I've been trying to solve this for three days now...

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'm pretty sure the Fields collection is zero based, but I don't see the purpose of looping the fields, since you only have one ([ID COUNTRY]). Were you meaning to loop records? Looping the fields would only make sense if you returned more that one.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Wow, I didn't even notice that.

    pbaldy's right: What you're doing is running the exact same Query repeatedly. This Query is returning a Recordset with a single Field in each Record. You're then trying to read (from the same Record every time), Fields 2 through NC+1. Which don't exist since you only have one Field in each Record.

    If you're trying to run through each Record returned, try running the Query before the loop starts. Then, at the end of each loop iteration, just use .MoveNext to go to the Record in the Recordset.

    Code:
      sqlstr = "SELECT [ID COUNTRY] FROM [_Country Filter]"
      rsread = db.OpenRecordset(strsql, dbOpenDynaset)
    
      If Not rsread.RecordCount = 0 Then
        Do Until rsread.EOF
          CountryCurveSelect = rsread("ID COUNTRY")
    
          . . . ' Do your stuff here
    
          rsread.MoveNext
        Loop
      End If
    Of course without knowing more of your Code, Table setup, Form, etc. I can't tell you if the above code is what you're looking for or not. It does the following:

    1. Loads a list of every COUNTRY ID in the _Country Filter Table into the rsread Recordset
    2. It assigns the first COUNTRY ID returned into your CountryCurveSelect variable.
    3. Performs the rest of your code on it
    4. Starts the whole process over (from Step 2) on the next COUNTRY ID in rsread
    5. Repeats steps 2-4 until all of the COUNTRY IDs have been read.

  7. #7
    theracer06 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    7
    Thanks Rawb, now you nailed with precision!
    One more step into learning how to use recordsets!

    Best,TR

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Recordset not updateable
    By jgelpi16 in forum Forms
    Replies: 3
    Last Post: 09-20-2010, 09:50 AM
  2. ADO Recordset.Open (SQL) does nothing
    By workindan in forum Programming
    Replies: 3
    Last Post: 06-23-2010, 02:07 PM
  3. Replies: 3
    Last Post: 04-04-2010, 05:26 PM
  4. Replies: 1
    Last Post: 03-27-2010, 06:13 AM
  5. Replies: 1
    Last Post: 11-13-2009, 03:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums