D
DCPan
Hi,
From some strange reason, my code for dynamically formatting multiple tabs
work until I have to "sort" the worksheet.
If I strip out the code for sorting, everything works fine....
HELP!
Public Sub Download_SKU()
'Last Updated On 02/06/2008 by DCPan
'Declare Variables
Dim rst_SKU_No As New ADODB.Recordset
'Declare variables to format the download
Dim objXLApp As Object
Dim objXLBook As Object
Dim objXLSheet01 As Object
Dim strWorkSht As String
'Use the local connection
Call Local_Connect
'Open the recordset with data from the server table specified
rst_SKU_Npen "SELECT DISTINCT tbl_All_Entries.SKU FROM
tbl_All_Entries", _
objLocalDB, adOpenKeyset
'If the recordset is empty
If rst_SKU_No.BOF And rst_SKU_No.EOF Then
Else
'Scroll to the first record
rst_SKU_No.MoveFirst
Do Until rst_SKU_No.EOF = True
'Insert the line items
DoCmd.RunSQL "SELECT tbl_All_Entries.Entry_Type, " & _
"tbl_All_Entries.RA_No, " & _
"tbl_All_Entries.Claim_No, " & _
"tbl_All_Entries.SKU, " & _
"tbl_All_Entries.Prod_Desc, " & _
"tbl_All_Entries.Qty, " & _
"tbl_All_Entries.Unit_Price, " & _
"tbl_All_Entries.Total " & _
"INTO " & rst_SKU_No!SKU & " " & _
"FROM tbl_All_Entries " & _
"WHERE (((tbl_All_Entries.SKU) = '" & rst_SKU_No!SKU & "'))"
& _
"ORDER BY tbl_All_Entries.Unit_Price"
'Insert the line totals
DoCmd.RunSQL "INSERT INTO " & rst_SKU_No!SKU & " " & _
"( Entry_Type, SKU, Prod_Desc, Qty, Unit_Price, Total )"
& _
"SELECT tbl_All_Entries.Entry_Type, " & _
"tbl_All_Entries.SKU, " & _
"tbl_All_Entries.Prod_Desc, " & _
"Sum(tbl_All_Entries.Qty) AS SumOfQty, " & _
"tbl_All_Entries.Unit_Price, " & _
"Sum(tbl_All_Entries.Total) AS SumOfTotal " & _
"FROM tbl_All_Entries " & _
"GROUP BY tbl_All_Entries.Entry_Type, " & _
"tbl_All_Entries.SKU, " & _
"tbl_All_Entries.Prod_Desc, " & _
"tbl_All_Entries.Unit_Price " & _
"HAVING (((tbl_All_Entries.SKU)='" & rst_SKU_No!SKU & "'))"
'Download tabs
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
rst_SKU_No!SKU, strSaveFile, True
'Format the tabs
strWorkSht = rst_SKU_No!SKU
'Set the objects to format
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open(strSaveFile)
Set objXLSheet01 = objXLBook.Worksheets(strWorkSht)
'1 = black
'2 = white
'3 = red
'5 = blue
'10 = green
'13 = purple
'Format the headers
objXLSheet01.Range("A1:H1").Font.Bold = True
objXLSheet01.Range("A1:H1").HorizontalAlignment = xlCenter
'AutoFit the columns
objXLSheet01.Range("A:H").Columns.AutoFit
'Activate Sheet
objXLSheet01.Activate
'Sort columns
objXLSheet01.Columns("A:H").Select
Selection.Sort Key1:=Range("G2"), Order1:=xlAscending,
Key2:=Range("A2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,
MatchCase:= _
False, Orientation:=xlTopToBottom,
DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
'Freeze Panes
objXLSheet01.Range("2:2").Select
objXLApp.ActiveWindow.FreezePanes = True
'Set the cursor back on the first cell
objXLSheet01.Range("A1:A1").Select
'Clean-Up
objXLBook.Save
objXLBook.Close
objXLApp.Quit
Set objXLSheet01 = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
'Drop the temporary table
DoCmd.RunSQL "Drop Table " & rst_SKU_No!SKU
'Move to the next record
rst_SKU_No.MoveNext
Loop
End If
'Close Adodb Recordset
rst_SKU_No.Close
Set rst_SKU_No = Nothing
'Close Adodb Connection
objLocalDB.Close
Set objLocalDB = Nothing
Exit_Code:
Exit Sub
End Sub
From some strange reason, my code for dynamically formatting multiple tabs
work until I have to "sort" the worksheet.
If I strip out the code for sorting, everything works fine....
HELP!
Public Sub Download_SKU()
'Last Updated On 02/06/2008 by DCPan
'Declare Variables
Dim rst_SKU_No As New ADODB.Recordset
'Declare variables to format the download
Dim objXLApp As Object
Dim objXLBook As Object
Dim objXLSheet01 As Object
Dim strWorkSht As String
'Use the local connection
Call Local_Connect
'Open the recordset with data from the server table specified
rst_SKU_Npen "SELECT DISTINCT tbl_All_Entries.SKU FROM
tbl_All_Entries", _
objLocalDB, adOpenKeyset
'If the recordset is empty
If rst_SKU_No.BOF And rst_SKU_No.EOF Then
Else
'Scroll to the first record
rst_SKU_No.MoveFirst
Do Until rst_SKU_No.EOF = True
'Insert the line items
DoCmd.RunSQL "SELECT tbl_All_Entries.Entry_Type, " & _
"tbl_All_Entries.RA_No, " & _
"tbl_All_Entries.Claim_No, " & _
"tbl_All_Entries.SKU, " & _
"tbl_All_Entries.Prod_Desc, " & _
"tbl_All_Entries.Qty, " & _
"tbl_All_Entries.Unit_Price, " & _
"tbl_All_Entries.Total " & _
"INTO " & rst_SKU_No!SKU & " " & _
"FROM tbl_All_Entries " & _
"WHERE (((tbl_All_Entries.SKU) = '" & rst_SKU_No!SKU & "'))"
& _
"ORDER BY tbl_All_Entries.Unit_Price"
'Insert the line totals
DoCmd.RunSQL "INSERT INTO " & rst_SKU_No!SKU & " " & _
"( Entry_Type, SKU, Prod_Desc, Qty, Unit_Price, Total )"
& _
"SELECT tbl_All_Entries.Entry_Type, " & _
"tbl_All_Entries.SKU, " & _
"tbl_All_Entries.Prod_Desc, " & _
"Sum(tbl_All_Entries.Qty) AS SumOfQty, " & _
"tbl_All_Entries.Unit_Price, " & _
"Sum(tbl_All_Entries.Total) AS SumOfTotal " & _
"FROM tbl_All_Entries " & _
"GROUP BY tbl_All_Entries.Entry_Type, " & _
"tbl_All_Entries.SKU, " & _
"tbl_All_Entries.Prod_Desc, " & _
"tbl_All_Entries.Unit_Price " & _
"HAVING (((tbl_All_Entries.SKU)='" & rst_SKU_No!SKU & "'))"
'Download tabs
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
rst_SKU_No!SKU, strSaveFile, True
'Format the tabs
strWorkSht = rst_SKU_No!SKU
'Set the objects to format
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open(strSaveFile)
Set objXLSheet01 = objXLBook.Worksheets(strWorkSht)
'1 = black
'2 = white
'3 = red
'5 = blue
'10 = green
'13 = purple
'Format the headers
objXLSheet01.Range("A1:H1").Font.Bold = True
objXLSheet01.Range("A1:H1").HorizontalAlignment = xlCenter
'AutoFit the columns
objXLSheet01.Range("A:H").Columns.AutoFit
'Activate Sheet
objXLSheet01.Activate
'Sort columns
objXLSheet01.Columns("A:H").Select
Selection.Sort Key1:=Range("G2"), Order1:=xlAscending,
Key2:=Range("A2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,
MatchCase:= _
False, Orientation:=xlTopToBottom,
DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
'Freeze Panes
objXLSheet01.Range("2:2").Select
objXLApp.ActiveWindow.FreezePanes = True
'Set the cursor back on the first cell
objXLSheet01.Range("A1:A1").Select
'Clean-Up
objXLBook.Save
objXLBook.Close
objXLApp.Quit
Set objXLSheet01 = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
'Drop the temporary table
DoCmd.RunSQL "Drop Table " & rst_SKU_No!SKU
'Move to the next record
rst_SKU_No.MoveNext
Loop
End If
'Close Adodb Recordset
rst_SKU_No.Close
Set rst_SKU_No = Nothing
'Close Adodb Connection
objLocalDB.Close
Set objLocalDB = Nothing
Exit_Code:
Exit Sub
End Sub