Access freezing after Excel import

nuttychick

Registered User.
Local time
Today, 08:15
Joined
Jan 16, 2004
Messages
84
:confused: Hi all - hope someone can help I dont know where to start.

I've got a cmd button that imports data from a excel sheet - that actual code is working fine, however when it runs it causes Access to freeze.

The data in imported correctly - so it must complete the process but Access locks up and has to be ended via task manager.

Any one got any clues why this is happening and how I can stop it?

This is an Access 97 database running mainly on NT and 2000

Code:
Private Sub LoadActualsDataButton_Click() On Error GoTo Err_LoadActualsDataButton_Click

' This procedure performs a two file match between the Actuals table (the Master file) and ' The Actuals spreadsheet file (the Transaction file).
'
' Keys : Study Code|Work Package|Period
'
' If the Master key < Transaction key then
'       Read the next Master record.
' If the Transaction key > Master key then
'       Add the transaction record to the Master file
'       Read the next Transaction record.
' If the Master key = Transaction key then
'       Update the value on the Master record with the value on the Transaction record
'       Read the next Master Record
'       Read the next Transaction record.
'
' End of File processing
'       At End of File on the Master file, set the Master key to "ZZZZZZ"
'       At End of File on the Transaction file, set the Transaction key to "ZZZZZZ"
'       Continue processing until both keys are equal to "ZZZZZZ"


Dim MyDB As Database, MySQL As String, MySet As Recordset Dim appExcel As Excel.application Dim MyFiles As String Dim MasterKey As String, TransactionKey As String

Set MyDB = CurrentDb()
Set appExcel = CreateObject("Excel.Application")

' Set up the transaction file (Actual Data Spreadsheet)

MyFiles = appExcel.GetOpenFilename("Excel Files(*.xls),*.xls", , "Open Actuals Spreadsheet") If MyFiles = "False" Then Exit Sub

appExcel.Workbooks.Open FileName:=MyFiles, ReadOnly:=True appExcel.Visible = False

' Check that this is a genuine Actual spreadsheet On Error Resume Next Let Err.Number = 0 appExcel.Sheets("Sheet1").Range("B1").Select
If Err.Number = 9 Then
   MsgBox "This is not a valid Actuals Spreadsheet."
    appExcel.Quit
   Exit Sub
End If

If appExcel.ActiveCell <> " Extracted Actuals Data" Then
    MsgBox "This is not a valid Actuals Spreadsheet."
    appExcel.Quit
    Exit Sub
Else
    appExcel.ActiveCell.OffSet(1, 0).Range("A1").Select
    TransactionKey = appExcel.ActiveCell.OffSet & appExcel.ActiveCell.OffSet(0, 1) & appExcel.ActiveCell.OffSet(0, 2) End If appExcel.Visible = True

' Set up the Master File (Actual Table)

MySQL = "SELECT Actuals.[Study Code], Actuals.[TBCS Code], Actuals.[Year/Month], Actuals.Actual "
MySQL = MySQL + "From Actuals "
MySQL = MySQL + "ORDER BY Actuals.[Study Code], Actuals.[TBCS Code], Actuals.[Year/Month]; "
Set MySet = MyDB.OpenRecordset(MySQL)
If MySet.EOF Then
    MasterKey = "ZZZZZZ"
Else
    MasterKey = MySet![Study Code] & MySet![TBCS Code] & MySet![Year/Month] End If

Do Until TransactionKey = "ZZZZZZ"
    If MasterKey < TransactionKey Then
        ' Read the next master record
        MySet.MoveNext
        MasterKey = MySet![Study Code] & MySet![TBCS Code] & MySet![Year/Month]
        GoTo Next_Loop
    End If
    If MasterKey > TransactionKey Then
        ' Add a new record from the Transaction to the Master
        MySet.AddNew
        MySet![Study Code] = appExcel.ActiveCell
        MySet![TBCS Code] = appExcel.ActiveCell.OffSet(0, 1)
        MySet![Year/Month] = appExcel.ActiveCell.OffSet(0, 2)
        MySet!Actual = appExcel.ActiveCell.OffSet(0, 4)
        MySet.Update
'        MySet.Requery
        appExcel.ActiveCell.OffSet(1, 0).Range("A1").Select
        TransactionKey = appExcel.ActiveCell.OffSet & appExcel.ActiveCell.OffSet(0, 1) & appExcel.ActiveCell.OffSet(0, 2)
        GoTo Next_Loop
    End If
    ' Keys are equal so update the Master with the Transaction value
    MySet.Edit
    MySet!Actual = appExcel.ActiveCell.OffSet(0, 4)
    MySet.Update
'    GoTo Next_Loop
    appExcel.ActiveCell.OffSet(1, 0).Range("A1").Select
    TransactionKey = appExcel.ActiveCell.OffSet & appExcel.ActiveCell.OffSet(0, 1) & appExcel.ActiveCell.OffSet(0, 2)
    MySet.MoveNext
    MasterKey = MySet![Study Code] & MySet![TBCS Code] & MySet![Year/Month]
Next_Loop:
Loop

Exit_LoadActualsDataButton_Click:
    Exit Sub

Err_LoadActualsDataButton_Click:
    MsgBox "An has occured." & vbCrLf & vbCrLf & _
    "Error number: " & Err.Number & vbCrLf & vbCrLf & _
    "Description: " & Err.Description
    Resume Exit_LoadActualsDataButton_Click
    
' = Mid(ActiveCell, 1, (Len(ActiveCell) - 1)))

End Sub



Private Sub MainMenuButton_Click()
On Error GoTo Err_MainMenuButton_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Main_Menu"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_MainMenuButton_Click:
    Exit Sub

Err_MainMenuButton_Click:
    MsgBox Err.Description
    Resume Exit_MainMenuButton_Click
    
End Sub

Private Sub MaintainContactTableButton_Click()
On Error GoTo Err_MaintainContactTableButton_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frmBDTSContactsMaintenance"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_MaintainContactTableButton_Click:
    Exit Sub

Err_MaintainContactTableButton_Click:
    MsgBox Err.Description
    Resume Exit_MaintainContactTableButton_Click
    
End Sub

Private Sub MaintainersNBTUsersButton_Click() On Error GoTo Err_MaintainersNBTUsersButton_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frmBDTSMaintainNBTUsers"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_MaintainersNBTUsersButton_Click:
    Exit Sub

Err_MaintainersNBTUsersButton_Click:
    MsgBox Err.Description
    Resume Exit_MaintainersNBTUsersButton_Click
    
End Sub
 
Just looking quickly at the code : it looks as though if it opens an valid Actuals Spreadsheet OK, appExcel never recieves a ".Quit" method.
 
Excel macros?

Thanks Richary

Been doing some checks and it works fine with a old version of the excel import.

The imported file has changed due to it coming from a different source. - if I import one of the old files then it works fine.

The old version is formatted using a macro - the macro has been amended to work with the new source of information

Could it be the macros on the other excel sheets? These have the following code to covert the data to import to access

Format the data so it will import to access

Code:
Sub DTXFormat()
'
' DTXFormat Macro
' Macro recorded 10/08/05 by Nicki Smith

Dim strMonthName As String, strMonthNumber As String

'Delete the first three heading lines NS Works with DTX 24/08/05
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp

' Add in the Actuals identifying line NS Works with DTX 24/08/05
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown

'NS Delete columns c,d,e NS Works with DTX 24/08/05
    Columns("c:e").Select
    Selection.Delete Shift:=xlToLeft
'NS Now delete columns d,e,f,g NS Works with DTX 24/08/05
    Columns("d:g").Select
    Selection.Delete Shift:=xlToLeft
    
'NS Insert new column to hold the extracted Study Code NS Works with DTX 24/08/05
    Columns("B").Select
    Selection.Insert Shift:=xlToRight

'NS Insert new column to hold the first date NS Works with DTX 24/08/05
    Columns("d").Select
    Selection.Insert Shift:=xlToRight
    
'NS Insert new column to hold the second date NS Works with DTX 24/08/05
    Columns("d").Select
    Selection.Insert Shift:=xlToRight
    
' Insert two new columns to hold the extracted Study Code and the Work Type
'    Columns("B:D").Select
 '   Selection.Insert Shift:=xlToRight

'NS Format new column B NS Works with DTX 24/08/05
    Columns("B").Select
    Selection.NumberFormat = "@"



'NS Format new column D NS Works with DTX 24/08/05
    Columns("D").Select
    Selection.NumberFormat = "@"
    
'NS Add title NS Works with DTX 24/08/05
    Range("B1").Select
    ActiveCell.FormulaR1C1 = " Extracted Actuals Data"
    Range("A2").Select
    
    Do While ActiveCell <> ""

' Change any "_" to "-" NS Works with DTX 24/08/05
        If InStr(1, ActiveCell, "_") <> 0 Then
            ActiveCell = Left$(ActiveCell, (InStr(1, ActiveCell, "_") - 1)) & "-" & Mid(ActiveCell, (InStr(ActiveCell, "_") + 1), 4)
        End If
        
'NS extract the study code by removing characters "P04F*" from the front of the Study code. NS Works with DTX 24/08/05
        If Left$(ActiveCell, 4) = "P04F" Then
            ActiveCell.Offset(0, 1) = Mid(ActiveCell, 5, (Len(ActiveCell) - 4))
        Else
            ActiveCell.Offset(0, 1) = Mid(ActiveCell, 1, Len(ActiveCell) - 3)
        End If

' NOT REQUIRED NS 24/08/05 Remove the characters "SBE*" from the front of the Study code and
' remove the work package code from the end
       ' If Left$(ActiveCell, 3) = "SBE" Then
           ' ActiveCell.Offset(0, 1) = Mid(ActiveCell, 5, (Len(ActiveCell) - 7))
        'Else
        '    ActiveCell.Offset(0, 1) = Mid(ActiveCell, 1, Len(ActiveCell) - 3)
        'End If

' Remove the 'dross' studies i.e. those that are not genuine! NS Works with DTX 24/08/05
        If Left$(ActiveCell.Offset(0, 1), 2) < "16" Or Left$(ActiveCell.Offset(0, 1), 2) > "99" Then
            ActiveCell.EntireRow.Delete
            GoTo Exit_Loop
        End If
        
' Extract the work package and store it in the spreadsheet- NS Works with DTX 24/08/05
        ActiveCell.Offset(0, 2).Activate
        ActiveCell = Mid(ActiveCell, 10, (Len(ActiveCell) - 3))

'Change the work package to correct one to work with JDB. 08-02, 12-01, 06-01, 04-02. NS Works with DTX 24/08/05
    If ActiveCell = "8" Then
    ActiveCell = "'02"
    End If
    If ActiveCell = "12" Then
    ActiveCell = "'01"
    End If
    If ActiveCell = "06" Then
    ActiveCell = "'01"
    End If
    If ActiveCell = "04" Then
    ActiveCell = "'02"
    End If
    ActiveCell.NumberFormat = "00"

'Add the current month in YYYY MMM format.NS Works with DTX 24/08/05
    ActiveCell.Offset(0, 1).Activate
    ActiveCell.NumberFormat = Text
    ActiveCell = Range("D1")
    
    

'Add the current month in YYYY MM format
    ActiveCell.Offset(0, 1).Activate
    ActiveCell.NumberFormat = Text
    ActiveCell = Range("E1")

'Divide the Expenditure Quantity from DTX by 7.5
    ActiveCell.Offset(0, 1).Activate
    ActiveCell.NumberFormat = "0.00"
     ActiveCell = ActiveCell / 7.5
     ActiveCell.NumberFormat = "0.00"


' Reformat the date
   '     ActiveCell.Offset(0, 5).Range("A1").Select
        'strMonthNumber = Right$(ActiveCell.Offset(0, 4), 2)
        'Select Case strMonthNumber
           ' Case "01"
           '     strMonthName = "Jan"
           ' Case "02"
           '     strMonthName = "Feb"
            'Case "03"
             '   strMonthName = "Mar"
            'Case "04"
            '    strMonthName = "Apr"
            'Case "05"
            '    strMonthName = "May"
            'Case "06"
            '    strMonthName = "Jun"
            'Case "07"
            '    strMonthName = "Jul"
            'Case "08"
            '    strMonthName = "Aug"
            'Case "09"
            '    strMonthName = "Sep"
            'Case "10"
            '    strMonthName = "Oct"
            'Case "11"
            '    strMonthName = "Nov"
            'Case "12"
            '    strMonthName = "Dec"
            'Case Else
            '    strName = "***"
        'End Select
        ActiveCell.Offset(0, 3) = Left$(ActiveCell.Offset(0, 4), 4) & " " & strMonthName
       
' Move on to the next row
        ActiveCell.Offset(1, -5).Range("A1").Select
        
Exit_Loop:
    Loop

    ActiveCell.Offset(0, 1) = "ZZZZZZ"
    
' Sort the spreadsheet
    Cells.Select
    Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Key2:=Range("C1") _
        , Order2:=xlAscending, Key3:=Range("D1"), Order3:=xlAscending, Header:= _
        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End Sub
Adds the month to two columns

Code:
Sub AddMonths()
'
' AddMonths Macro
' Macro recorded 31/08/05 by IRAspire
'

Dim strMonthName As String, strMonthNumber As String
Do While ActiveCell <> ""

'Add the current month in YYYY MMM format.NS Works with DTX 24/08/05
    ActiveCell.Offset(0, 1).Activate
    ActiveCell.NumberFormat = Text
    ActiveCell = Range("D1")
 
   
'Add the current month in YYYY MM format
    ActiveCell.Offset(0, 1).Activate
    ActiveCell.NumberFormat = Text
    ActiveCell = Range("E1")

       
' Move on to the next row
        ActiveCell.Offset(1, -2).Activate
        
        
Exit_Loop:
    Loop

    ActiveCell.Offset(0, 1) = "ZZZZZZ"
    
' Sort the spreadsheet
    Cells.Select
    Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Key2:=Range("C1") _
        , Order2:=xlAscending, Key3:=Range("D1"), Order3:=xlAscending, Header:= _
        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom


'
End Sub
 
Last edited:
Just read yor original post and saw that it was Access that was freezing - not Excel. My apologies to you.

I would imagine that the problem is in Excel, but I don't think that it is realted to the macros. Without seeing examples of both types of spreadsheet, it will be difficult for me to work out where the problem is.

Have you tried stepping through the LoadActualsDataButton_Click() sub to see which line of code Access hangs on? That should narrow down the problem. It would also be worth ALT-TABbing to the Excel process to make sure that it is behaving the way you expect.
 
Sorted

:rolleyes: silly billy me!

Fresh thoughts on a monday certainly do help!

I'd copied over code from the one macro to the other - to replicate the loop. However I'd forgot to take out the additional - put zzzzz on the end.

The additional zzzzz was confusing the application - took it out and it works fine!

Thank you for your help!

Nicki
 

Users who are viewing this thread

Back
Top Bottom