Queennie L
asked on
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:
I really appreciate all the help.
Output-With-My-Current-Code-In-VB-n.xlsx
Output-What-I-Need.xlsx
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
I really appreciate all the help.
Output-With-My-Current-Code-In-VB-n.xlsx
Output-What-I-Need.xlsx
ASKER
@AndyAinscow:
I tried that but still the first IDNo is exporting to all Sheet tabs.
I tried that but still the first IDNo is exporting to all Sheet tabs.
Please show your latest code.
ps. Are you moving to the next record inside the loop?
ps. Are you moving to the next record inside the loop?
ASKER
@AndyAinscow:
Yes.
Thank you.
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
You have two recordsets rs and rs1. Please check you are moving to the next record in the one you want to.
ASKER
@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:
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
Output-.xlsx
Does changing this:
to 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.
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
make any difference? (Just wondering if the CopyFromRecordset resets to first record)
ASKER
@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:=.Wo rksheets(s htCnt))
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:
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:=.Wo
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
Output-as-of-now-P2.xlsx
ASKER
Any help please?
ASKER
Error:
See attached.
rs1.CursorLocation = ADODB.CursorLocationEnum.a dUseClient
rs1.CursorType = ADODB.CursorTypeEnum.adOpe nStatic
rs1.LockType = ADODB.LockTypeEnum.adLockB atchOptimi stic
rs1.Open("select * from tblDetails Where IDNo=' " & rs.Fields("IDNo").Value & "'", conn)
Dim da As New System.Data.OleDb.OleDbDat aAdapter()
Dim ds As New DataSet()
da.Fill(ds, rs1, "tblDetails")
Please help.
See attached.
rs1.CursorLocation = ADODB.CursorLocationEnum.a
rs1.CursorType = ADODB.CursorTypeEnum.adOpe
rs1.LockType = ADODB.LockTypeEnum.adLockB
rs1.Open("select * from tblDetails Where IDNo=' " & rs.Fields("IDNo").Value & "'", conn)
Dim da As New System.Data.OleDb.OleDbDat
Dim ds As New DataSet()
da.Fill(ds, rs1, "tblDetails")
Please help.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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