+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: Object variable or with block variable not set

  1. #16
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Object variable or with block variable not set

    You could try something like this. This works in Access 03, not sure if any changes would be needed for 07, you'll have to consult the help to get it worked out.

    Private Sub cmdReturnMTMPortfolio_Click()
    
        Dim strFile As String
        Dim asofDATE As String
        Dim txtDate As TextBox
        Dim fd As FileDialog
        Dim varFile As Variant
                
        On Error GoTo HandleErr
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        
        With fd
            .Filters.Add "Excel Files", "*.xls*"
            .Title = "Select MTM file to import"
            If .Show = -1 Then
                For Each varFile In .SelectedItems
                    strFile = varFile
                Next varFile
            End If
        End With
        
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tempMTMReportData", strFile, True, "MTM Detail!"
        MsgBox "Your MTM file has been imported."
        
    ExitHere:
        Exit Sub
    HandleErr:
        MsgBox Err & ": " & Err.Description
        Resume ExitHere
        Resume
        
    End Sub
    Can I recommend losing the Resume and Resume Next on the error handling?
    Last edited by davegugg; 05-24-2011 at 12:33 PM.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  2. #17
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Object variable or with block variable not set

    This works, but its sloppy. I still cannot figure out a relative path.

    Private Sub cmdReturnMTMPortfolio_Click()
        Dim appXL As Excel.Application
        Dim strFilePath As String
        Dim strFileName As String
        Dim strFilePrefix As String
        Dim strFullFileName As String
        Dim strFile As String
        Dim asofDATE As String
        Dim txtDate As TextBox
        Dim wbk As Excel.Workbook
                
        On Error GoTo HandleErr
        strFilePrefix = "mtm_report_asof_"
        strFilePath = "N:\CBI-DEPT\Application Delivery\Assignments and Projects\FCIP Program\Spreadsheet Automation\Product\Excel Automation\Robin\Communication\Portfolio List\"
           
        asofDATE = Me.txtDate.Value
        'asofDATE = Format(asofDATE, "yyyymmdd")
        
        Set appXL = New Excel.Application
        
        strFullFileName = strFilePrefix & asofDATE & ".xls"
        
        Set wbk = appXL.Workbooks.Open(strFilePath & strFullFileName)
        
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tempMTMReportData", strFilePath & strFullFileName, True, "MTM Detail!"
        DoCmd.OpenQuery "qryCPNamesForMTMDailyReportParentandChild"
        DoCmd.Close acQuery, "qryCPNamesForMTMDailyReportParentandChild"
        MsgBox "Your MTM file has been imported."
              
    ExitHere:
        On Error Resume Next
        appXL.ScreenUpdating = True
        appXL.Quit
        Set appXL = Nothing
        Exit Sub
    HandleErr:
        MsgBox Err & ": " & Err.Description
        Resume ExitHere
        Resume
        
    End Sub

  3. #18
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Object variable or with block variable not set

    Did you try my above solution???
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  4. #19
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Object variable or with block variable not set

    I did. Thank you. Some errors came up - I did not dig and try to investigate. I have an nearly 1 week extension on this so not all is lost. I need to read up on the file dialog to see how that works in Access 2007. I have some other issues with the code so I will be posting shortly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0