CopyFromRecordset Object in vb.net

I am trying to export 2 linked SQL Server tables to Excel using VB.net. The IDNo is the linked key field.

I have problem with my code. It only exported the first IDNo to all tabs.

What I am trying to accomplish is:

IDNo should be in the tab name and next tab will rename the next IDNo.

SQL Server 2008 R2 tables:
Dbo.tblHeader:
      IDNo
      Description
      PDesc
      
Dbo.tblDetails
      IDNo
      Dept
      Last_Name
      First_Name
      Hire_Date

Excel.xlsx

This code is working but it only exports the first IDNo to all tabs.

Please help.

My current code:
Dim conn As New ADODB.Connection()
        Dim rs As ADODB.Recordset = Nothing
        Dim rs1 As ADODB.Recordset = Nothing
        Dim sql As String
        Dim lnStoreNo As String

        conn.Open("Data Source=Main;Initial Catalog=Main;Persist Security Info=True;User ID=sa;Password=hello;")
        conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
        rs = conn.Execute("dbo.tblHeader", , ADODB.CommandTypeEnum.adCmdTable)


        lnIDNo = rs.Fields("IDNo").Value

        'Create a new workbook in Excel.
        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)


        
        Dim shtCnt As Integer
        Dim j As Integer


        With oExcel
            .Visible = True
            shtCnt = .Worksheets.Count
            Do
                
                rs1 = conn.Execute("tblDetails", , ADODB.CommandTypeEnum.adCmdTable)



                .Worksheets(shtCnt).Activate()

                .ActiveSheet.Range("A1").Value = "KEY: DEPT:"
                .ActiveSheet.Rows("1:1").Font.Bold = True
                .ActiveSheet.Rows("1:1").Font.Size = 8
                .ActiveSheet.Rows("1:1").RowHeight = 10

                .ActiveSheet.Rows("3:3").RowHeight = 5

                .ActiveSheet.Range("A2").Value = "Assigned"
                .ActiveSheet.Rows("2:2").Font.Bold = True
                .ActiveSheet.Rows("2:2").Font.Size = 8
                .ActiveSheet.Rows("2:2").RowHeight = 10

                .ActiveSheet.Range("A4").Value = "REPORT"
                .ActiveSheet.Range("A4").Font.Bold = True
                .ActiveSheet.Range("A4").Font.Size = 20
                .ActiveSheet.Rows("4:4").RowHeight = 26.25
                .ActiveSheet.Rows("4:4").VerticalAlignment = -4108

                .ActiveSheet.Rows("9:9").RowHeight = 10

                .ActiveSheet.Range("D4").Value = rs.Fields("IDNo").Value
                .ActiveSheet.Range("D4").Font.Bold = True
                .ActiveSheet.Range("D4").Font.Size = 14

                .ActiveSheet.Range("A6").Value = rs.Fields("Description").Value
                .ActiveSheet.Range("A6").Font.Bold = True
                .ActiveSheet.Range("A6").Font.Size = 18
                .ActiveSheet.Range("A6").VerticalAlignment = -4108

                

                .ActiveSheet.Range("H4").Value = "Period"
                .ActiveSheet.Range("H4").Font.Bold = True
                .ActiveSheet.Range("H4").Font.Size = 14

                .ActiveSheet.Range("I4").Value = rs.Fields("PDesc").Value
                .ActiveSheet.Range("I4").Font.Bold = True
                .ActiveSheet.Range("I4").Font.Size = 14
               


                'Column Headers

                .ActiveSheet.Range("A10").Value = "IDNo"
                .ActiveSheet.Range("A10").Font.Size = 12
                .ActiveSheet.Range("A10").Font.Bold = True
                .ActiveSheet.Range("A10").Borders.LineStyle = 1
                .ActiveSheet.Range("A10").HorizontalAlignment = -4108
                .ActiveSheet.Range("A10").VerticalAlignment = -4108

                .ActiveSheet.Range("B10").Value = "Dept"
                .ActiveSheet.Range("B10").Font.Size = 12
                .ActiveSheet.Range("B10").Font.Bold = True
                .ActiveSheet.Range("B10").Borders.LineStyle = 1
                .ActiveSheet.Range("B10").HorizontalAlignment = -4108
                .ActiveSheet.Range("B10").VerticalAlignment = -4108

                .ActiveSheet.Range("C10").Value = "Last Name"
                .ActiveSheet.Range("C10").Font.Size = 12
                .ActiveSheet.Range("C10").Font.Bold = True
                .ActiveSheet.Range("C10").Borders.LineStyle = 1
                .ActiveSheet.Range("C10").HorizontalAlignment = -4108
                .ActiveSheet.Range("C10").VerticalAlignment = -4108
                .ActiveSheet.Range("C10").Columns.AutoFit()

                .ActiveSheet.Range("D10").Value = "First Name"
                .ActiveSheet.Range("D10").Font.Size = 12
                .ActiveSheet.Range("D10").Font.Bold = True
                .ActiveSheet.Range("D10").Borders.LineStyle = 1
                .ActiveSheet.Range("D10").HorizontalAlignment = -4108
                .ActiveSheet.Range("D10").VerticalAlignment = -4108
                .ActiveSheet.Range("D10").WrapText = True

                .ActiveSheet.Range("E10").Value = "Hire Date"
                .ActiveSheet.Range("E10").Font.Size = 12
                .ActiveSheet.Range("E10").Font.Bold = True
                .ActiveSheet.Range("E10").Borders.LineStyle = 1
                .ActiveSheet.Range("E10").HorizontalAlignment = -4108
                .ActiveSheet.Range("E10").VerticalAlignment = -4108
                

                .ActiveSheet.Rows("10:10").RowHeight = 30


                
                For j = 0 To rs1.Fields.Count - 1

                Next


                'Transfer the data to Excel 
                .ActiveSheet.Range("A11").CopyFromRecordset(rs1)
                .ActiveSheet.Range("A11:E14" & shtCnt).Font.Size = 12
                
                .ActiveSheet.name = rs.Fields("IDNo").Value  ‘This line will rename the tab Sheet Name by IDNo.
                .ActiveSheet.Range("A11:C14" & shtCnt).Font.Size = 12
                .ActiveSheet.Range("C11:C14" & shtCnt).Columns.AutoFit()
                .ActiveSheet.Range("A11:E14" & shtCnt).HorizontalAlignment = -4108
                .ActiveSheet.Range("A11:E14" & shtCnt).VerticalAlignment = -4108
                
                .ActiveSheet.Range("A11:E14" & shtCnt).RowHeight = 15
                .ActiveSheet.Range("A11:E14").Borders.LineStyle = 1


                oExcel.ActiveSheet.PageSetup.PrintArea = "$A1:$E14"

                
		‘this will add new sheet tab:
                If rs.EOF Then Exit Do
                .Worksheets.Add(after:=.Worksheets(shtCnt))
                shtCnt = shtCnt + 1

            Loop

        End With
        oSheet = Nothing
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
        GC.Collect()

        'Close the connection
        rs.Close()
        conn.Close()
    End Sub

Open in new window


I really appreciate all the help.
Output-With-My-Current-Code-In-VB-n.xlsx
Output-What-I-Need.xlsx
Queennie LAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
           Do
                
                rs1 = conn.Execute("tblDetails", , ADODB.CommandTypeEnum.adCmdTable)
.....
Loop

Open in new window


You have something ike the above where each time in the loop you recreate the recordset - hence always using the first record.
You need to have the conn.Execute before the do command AND inside the loop move to the next record whilst there is one
0
Queennie LAuthor Commented:
@AndyAinscow:

I tried that but still the first IDNo is exporting  to all Sheet tabs.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Please show your latest code.

ps.  Are you moving to the next record inside the loop?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Queennie LAuthor Commented:
@AndyAinscow:

Yes.

Thank you.


Dim conn As New ADODB.Connection()
        Dim rs As ADODB.Recordset = Nothing
        Dim rs1 As ADODB.Recordset = Nothing
        Dim sql As String
        Dim lnStoreNo As String

        conn.Open("Data Source=Main;Initial Catalog=Main;Persist Security Info=True;User ID=sa;Password=hello;")
        conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
        rs = conn.Execute("dbo.tblHeader", , ADODB.CommandTypeEnum.adCmdTable)


        lnIDNo = rs.Fields("IDNo").Value

        'Create a new workbook in Excel.
        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)


        
        Dim shtCnt As Integer
        Dim j As Integer


        With oExcel
            .Visible = True
            shtCnt = .Worksheets.Count

              rs1 = conn.Execute("tblDetails", , ADODB.CommandTypeEnum.adCmdTable)

            Do
                



                .Worksheets(shtCnt).Activate()

                .ActiveSheet.Range("A1").Value = "KEY: DEPT:"
                .ActiveSheet.Rows("1:1").Font.Bold = True
                .ActiveSheet.Rows("1:1").Font.Size = 8
                .ActiveSheet.Rows("1:1").RowHeight = 10

                .ActiveSheet.Rows("3:3").RowHeight = 5

                .ActiveSheet.Range("A2").Value = "Assigned"
                .ActiveSheet.Rows("2:2").Font.Bold = True
                .ActiveSheet.Rows("2:2").Font.Size = 8
                .ActiveSheet.Rows("2:2").RowHeight = 10

                .ActiveSheet.Range("A4").Value = "REPORT"
                .ActiveSheet.Range("A4").Font.Bold = True
                .ActiveSheet.Range("A4").Font.Size = 20
                .ActiveSheet.Rows("4:4").RowHeight = 26.25
                .ActiveSheet.Rows("4:4").VerticalAlignment = -4108

                .ActiveSheet.Rows("9:9").RowHeight = 10

                .ActiveSheet.Range("D4").Value = rs.Fields("IDNo").Value
                .ActiveSheet.Range("D4").Font.Bold = True
                .ActiveSheet.Range("D4").Font.Size = 14

		   .ActiveSheet.Range("A6").Value = rs.Fields("Description").Value
                .ActiveSheet.Range("A6").Font.Bold = True
                .ActiveSheet.Range("A6").Font.Size = 18
                .ActiveSheet.Range("A6").VerticalAlignment = -4108

                

                .ActiveSheet.Range("H4").Value = "Period"
                .ActiveSheet.Range("H4").Font.Bold = True
                .ActiveSheet.Range("H4").Font.Size = 14

                .ActiveSheet.Range("I4").Value = rs.Fields("PDesc").Value
                .ActiveSheet.Range("I4").Font.Bold = True
                .ActiveSheet.Range("I4").Font.Size = 14
                ' .ActiveSheet.Range("I4").Font.name = Tahoma



                'Column Headers

                .ActiveSheet.Range("A10").Value = "IDNo"
                .ActiveSheet.Range("A10").Font.Size = 12
                .ActiveSheet.Range("A10").Font.Bold = True
                .ActiveSheet.Range("A10").Borders.LineStyle = 1
                .ActiveSheet.Range("A10").HorizontalAlignment = -4108
                .ActiveSheet.Range("A10").VerticalAlignment = -4108

                .ActiveSheet.Range("B10").Value = "Dept"
                .ActiveSheet.Range("B10").Font.Size = 12
                .ActiveSheet.Range("B10").Font.Bold = True
                .ActiveSheet.Range("B10").Borders.LineStyle = 1
                .ActiveSheet.Range("B10").HorizontalAlignment = -4108
                .ActiveSheet.Range("B10").VerticalAlignment = -4108

                .ActiveSheet.Range("C10").Value = "Last Name"
                .ActiveSheet.Range("C10").Font.Size = 12
                .ActiveSheet.Range("C10").Font.Bold = True
                .ActiveSheet.Range("C10").Borders.LineStyle = 1
                .ActiveSheet.Range("C10").HorizontalAlignment = -4108
                .ActiveSheet.Range("C10").VerticalAlignment = -4108
                .ActiveSheet.Range("C10").Columns.AutoFit()

                .ActiveSheet.Range("D10").Value = "First Name"
                .ActiveSheet.Range("D10").Font.Size = 12
                .ActiveSheet.Range("D10").Font.Bold = True
                .ActiveSheet.Range("D10").Borders.LineStyle = 1
                .ActiveSheet.Range("D10").HorizontalAlignment = -4108
                .ActiveSheet.Range("D10").VerticalAlignment = -4108
                .ActiveSheet.Range("D10").WrapText = True

                .ActiveSheet.Range("E10").Value = "Hire Date"
                .ActiveSheet.Range("E10").Font.Size = 12
                .ActiveSheet.Range("E10").Font.Bold = True
                .ActiveSheet.Range("E10").Borders.LineStyle = 1
                .ActiveSheet.Range("E10").HorizontalAlignment = -4108
                .ActiveSheet.Range("E10").VerticalAlignment = -4108
                

                .ActiveSheet.Rows("10:10").RowHeight = 30


                
                For j = 0 To rs1.Fields.Count - 1

                Next


                'Transfer the data to Excel 
                .ActiveSheet.Range("A11").CopyFromRecordset(rs1)
                .ActiveSheet.Range("A11:E14" & shtCnt).Font.Size = 12
                
                .ActiveSheet.name = rs.Fields("IDNo").Value  ‘This line will rename the tab Sheet Name by IDNo.
                .ActiveSheet.Range("A11:C14" & shtCnt).Font.Size = 12
                .ActiveSheet.Range("C11:C14" & shtCnt).Columns.AutoFit()
                .ActiveSheet.Range("A11:E14" & shtCnt).HorizontalAlignment = -4108
                .ActiveSheet.Range("A11:E14" & shtCnt).VerticalAlignment = -4108
                
                .ActiveSheet.Range("A11:E14" & shtCnt).RowHeight = 15
                .ActiveSheet.Range("A11:E14").Borders.LineStyle = 1


                oExcel.ActiveSheet.PageSetup.PrintArea = "$A1:$E14"

                
		‘this will add new sheet tab:
                If rs.EOF Then Exit Do
                .Worksheets.Add(after:=.Worksheets(shtCnt))
                shtCnt = shtCnt + 1

	rs1.MoveNext()


            Loop

        End With
        oSheet = Nothing
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
        GC.Collect()

        'Close the connection
        rs.Close()
        conn.Close()
    End Sub

Open in new window

0
AndyAinscowFreelance programmer / ConsultantCommented:
You have two recordsets rs and rs1.  Please check you are moving to the next record in the one you want to.
0
Queennie LAuthor Commented:
@AndyAinscow:

I change to rs1.Movenext to rs.Movenext

It shows the IDNo but it did not rename the sheet tab to the IDNo.

This line of code supposed to do the trick:

.ActiveSheet.name = rs.Fields("IDNo").Value  


Latest Code:

 
Dim conn As New ADODB.Connection()
        Dim rs As ADODB.Recordset = Nothing
        Dim rs1 As ADODB.Recordset = Nothing
        Dim sql As String
        Dim lnStoreNo As String

        conn.Open("Data Source=Main;Initial Catalog=Main;Persist Security Info=True;User ID=sa;Password=hello;")
        conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
        rs = conn.Execute("dbo.tblHeader", , ADODB.CommandTypeEnum.adCmdTable)


        lnIDNo = rs.Fields("IDNo").Value

        'Create a new workbook in Excel.
        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)


        
        Dim shtCnt As Integer
        Dim j As Integer


        With oExcel
            .Visible = True
            shtCnt = .Worksheets.Count

              rs1 = conn.Execute("tblDetails", , ADODB.CommandTypeEnum.adCmdTable)

            Do
                



                .Worksheets(shtCnt).Activate()

                .ActiveSheet.Range("A1").Value = "KEY: DEPT:"
                .ActiveSheet.Rows("1:1").Font.Bold = True
                .ActiveSheet.Rows("1:1").Font.Size = 8
                .ActiveSheet.Rows("1:1").RowHeight = 10

                .ActiveSheet.Rows("3:3").RowHeight = 5

                .ActiveSheet.Range("A2").Value = "Assigned"
                .ActiveSheet.Rows("2:2").Font.Bold = True
                .ActiveSheet.Rows("2:2").Font.Size = 8
                .ActiveSheet.Rows("2:2").RowHeight = 10

                .ActiveSheet.Range("A4").Value = "REPORT"
                .ActiveSheet.Range("A4").Font.Bold = True
                .ActiveSheet.Range("A4").Font.Size = 20
                .ActiveSheet.Rows("4:4").RowHeight = 26.25
                .ActiveSheet.Rows("4:4").VerticalAlignment = -4108

                .ActiveSheet.Rows("9:9").RowHeight = 10

                .ActiveSheet.Range("D4").Value = rs.Fields("IDNo").Value
                .ActiveSheet.Range("D4").Font.Bold = True
                .ActiveSheet.Range("D4").Font.Size = 14

		   .ActiveSheet.Range("A6").Value = rs.Fields("Description").Value
                .ActiveSheet.Range("A6").Font.Bold = True
                .ActiveSheet.Range("A6").Font.Size = 18
                .ActiveSheet.Range("A6").VerticalAlignment = -4108

                

                .ActiveSheet.Range("H4").Value = "Period"
                .ActiveSheet.Range("H4").Font.Bold = True
                .ActiveSheet.Range("H4").Font.Size = 14

                .ActiveSheet.Range("I4").Value = rs.Fields("PDesc").Value
                .ActiveSheet.Range("I4").Font.Bold = True
                .ActiveSheet.Range("I4").Font.Size = 14
                ' .ActiveSheet.Range("I4").Font.name = Tahoma



                'Column Headers

                .ActiveSheet.Range("A10").Value = "IDNo"
                .ActiveSheet.Range("A10").Font.Size = 12
                .ActiveSheet.Range("A10").Font.Bold = True
                .ActiveSheet.Range("A10").Borders.LineStyle = 1
                .ActiveSheet.Range("A10").HorizontalAlignment = -4108
                .ActiveSheet.Range("A10").VerticalAlignment = -4108

                .ActiveSheet.Range("B10").Value = "Dept"
                .ActiveSheet.Range("B10").Font.Size = 12
                .ActiveSheet.Range("B10").Font.Bold = True
                .ActiveSheet.Range("B10").Borders.LineStyle = 1
                .ActiveSheet.Range("B10").HorizontalAlignment = -4108
                .ActiveSheet.Range("B10").VerticalAlignment = -4108

                .ActiveSheet.Range("C10").Value = "Last Name"
                .ActiveSheet.Range("C10").Font.Size = 12
                .ActiveSheet.Range("C10").Font.Bold = True
                .ActiveSheet.Range("C10").Borders.LineStyle = 1
                .ActiveSheet.Range("C10").HorizontalAlignment = -4108
                .ActiveSheet.Range("C10").VerticalAlignment = -4108
                .ActiveSheet.Range("C10").Columns.AutoFit()

                .ActiveSheet.Range("D10").Value = "First Name"
                .ActiveSheet.Range("D10").Font.Size = 12
                .ActiveSheet.Range("D10").Font.Bold = True
                .ActiveSheet.Range("D10").Borders.LineStyle = 1
                .ActiveSheet.Range("D10").HorizontalAlignment = -4108
                .ActiveSheet.Range("D10").VerticalAlignment = -4108
                .ActiveSheet.Range("D10").WrapText = True

                .ActiveSheet.Range("E10").Value = "Hire Date"
                .ActiveSheet.Range("E10").Font.Size = 12
                .ActiveSheet.Range("E10").Font.Bold = True
                .ActiveSheet.Range("E10").Borders.LineStyle = 1
                .ActiveSheet.Range("E10").HorizontalAlignment = -4108
                .ActiveSheet.Range("E10").VerticalAlignment = -4108
                

                .ActiveSheet.Rows("10:10").RowHeight = 30


                
                For j = 0 To rs1.Fields.Count - 1

                Next


                'Transfer the data to Excel 
                .ActiveSheet.Range("A11").CopyFromRecordset(rs1)
                .ActiveSheet.Range("A11:E14" & shtCnt).Font.Size = 12
                
                .ActiveSheet.name = rs.Fields("IDNo").Value  ‘This line will rename the tab Sheet Name by IDNo.
                .ActiveSheet.Range("A11:C14" & shtCnt).Font.Size = 12
                .ActiveSheet.Range("C11:C14" & shtCnt).Columns.AutoFit()
                .ActiveSheet.Range("A11:E14" & shtCnt).HorizontalAlignment = -4108
                .ActiveSheet.Range("A11:E14" & shtCnt).VerticalAlignment = -4108
                
                .ActiveSheet.Range("A11:E14" & shtCnt).RowHeight = 15
                .ActiveSheet.Range("A11:E14").Borders.LineStyle = 1


                oExcel.ActiveSheet.PageSetup.PrintArea = "$A1:$E14"

                
		‘this will add new sheet tab:
                If rs.EOF Then Exit Do
                .Worksheets.Add(after:=.Worksheets(shtCnt))
                shtCnt = shtCnt + 1

	rs.MoveNext()


            Loop
	rs.Close()

        End With
        oSheet = Nothing
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
        GC.Collect()

        'Close the connection
        rs.Close()
        conn.Close()
    End Sub

Open in new window

Output-.xlsx
0
AndyAinscowFreelance programmer / ConsultantCommented:
Does changing  this:
                'Transfer the data to Excel 
                .ActiveSheet.Range("A11").CopyFromRecordset(rs1)
                .ActiveSheet.Range("A11:E14" & shtCnt).Font.Size = 12
                
                .ActiveSheet.name = rs.Fields("IDNo").Value  ‘This line will rename the tab Sheet Name by IDNo.

Open in new window


to this:
                'Transfer the data to Excel 
                .ActiveSheet.name = rs.Fields("IDNo").Value  ‘This line will rename the tab Sheet Name by IDNo.

                 .ActiveSheet.Range("A11").CopyFromRecordset(rs1)
                .ActiveSheet.Range("A11:E14" & shtCnt).Font.Size = 12
                

Open in new window

make any difference?  (Just wondering if the CopyFromRecordset resets to first record)
0
Queennie LAuthor Commented:
@AndyAinscow:

Is there a way the rs1 to execute the tblDetails where IDNo = rs.IDNo?

I changed this code to:

  If rs.EOF Then Exit Do
                .Worksheets.Add(after:=.Worksheets(shtCnt))
                shtCnt = shtCnt + 1

                rs.MoveNext()

            Loop
            rs1.MoveNext()
            rs.Close()
            rs1.Close()

But the tblDetails is not populating only the first IDNo to all the tabs.

Is there something I did wrong?

Latest Code and output:

Dim conn As New ADODB.Connection()
        Dim rs As ADODB.Recordset = Nothing
        Dim rs1 As ADODB.Recordset = Nothing
        Dim sql As String
        Dim lnStoreNo As String

        conn.Open("Data Source=Main;Initial Catalog=Main;Persist Security Info=True;User ID=sa;Password=hello;")
        conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
        rs = conn.Execute("dbo.tblHeader", , ADODB.CommandTypeEnum.adCmdTable)


        lnIDNo = rs.Fields("IDNo").Value

        'Create a new workbook in Excel.
        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)


        
        Dim shtCnt As Integer
        Dim j As Integer


        With oExcel
            .Visible = True
            shtCnt = .Worksheets.Count

              rs1 = conn.Execute("tblDetails", , ADODB.CommandTypeEnum.adCmdTable)

            Do
                



                .Worksheets(shtCnt).Activate()

                .ActiveSheet.Range("A1").Value = "KEY: DEPT:"
                .ActiveSheet.Rows("1:1").Font.Bold = True
                .ActiveSheet.Rows("1:1").Font.Size = 8
                .ActiveSheet.Rows("1:1").RowHeight = 10

                .ActiveSheet.Rows("3:3").RowHeight = 5

                .ActiveSheet.Range("A2").Value = "Assigned"
                .ActiveSheet.Rows("2:2").Font.Bold = True
                .ActiveSheet.Rows("2:2").Font.Size = 8
                .ActiveSheet.Rows("2:2").RowHeight = 10

                .ActiveSheet.Range("A4").Value = "REPORT"
                .ActiveSheet.Range("A4").Font.Bold = True
                .ActiveSheet.Range("A4").Font.Size = 20
                .ActiveSheet.Rows("4:4").RowHeight = 26.25
                .ActiveSheet.Rows("4:4").VerticalAlignment = -4108

                .ActiveSheet.Rows("9:9").RowHeight = 10

                .ActiveSheet.Range("D4").Value = rs.Fields("IDNo").Value
                .ActiveSheet.Range("D4").Font.Bold = True
                .ActiveSheet.Range("D4").Font.Size = 14

		   .ActiveSheet.Range("A6").Value = rs.Fields("Description").Value
                .ActiveSheet.Range("A6").Font.Bold = True
                .ActiveSheet.Range("A6").Font.Size = 18
                .ActiveSheet.Range("A6").VerticalAlignment = -4108

                

                .ActiveSheet.Range("H4").Value = "Period"
                .ActiveSheet.Range("H4").Font.Bold = True
                .ActiveSheet.Range("H4").Font.Size = 14

                .ActiveSheet.Range("I4").Value = rs.Fields("PDesc").Value
                .ActiveSheet.Range("I4").Font.Bold = True
                .ActiveSheet.Range("I4").Font.Size = 14
                ' .ActiveSheet.Range("I4").Font.name = Tahoma



                'Column Headers

                .ActiveSheet.Range("A10").Value = "IDNo"
                .ActiveSheet.Range("A10").Font.Size = 12
                .ActiveSheet.Range("A10").Font.Bold = True
                .ActiveSheet.Range("A10").Borders.LineStyle = 1
                .ActiveSheet.Range("A10").HorizontalAlignment = -4108
                .ActiveSheet.Range("A10").VerticalAlignment = -4108

                .ActiveSheet.Range("B10").Value = "Dept"
                .ActiveSheet.Range("B10").Font.Size = 12
                .ActiveSheet.Range("B10").Font.Bold = True
                .ActiveSheet.Range("B10").Borders.LineStyle = 1
                .ActiveSheet.Range("B10").HorizontalAlignment = -4108
                .ActiveSheet.Range("B10").VerticalAlignment = -4108

                .ActiveSheet.Range("C10").Value = "Last Name"
                .ActiveSheet.Range("C10").Font.Size = 12
                .ActiveSheet.Range("C10").Font.Bold = True
                .ActiveSheet.Range("C10").Borders.LineStyle = 1
                .ActiveSheet.Range("C10").HorizontalAlignment = -4108
                .ActiveSheet.Range("C10").VerticalAlignment = -4108
                .ActiveSheet.Range("C10").Columns.AutoFit()

                .ActiveSheet.Range("D10").Value = "First Name"
                .ActiveSheet.Range("D10").Font.Size = 12
                .ActiveSheet.Range("D10").Font.Bold = True
                .ActiveSheet.Range("D10").Borders.LineStyle = 1
                .ActiveSheet.Range("D10").HorizontalAlignment = -4108
                .ActiveSheet.Range("D10").VerticalAlignment = -4108
                .ActiveSheet.Range("D10").WrapText = True

                .ActiveSheet.Range("E10").Value = "Hire Date"
                .ActiveSheet.Range("E10").Font.Size = 12
                .ActiveSheet.Range("E10").Font.Bold = True
                .ActiveSheet.Range("E10").Borders.LineStyle = 1
                .ActiveSheet.Range("E10").HorizontalAlignment = -4108
                .ActiveSheet.Range("E10").VerticalAlignment = -4108
                

                .ActiveSheet.Rows("10:10").RowHeight = 30


                
                For j = 0 To rs1.Fields.Count - 1

                Next


                'Transfer the data to Excel 
                .ActiveSheet.Range("A11").CopyFromRecordset(rs1)
                .ActiveSheet.Range("A11:E14" & shtCnt).Font.Size = 12
                
                .ActiveSheet.name = rs.Fields("IDNo").Value  ‘This line will rename the tab Sheet Name by IDNo.
                .ActiveSheet.Range("A11:C14" & shtCnt).Font.Size = 12
                .ActiveSheet.Range("C11:C14" & shtCnt).Columns.AutoFit()
                .ActiveSheet.Range("A11:E14" & shtCnt).HorizontalAlignment = -4108
                .ActiveSheet.Range("A11:E14" & shtCnt).VerticalAlignment = -4108
                
                .ActiveSheet.Range("A11:E14" & shtCnt).RowHeight = 15
                .ActiveSheet.Range("A11:E14").Borders.LineStyle = 1


                oExcel.ActiveSheet.PageSetup.PrintArea = "$A1:$E14"

                
		‘this will add new sheet tab:
                If rs.EOF Then Exit Do
                .Worksheets.Add(after:=.Worksheets(shtCnt))
                shtCnt = shtCnt + 1

	rs.MoveNext()


            Loop
rs1.movenext
	rs.Close()
rs1.close()

        End With
        oSheet = Nothing
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
        GC.Collect()

        'Close the connection
        rs.Close()
        conn.Close()
    End Sub

Open in new window

Output-as-of-now-P2.xlsx
0
Queennie LAuthor Commented:
Any help please?
0
Queennie LAuthor Commented:
Error:

See attached.

            rs1.CursorLocation = ADODB.CursorLocationEnum.adUseClient
            rs1.CursorType = ADODB.CursorTypeEnum.adOpenStatic
            rs1.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
            rs1.Open("select * from tblDetails Where IDNo=' " & rs.Fields("IDNo").Value & "'", conn)
           

            Dim da As New System.Data.OleDb.OleDbDataAdapter()
            Dim ds As New DataSet()
            da.Fill(ds, rs1, "tblDetails")


Please help.
0
Queennie LAuthor Commented:
I forgot to upload the attached Error file.

thank you again.
Error.docx
0
AndyAinscowFreelance programmer / ConsultantCommented:
I'm puzzled.
You have said the problem is with the names of the tabs but then supply an excel workbook where they have the (correct?) naming but the contents of some cells appears to be the problem.  Exactly what is not working?

Have you checked the output of the query - maybe your code is working perfectly and the problem is elsewhere.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Queennie LAuthor Commented:
I got it working.

You  helped me with your excellent suggestions. The code is working, I just had to rearranged the code that I have.

Thank you for your time and help.

Have a wonderful day
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.