Results 1 to 4 of 4

Thread: Problem while copying data from recordset to excel

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    22

    Exclamation Problem while copying data from recordset to excel

    I am using below code

    cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & "C:\work\User Audit Tool\AuditReports\;" & _
    "Extended Properties=""Text;HDR=No;"""

    rs.Open "Select * from AuditReport.csv", cnt, adOpenStatic, adLockReadOnly, adCmdText
    Set objXLApp = CreateObject("Excel.Application")
    Set xlWb = objXLApp.Workbooks.Add
    Set xlWs = xlWb.Worksheets("Sheet1")
    objXLApp.Visible = True

    i = 1
    With rs
    .MoveFirst
    Do Until .EOF
    xlWs.Cells(i, 1).CopyFromRecordset rs, 1
    i = i + 1
    'objWS.Range(objWS.Cells(1, 1), objWS.Cells(1, rs.Fields.Count)).Font.Bold = True


    If i > 10 And i < 21 Then
    Set xlWs = objXLApp.Worksheets.Add
    'objWS.Range(xlWs.Cells(i - 11, 1)).CopyFromRecordset rs
    xlWs.Cells(i - 10, 1).CopyFromRecordset rs
    End If
    If i > 20 Then
    Set xlWs = objXLApp.Worksheets.Add
    xlWs.Cells(i - 20, 1).CopyFromRecordset rs
    End If
    .MoveNext
    Loop
    End With

    But when i try to debug using F8 ,the debugger only moves between two statements below .MoveNext statement
    ie.
    Do Until .EOF
    xlWs.Cells(i, 1).CopyFromRecordset rs, 1

    But somehow it copies first 10 records to sheet 1 and the rest of them to sheet 2 ...don't know how can it add sheet without the cursor going into the sheet adding statements.

    Please help!!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problem while copying data from recordset to excel

    how many records in total?

    why have that part of the code to add sheets, if you want all the records in the same sheet?

    it would appear that for every record above 10 it should add an additional worksheet to the workbook
    Last edited by westconn1; May 16th, 2012 at 07:18 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    22

    Re: Problem while copying data from recordset to excel

    Quote Originally Posted by westconn1 View Post
    how many records in total?

    why have that part of the code to add sheets, if you want all the records in the same sheet?

    it would appear that for every record above 10 it should add an additional worksheet to the workbook
    I want to apply this code if the records exceed 65535 limit of excel sheet.
    But right now i am trying it for 130 records or so ,just to make sure they are multiple sheets after each 10 records.
    I am not sure if Recordset will be able to hold that many records or not,will it?

    So my problem is basically a .csv file with huge amount of data so if records exceed 65356 ,they are pasted on new sheet.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problem while copying data from recordset to excel

    If i > 10 And i < 21 Then
    in this every i between 10 and 21 would add a new sheet, when really you only want a new sheet when i = 10 (or really 65535) or multiple thereof

    vb Code:
    1. if i mod 10 = 0 then ' add new sheet (or i mod 65535)

    I am not sure if Recordset will be able to hold that many records or not,will it?
    i am not sure of any limitations in a recordset, that may be a question for the database forum and may depend on the database used, in any case there would be other methods to read from a csv file if necessary

    to allow for upgrade to later versions of excel you should use rows.count rather than the literal value of 65535
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width