Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i used this code it shows error that is "EOF if required for move next record set and if i call "while not rs.EOF" on begging of for loop then loop is not execute
what is error in this code.
I want to find smallest number from access database using loop (OR any simple method if you know)
if you have any other method to find smallest number from database then please tell me...?
VB
Dim constr
constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\xyz.mdb;Persist Security Info=False"
Dim con As New ADODB.Connection
         con.Open constr
         Dim rs As New ADODB.Recordset
         rs.Open "Select * from xyz where abc ='A'", con, adOpenDynamic, adLockOptimistic

Dim Aray(1 To 10) As Integer
Dim sn As Integer
Dim i
Dim a
For i = 1 To 10
Aray(i) = rs("xyz")
rs.movenext
Next
sn = Aray(1)
For a = 1 To 10
If Aray(a) < sn Then sn = Aray(a)
Next
MsgBox "The smalles number is: " & sn, vbInformation
rs. closed
con.closed
Posted
Updated 29-Nov-11 7:59am
v2
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 12:52pm    
Why 10? :-)
--SA

1 solution

There's no need for you to use an array. I assume this is a homework question, since you explicitly want to find out how to locate the smallest number using a loop... You can get it directly from the database by using the min() function.

Here's a code snippet for how to go about it - you will need to pad it out as required.

VB
' Get the initial value
sn = rs("xyz")
rs.movenext

' While not eof, cycle through and check the rest
do while not rs.eof
  if sn < rs("xyz") then
    ' It's smaller, better record that value instead
    sn = rs("xyz")
  end if
  
  ' Move to next record
  rs.movenext
loop
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900