deyken
05-07-2009, 07:02 AM
Hi Guys!
I have created a Master/Detail relationship between 2 tables in SQL Server and need to fill out the Master table data into specific fields on an Excel Sheet, then (using existing formatting) need to insert rows for each Detail record (perhaps with a For..Next statement) while retaining the existing formatting (borders, shades, fonts etc.).
How do I accomplish the row insertion and formatting with VBA?
Here is my Code attempt:
____________________________________________________________
PLDetail.Open "SELECT * FROM tblPACKING_LISTS_d WHERE tblPACKING_LISTS_d.PACKINGLIST_NO = " & PackingList, BSS_PL, adOpenDynamic, adLockReadOnly
If PLDetail.RecordCount <> 0 Then
For i = 0 To PLDetail.RecordCount - 1
Rows("8:8").Select
Selection.Insert Shift:=xlDown
Range("A8:G8").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Next i
Else
Sheet2.Cells(1, "A") = "No items for " & PackingList
End If
____________________________________________________________
PS: Sheet1 is my main formated document, but I am only testing code on Sheet2 so do not worry about mixed references of these two sheets...
I have created a Master/Detail relationship between 2 tables in SQL Server and need to fill out the Master table data into specific fields on an Excel Sheet, then (using existing formatting) need to insert rows for each Detail record (perhaps with a For..Next statement) while retaining the existing formatting (borders, shades, fonts etc.).
How do I accomplish the row insertion and formatting with VBA?
Here is my Code attempt:
____________________________________________________________
PLDetail.Open "SELECT * FROM tblPACKING_LISTS_d WHERE tblPACKING_LISTS_d.PACKINGLIST_NO = " & PackingList, BSS_PL, adOpenDynamic, adLockReadOnly
If PLDetail.RecordCount <> 0 Then
For i = 0 To PLDetail.RecordCount - 1
Rows("8:8").Select
Selection.Insert Shift:=xlDown
Range("A8:G8").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Next i
Else
Sheet2.Cells(1, "A") = "No items for " & PackingList
End If
____________________________________________________________
PS: Sheet1 is my main formated document, but I am only testing code on Sheet2 so do not worry about mixed references of these two sheets...