Easy Loops question? 
Author Message
 Easy Loops question?

If anyone could tell me why this isn't working, I'd be
very grateful.
I have a table called 'olive' which contains a field
called 'link'.
I want to print the ID of each record in the 'link' field
that is over a certain length.  Instead this code prints
each ID for each record.

Any help much appreciated.

Sub MySub()
    Dim db As Database
    Dim rst As Recordset
    Dim MyField As String
    Dim MyLength As Integer

    Set db = DBEngine.Workspaces(0).Databases(0)
    Set rst = db.OpenRecordset("olive")
     MyField = rst("link")

    Do Until rst.EOF
        If Len(MyField) > 30 Then
        Debug.Print ("too long")
        End If
        rst.MoveNext
        Loop

    End Sub



Tue, 20 Jan 2004 19:13:36 GMT  
 Easy Loops question?
try changing
Debug.Print ("too long")
to
Debug.Print MyField,Len(MyField)

to see what you are dealing with
--

~~~~~~~~~~~~~
Make a Great Day
TomG


Quote:
> If anyone could tell me why this isn't working, I'd be
> very grateful.
> I have a table called 'olive' which contains a field
> called 'link'.
> I want to print the ID of each record in the 'link' field
> that is over a certain length.  Instead this code prints
> each ID for each record.

> Any help much appreciated.

> Sub MySub()
>     Dim db As Database
>     Dim rst As Recordset
>     Dim MyField As String
>     Dim MyLength As Integer

>     Set db = DBEngine.Workspaces(0).Databases(0)
>     Set rst = db.OpenRecordset("olive")
>      MyField = rst("link")

>     Do Until rst.EOF
>         If Len(MyField) > 30 Then
>         Debug.Print ("too long")
>         End If
>         rst.MoveNext
>         Loop

>     End Sub



Tue, 20 Jan 2004 20:14:38 GMT  
 Easy Loops question?
Short answer is you didn't ask for it to print.

See below.

Steve

Sub MySub()
    Dim db As Database
    Dim rst As Recordset
    Dim MyField As String
    Dim MyLength As Integer

    Set db = DBEngine.Workspaces(0).Databases(0)
    Set rst = db.OpenRecordset("olive")

    Do Until rst.EOF
        If Len(rst!link) > 30 Then 'or If Len(rst("link")) > 30 Then
           Debug.Print rst!ID & " " rst!link & " " & "too long"
        End If
        rst.MoveNext
    Loop

   'release object variables
    rst.Close
    Set rst = Nothing
    Set db = Nothing
End Sub

Quote:

> If anyone could tell me why this isn't working, I'd be
> very grateful.
> I have a table called 'olive' which contains a field
> called 'link'.
> I want to print the ID of each record in the 'link' field
> that is over a certain length.  Instead this code prints
> each ID for each record.

> Any help much appreciated.

> Sub MySub()
>     Dim db As Database
>     Dim rst As Recordset
>     Dim MyField As String
>     Dim MyLength As Integer

>     Set db = DBEngine.Workspaces(0).Databases(0)
>     Set rst = db.OpenRecordset("olive")
>      MyField = rst("link")

>     Do Until rst.EOF
>         If Len(MyField) > 30 Then
>         Debug.Print ("too long")
>         End If
>         rst.MoveNext
>         Loop

>     End Sub



Wed, 21 Jan 2004 01:44:59 GMT  
 Easy Loops question?
You need to reference the field of the concerned column for each row.

This can be done by creating a field object that references the field of the
recordset.  Alternatively, you can interrogate the value from the recordset
fields collection.

Sub MySub()
    Dim db As Database
    Dim rst As Recordset
    Dim MyField As String
    Dim MyLength As Integer

    Set db = DBEngine.Workspaces(0).Databases(0)
    Set rst = db.OpenRecordset("olive")

    Do Until rst.EOF
        MyField = rst("link").Value 'interrogate field value from field item
of fields collection of recordset
        If Len(MyField) > 30 Then
            Debug.Print ("too long")
        End If
        rst.MoveNext
    Loop
'remember to teardown the recordset  and destroy your objects
rst.close
set rst = nothing
set dbs = nothing 'don't close the db because it is the instance that ms
access is using
End Sub

Sub MySub()
    Dim db As Database
    Dim rst As Recordset
    Dim MyField As DAO.Field
    Dim MyLength As Integer

    Set db = DBEngine.Workspaces(0).Databases(0)
    Set rst = db.OpenRecordset("olive")
    Set fld = rst.Fields("link")

    Do Until rst.EOF
        'Object reference
        If Len(MyField.Value) > 30 Then
            Debug.Print ("too long")
        End If
        rst.MoveNext
    Loop
'remember to teardown the recordset  and destroy your objects
rst.close
set rst = nothing
set dbs = nothing 'don't close the db because it is the instance that ms
access is using
End Sub

Ted McNeal


Quote:
> If anyone could tell me why this isn't working, I'd be
> very grateful.
> I have a table called 'olive' which contains a field
> called 'link'.
> I want to print the ID of each record in the 'link' field
> that is over a certain length.  Instead this code prints
> each ID for each record.

> Any help much appreciated.

> Sub MySub()
>     Dim db As Database
>     Dim rst As Recordset
>     Dim MyField As String
>     Dim MyLength As Integer

>     Set db = DBEngine.Workspaces(0).Databases(0)
>     Set rst = db.OpenRecordset("olive")
>      MyField = rst("link")

>     Do Until rst.EOF
>         If Len(MyField) > 30 Then
>         Debug.Print ("too long")
>         End If
>         rst.MoveNext
>         Loop

>     End Sub



Wed, 21 Jan 2004 01:47:51 GMT  
 Easy Loops question?
Each ID for each record? As you've assigned the value *before* entering the
loop, I would expect it to just keep testing the value of the 'link' field
in the *first* record.

You need to assign the value to the variable *within* the loop:

    Do Until rst.EOF
        MyField = rst("link")
        If Len(MyField) > 30 Then
            Debug.Print ("too long")
        End If
        rst.MoveNext
    Loop

If there are a large number of records, you might see some performance
improvement by selecting only the records and fields you are interested in:

Set rst = db.OpenRecordset("SELECT link FROM olive WHERE Len(link) > 30")

Then you can dispense with the test within the loop, as the recordset will
contain only the records that meet your condition.

--
Brendan Reynolds

http://www11.ewebcity.com/brenreyn


Quote:
> If anyone could tell me why this isn't working, I'd be
> very grateful.
> I have a table called 'olive' which contains a field
> called 'link'.
> I want to print the ID of each record in the 'link' field
> that is over a certain length.  Instead this code prints
> each ID for each record.

> Any help much appreciated.

> Sub MySub()
>     Dim db As Database
>     Dim rst As Recordset
>     Dim MyField As String
>     Dim MyLength As Integer

>     Set db = DBEngine.Workspaces(0).Databases(0)
>     Set rst = db.OpenRecordset("olive")
>      MyField = rst("link")

>     Do Until rst.EOF
>         If Len(MyField) > 30 Then
>         Debug.Print ("too long")
>         End If
>         rst.MoveNext
>         Loop

>     End Sub



Wed, 21 Jan 2004 04:15:42 GMT  
 Easy Loops question?
Looks like you are printing "too long". To troubleshoot, why don't you use:
Debug.Print "MyField: >" & MyField & "<   Length: " & Len(MyField)
Seems to me this would provide a clue...
Is this an Access table?

Duane


Quote:
> If anyone could tell me why this isn't working, I'd be
> very grateful.
> I have a table called 'olive' which contains a field
> called 'link'.
> I want to print the ID of each record in the 'link' field
> that is over a certain length.  Instead this code prints
> each ID for each record.

> Any help much appreciated.

> Sub MySub()
>     Dim db As Database
>     Dim rst As Recordset
>     Dim MyField As String
>     Dim MyLength As Integer

>     Set db = DBEngine.Workspaces(0).Databases(0)
>     Set rst = db.OpenRecordset("olive")
>      MyField = rst("link")

>     Do Until rst.EOF
>         If Len(MyField) > 30 Then
>         Debug.Print ("too long")
>         End If
>         rst.MoveNext
>         Loop

>     End Sub



Wed, 21 Jan 2004 00:28:59 GMT  
 Easy Loops question?
The word link is a reserve word.  You can't use it as a
field name
Quote:
>-----Original Message-----
>If anyone could tell me why this isn't working, I'd be
>very grateful.
>I have a table called 'olive' which contains a field
>called 'link'.
>I want to print the ID of each record in the 'link' field
>that is over a certain length.  Instead this code prints
>each ID for each record.

>Any help much appreciated.

>Sub MySub()
>    Dim db As Database
>    Dim rst As Recordset
>    Dim MyField As String
>    Dim MyLength As Integer

>    Set db = DBEngine.Workspaces(0).Databases(0)
>    Set rst = db.OpenRecordset("olive")
>     MyField = rst("link")

>    Do Until rst.EOF
>        If Len(MyField) > 30 Then
>        Debug.Print ("too long")
>        End If
>        rst.MoveNext
>        Loop

>    End Sub
>.



Wed, 21 Jan 2004 00:16:56 GMT  
 Easy Loops question?
All that is wrong is that you set the value of your string outside the loop.
The string will never change from the value of the first record. (see
correction)

Nigel Johnson


Quote:
> If anyone could tell me why this isn't working, I'd be
> very grateful.
> I have a table called 'olive' which contains a field
> called 'link'.
> I want to print the ID of each record in the 'link' field
> that is over a certain length.  Instead this code prints
> each ID for each record.

> Any help much appreciated.

> Sub MySub()
>     Dim db As Database
>     Dim rst As Recordset
>     Dim MyField As String
>     Dim MyLength As Integer

>     Set db = DBEngine.Workspaces(0).Databases(0)
>     Set rst = db.OpenRecordset("olive")
>      MyField = rst("link")

>     Do Until rst.EOF
>         MyField = rst("link")
>         If Len(MyField) > 30 Then
>         Debug.Print ("too long")
>         End If
>         rst.MoveNext
>      Loop

>     End Sub



Tue, 20 Jan 2004 21:16:53 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. EASY EASY EASY question

2. help easy easy question

3. easy filecopy question...but maybe not so easy

4. Loops and Records (probably easy)

5. easy question

6. bitmaps, easy question

7. Public functions, easy question

8. Easy question

9. I'm sure that this is an easy question

10. easy question

11. help new to VBA, easy question

12. quick and easy question....please help!

 

 
Powered by phpBB® Forum Software