Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

End of records in a database

I have a VB program that is oping a recordset and looping until I run out. My problem is I do not know how to tell I am at the end of the records. Right now I just get an error and jump out. Here is the code:

Dim cn As New ADODB.Connection
Dim recSet As New ADODB.Recordset
Dim ConnectionString As String
Dim strSQL1 As String, strSQL2 As String
Dim i As Integer
Dim lvItem As ListItem
Dim sa As String
Dim sAssetFound As String
Dim j As Integer

On Error GoTo ErrorHandler2

Screen.MousePointer = vbHourglass

'Define connection string to connect to the database
ConnectionString = "PROVIDER=MSDASQL;dsn=Calibration Data;uid=LogInOut;pwd=loginout;"
cn.ConnectionString = ConnectionString

cn.Open
j = 1
sAssetFound = "1" 'just something to start
If cn.State = adStateOpen Then

If cn.State = adStateOpen Then
Do Until sAllTheModels(j) = ""
Do Until sAssetFound = ""
On Error GoTo GetNextModel
strSQL1 = "SELECT * FROM mt.Inventory A " & _
"WHERE A.I4203='" + sAllTheModels(j) + "'" & _
"AND A.I4230=0"
recSet.Open strSQL1, cn, adOpenDynamic, adLockOptimistic 'open for read / write using SQL string # 3
sAssetFound = CStr(recSet.Fields("I4201"))

recSet.Fields("I4254") = sAllTheData(j) 'change the Cal lab Std cal time
recSet.Fields("I4230") = 1 'change the integer
recSet.Update 'now update the database
recSet.Close 'close
Loop
GetNextModel: DoEvents
recSet.Close 'close ?
Err.Clear
j = j + 1
Loop
Else
MsgBox "The connection could not be made!", vbCritical, "Error"
GoTo ExitHandler2
End If

My problem is on the line: recSet.Open..... If I am at the end of the records I just get an error and jump out. What I need is a way to tell if I am at the end before this so that I can continue the first do loop.
[2510 byte] By [AM003295] at [2007-11-11 10:04:14]
# 1 Re: End of records in a database
I'm not sure I follow what your code is doing, but I've usually used the .EOF property to test whether I'm at the end of the recordset:

Do Until rs.EOF
' process current record
rs.MoveNext
Loop
Phil Weber at 2007-11-11 17:23:20 >
# 2 Re: End of records in a database
That fixed it. Thanks for the input.
AM003295 at 2007-11-11 17:24:20 >