Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 18013
Number of posts: 55386

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Why dont i come out of loop successfully Posted by sonu_amit on 29 Apr 2005 at 5:06 AM
Hi Folks,

I am running with a problem.
I have been retrieving values from a database and printing them in an excel file. I have sucessfully opened the file and i try to print values into it but when i try coming out of it i get any error message that reads like either the eof or bof has been encountered or the current field has been deleted. Even After Many debugs i fail to come out of it.
I dont know how to move further.

I show u the codes please help me debug it..............
Thanks........
'----------------------------------------------------------------------

  rsAgent.Open "Select Count(Disp_Id) From AgentDispoDetail Where WorkGroup_Id = 10 and CallStartDt Between '" & Day(login_dt) & "' And '" & Day(logend_dt) & "'", conConnection, adOpenDynamic, adLockReadOnly, adCmdText
 ' MsgBox "rsAgent Opened Successfully"
  
 ' MsgBox login_dt
 ' MsgBox logend_dt
'    rsAgent.MoveFirst
'    While Not rsAgent.EOF
'    If rsAgent.EOF = True Then
'    End
'    Else
'    rsAgent.MoveFirst

Do While Not rsAgent.EOF
    For Row = 3 To rsAgent.RecordCount + 3 Step 1
        MsgBox Row
        On Error Resume Next
        wksheet.Cells(Row + 1, col + 5) = rsAgent.Fields(0)
        tmp = rsAgent.Fields(0)
        total = total + tmp
        MsgBox total
        rsAgent.MoveNext
    Next Row
Loop
    rsAgent.Close
    wksheet.Cells(Row, col + 5) = total
    MsgBox " I am Closing The File"
'    dt = Now
    
    '-------------------------------------------------
    ' Close The Workbook After Saving It.
    '-------------------------------------------------
     
    appExcel.ActiveWorkbook.SaveAs "C:\bgb\version1\Camp_Biz.xls"
    appExcel.ActiveWorkbook.Save
    ' Close Excel Application Completely.
    appExcel.Quit
    Set appExcel = Nothing


Report
Re: Why dont i come out of loop successfully Posted by infidel on 29 Apr 2005 at 7:59 AM
:
: 
: Do While Not rsAgent.EOF
:     For Row = 3 To rsAgent.RecordCount + 3 Step 1
:         MsgBox Row
:         On Error Resume Next
:         wksheet.Cells(Row + 1, col + 5) = rsAgent.Fields(0)
:         tmp = rsAgent.Fields(0)
:         total = total + tmp
:         MsgBox total
:         rsAgent.MoveNext
:     Next Row
: Loop
: 
: 


The problem is that you're doing the .MoveNext inside a For loop which itself is inside a Do While loop. Let's say your recordset has just one record in it. the Do While loop will start because you're not at .EOF. Then the For loop will start with Row = 3. It will do whatever and then .MoveNext. Since the recordset only had one record, now you're on EOF. The next iteration of the For loop will raise an error trying to reference any of the recordset's fields because you're on EOF, not a record.

Bottom line, it's rarely a good idea to use a For loop to iterate through a recordset. Something like this would be better:

    Do Until rsAgent.EOF
        tmp = rsAgent.Fields(0)
        wksheet.Cells(rsAgent.AbsolutePosition + 4, col + 5) = tmp
        total = total + tmp
        MsgBox total
        rsAgent.MoveNext
    Loop



infidel

$ select * from users where clue > 0
no rows returned





 

Recent Jobs