Results 1 to 3 of 3

Thread: Weird problem with SQL RecordSet (DAO) [RESOLVED]

  1. #1

    Thread Starter
    Member Birth's Avatar
    Join Date
    Jan 2003
    Location
    Montreal
    Posts
    57

    Exclamation Weird problem with SQL RecordSet (DAO) [RESOLVED]

    Hi,

    When I use this code
    VB Code:
    1. ...
    2. Public RS As Recordset
    3. -----------------------------
    4. Dim SQL As String
    5.  
    6. SQL = "SELECT Table.NAM, Table.* From Table WHERE Table.NAM='" & NM$ & "'"
    7. or
    8. SQL = "SELECT DISTINCTROW CDP.NAM, CDP.* From CDP WHERE CDP.NAM='" & NM$ & "'"
    9.  
    10. Set RS = DB.OpenRecordset(SQL, dbOpenDynaset)
    11.  
    12. While Not RS.EOF
    13.     If RS("NAM") = NM$ Then
    14.         RS.Edit
    15.         RS("Number") = 1
    16.         RS("Status") = "Help"
    17.         ....
    18.         RS.Update
    19.     End If
    20.     RS.MoveNext
    21. Wend
    I get "Item not found in this collection" only for RS("NAM"). All of the other fields are there.

    I've been trying to figure this one out all day! What the hell is wrong with that?

    If I do...
    VB Code:
    1. Dim fld As Field, td As TableDef
    2.            
    3.             Set td = DB.TableDefs!cdp
    4.  
    5.             For Each fld In td.Fields: Debug.Print fld.Name: Next
    It displays all of the fields including 'NAM'.

    Please help!
    Last edited by Birth; Dec 11th, 2003 at 10:02 AM.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Your sql statement causes the name of the NAM field to become "Table.Nam" within the recordset. You would need to reference the recordset field using RS("Table.Nam").

    Note it depends on the sql statement. For instance this statement

    Select Table.Nam, Table.Nam From Table names the two recordset fields as "Expr100" and "Nam". Other forms of the sql statement may generate different names.

    Also, get familiar with the Locals Window (View menu) to help in debugging.

  3. #3

    Thread Starter
    Member Birth's Avatar
    Join Date
    Jan 2003
    Location
    Montreal
    Posts
    57

    Thumbs up

    Thanks a lot!

    It's funny that after all those years I never noticed that. But then again, I never was very good in SQL. I use Access to create my queries and modify them for VB, just don't tell anybody.

    And thanks also for that Locals tip. I've been using VB since version 3, I think, and I never stopped in there for whatever reason. By the way, my RS RecordSet doesn't show in there and is declared as Public in a main module. I wonder why? I have a DS (for DynaSet) that shows with all the other stuff (also declared as Public).

    Bert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width