Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

controlling Excel through Access VBA- causing many problems

Status
Not open for further replies.

KellyK

Programmer
Mar 28, 2002
212
0
0
US
Hi all,
I'm having difficulty with Excel automation. I get the Excel spreadsheets populated correctly (until the error), but when I run the subroutine it causes wacky problems within Windows 95. For instance, I see that a series of 0 KB length files (with no extension) are being created with names like 'A5980000' in the same directory where I store my Excel files. Then about halfway through the process, any open program I have running becomes locked and in the Close Program dialog box it has [Not Responding] next to it. If I click on the Excel process and then 'End Task', all my programs are ready to go again. It does not make it through the program without this happening. I'm wondering if any of you gurus could take the time to look at my code and see if there are major bugs. Otherwise I'm at a loss. Thanks very much!
Code:
Private Sub cmdEmailReports_Click()
DoCmd.SetWarnings False

    Dim strPath As String
    Dim strFile As String
    Dim strTestFile As String               'to check if file exists
    Dim rstSales As ADODB.Recordset         'for all Sales Ids with activity
    Dim rstIndividual As ADODB.Recordset    'an individual Salesperson's data
    Dim WeeklyDetail As Excel.Worksheet     'for PST detail report
    Dim WeeklySummary As Excel.Worksheet    'for PST summary report
    Dim WeeklyPSTwb As Excel.Workbook
    Dim CurrRow As Integer                  'counter for Excel row
    Dim CurrCell As Integer                 'counter for Excel cell
    Dim f As Field                          'to insert field names into Excel spreadsheet
    Dim xlApp As Excel.Application
    
Set xlApp = CreateObject("Excel.Application")
Set rstSales = New ADODB.Recordset
Set rstIndividual = New ADODB.Recordset

strPath = "C:\data\temp\"

'Compile PST data
rstSales.Open "Select * from qselSalespeoplewithLoads", CurrentProject.Connection

Do Until rstSales.EOF
    Me.cboSales = rstSales!SalesCode
    
    '
    '  Compile Summary Data
    '
    rstIndividual.Open "Select * from tblSalesDataSummary WHERE [SalesCode] = " & Forms!frmMain.cboSales, CurrentProject.Connection
    strFile = strPath & rstSales!SalesName & " PST Monthly Sales.xls"
    
    'Check for existing file and delete if found
    strTestFile = Dir(strFile)
    If strPath & strTestFile = strFile Then
        Kill (strFile)
    End If
    
   'Open new Excel Workbook
    Set WeeklyPSTwb = Excel.Workbooks.Add
    WeeklyPSTwb.SaveAs strFile

    'Point to Sheet1
    Set WeeklySummary = WeeklyPSTwb.Worksheets.Item("Sheet1")
    WeeklySummary.Name = "Summary"
    
    'Loop through the Weekly PST data for the current salesperson and add to Excel file
    CurrRow = 1
    CurrCell = 1
  
        With WeeklySummary
        For Each f In rstIndividual.Fields      'add field names
            .Cells(CurrRow, CurrCell).Value = f.Name

        CurrCell = CurrCell + 1
        Next f
            CurrRow = 2
            CurrCell = 1
        Do Until rstIndividual.EOF
            For Each f In rstIndividual.Fields
                If f.Name Like "*Rev*" Then
                .Cells(CurrRow, CurrCell).NumberFormat = "$#,##0.00"
                End If
                If f.Name Like "*Cont*" Then
                .Cells(CurrRow, CurrCell).NumberFormat = "$#,##0.00"
                End If
                If f.Name Like "*CM*" Then
                .Cells(CurrRow, CurrCell).NumberFormat = "0.00%"
                End If
                .Cells(CurrRow, CurrCell).Value = f.Value   'add data
                CurrCell = CurrCell + 1
            Next f
            CurrRow = CurrRow + 1
            CurrCell = 1
            rstIndividual.MoveNext      'move to next record for individual salesperson
        Loop
        End With
        
    rstIndividual.Close                 'close recordset
    
    '
    ' Compile Detail Data
    '
    rstIndividual.Open "Select * from tblSalesDataDetail WHERE [SalesCode] = " & Forms!frmMain.cboSales, CurrentProject.Connection
    strFile = strPath & rstSales!SalesName & " PST Monthly Sales.xls"
    
    'Point to Sheet2
    Set WeeklyDetail = WeeklyPSTwb.Worksheets.Item("Sheet2")
    WeeklyDetail.Name = "Detail"
    
    'Loop through the Weekly PST data for the current salesperson and add to Excel file
    CurrRow = 1
    CurrCell = 1
  
        With WeeklyDetail
        For Each f In rstIndividual.Fields
            .Cells(CurrRow, CurrCell).Value = f.Name    'add field names
            CurrCell = CurrCell + 1
        Next f
            CurrRow = 2
            CurrCell = 1
        Do Until rstIndividual.EOF
            For Each f In rstIndividual.Fields
                            If f.Name Like "*Rev*" Then
                .Cells(CurrRow, CurrCell).NumberFormat = "$#,##0.00"
                End If
                If f.Name Like "*Cont*" Then
                .Cells(CurrRow, CurrCell).NumberFormat = "$#,##0.00"
                End If
                If f.Name Like "*CM*" Then
                .Cells(CurrRow, CurrCell).NumberFormat = "0.00%"
                End If
                '.Cells(CurrRow, CurrCell).AutoSize
                .Cells(CurrRow, CurrCell).Value = f.Value   'add data
                CurrCell = CurrCell + 1
            Next f
            CurrRow = CurrRow + 1
            CurrCell = 1
            rstIndividual.MoveNext      'move to next record for individual salesperson
        Loop
        WeeklyPSTwb.Save                'save workbook
        WeeklyPSTwb.Close               'close workbook
        End With

    rstSales.MoveNext                   'move to next salesperson
    rstIndividual.Close                 'close recordset
Loop

rstSales.Close

xlApp.Quit      'quit Excel

'Cleanup
Set rstSales = Nothing
Set rstIndividual = Nothing
Set WeeklyPSTwb = Nothing
Set WeeklyDetail = Nothing
Set xlApp = Nothing

End Sub
 
Hi Kelly,
Here is a snippet of code I use to export tables and such into Excel.
You don't have to create an instance of Excel or open one either.
As for the special formatting, I do that in Macro from within excel and just run the macro's before I send the files out.
Hope this helps, If not please tell me and I will try harder


Function Proc_OutputToExcel()
' Inventory Output To Excel

On Error GoTo Proc_OutputToExcel_Err
pstrstorenum = MsgBox("Are You Sure You Want To Export Data?", vbOKCancel, "Ready For Export")
If pstrstorenum = vbCancel Then
Exit Function
End If
DoCmd.SetWarnings False

pstrstorenum = InputBox("Enter Store Number:", "Store Number For Export")

' Cost_Variance_Over_50
DoCmd.OutputTo acTable, "Cost_Variance_Over_50", "MicrosoftExcel(*.xls)", "c:\my documents\" & pstrstorenum & "Cost_Variance_Over_50.xls", False, ""
' Final_Combined_Scans
DoCmd.OutputTo acTable, "Final_Combined_Scans", "MicrosoftExcel(*.xls)", "c:\my documents\" & pstrstorenum & "Combined_Scans.xls", False, ""
' tblUnmatched_Scans
DoCmd.OutputTo acTable, "tblUnmatched_Scans", "MicrosoftExcel(*.xls)", "c:\my documents\" & pstrstorenum & "Unmatched_Scans.xls", False, ""
' tblScans_Description_Loc
DoCmd.OutputTo acTable, "tblScans_Description_Loc", "MicrosoftExcel(*.xls)", "c:\my documents\" & pstrstorenum & "Line_By_Line_Scans.xls", False, ""
pstrMessage = MsgBox("Files have been exported to C:\My Documents\StoreNumberCombinedScans.xls" & vbCrLf & _
" C:\My Documents\StoreNumberLineByLineScans.xls" & vbCrLf & _
" C:\My Documents\StoreNumberUnmatchedScans.xls" & vbCrLf & _
" C:\My Documents\StoreNumberCostVarianceOver50.xls", vbOKOnly, "ExportComplete")


If You Want Your Dreams to Come True, Don't Oversleep.
WebbWill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top