Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Why dont i come out of loop successfully

sonu_amitsonu_amit Posts: 37Member
[b]Hi Folks[/b],

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........
'----------------------------------------------------------------------
[code]
[green]
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:gbversion1Camp_Biz.xls"
appExcel.ActiveWorkbook.Save
' Close Excel Application Completely.
appExcel.Quit
Set appExcel = Nothing
[/green]
[/code]

Comments

  • infidelinfidel Posts: 2,900Member
    : [code]
    : [green]
    : 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
    : [/green]
    : [/code]

    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:

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


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.