Results 1 to 6 of 6

Thread: VB Code

  1. #1
    Join Date
    Sep 2005
    Posts
    313

    Unanswered: VB Code

    Hi:
    In VB code

    For i = 1 to 10
    if (isnull(rs.fields("code " & i))) then

    .............
    end if

    next i


    The rs.fields("code " & i) does't work correctly. since I add i variable. Does anybody help me to fix this problem? Thanks.

    Thanks.

  2. #2
    Join Date
    Sep 2005
    Location
    Bay Area CA
    Posts
    15
    Try adding paranthesese or storing it in a variable...

  3. #3
    Join Date
    Sep 2005
    Location
    Bay Area CA
    Posts
    15
    Sub test()
    Dim rst As Recordset
    Dim nIndex As Integer

    Dim dbs As Database
    Set dbs = CurrentDb

    Set rst = dbs.OpenRecordset("SELECT '1' as code1, '2' as code2, '3' as code3", dbOpenSnapshot)

    rst.MoveFirst

    For nIndex = 1 To 3
    MsgBox rst.Fields("code" & nIndex)
    Next nIndex

    rst.Close
    Set dbs = Nothing

    End Sub


    I just tried this on Access 2003 and it worked...

    ******************ANSWER**********
    The problem is that you have a space in the field name. If you want to use a space in the field name, you need to put brackets around it...
    *********************************

  4. #4
    Join Date
    Dec 2003
    Posts
    268

    Out of Range issues

    You may also encounter out of range issues. RS field indexing starts at 0 and goes to n. your counter goes from 1 to 10. This will only work if your RS has more than 10 fields.

    FYI

  5. #5
    Join Date
    Sep 2005
    Location
    Bay Area CA
    Posts
    15
    Quote Originally Posted by mjweyland
    You may also encounter out of range issues. RS field indexing starts at 0 and goes to n. your counter goes from 1 to 10. This will only work if your RS has more than 10 fields.

    FYI
    He is concatenating the counter to the field name, not referencing the field by its index.

  6. #6
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    When you say "Doesn't work correctly" what does that mean? My guess is that your fields may have an empty string in them even though they look like null fields. I would change the comparison to:

    If rs.Fields("code " & i) & ""="" Then

    The comparison will be true if the field is null or "" (assumes the field is a string). Otherwise, post all of you code to see if the problem is elsewhere.

Posting Permissions

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