Results 1 to 19 of 19

Object variable or with block variable not set

Threaded View

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

    Object variable or with block variable not set

    Hello everyone. This is closely related to my prior post - which was corrected. I no have encountered a new bug.

    I would like to transfer an Excel spreadsheet into an Access table using the TransferSheet Method. The caveat is that the name of the file needs to involve a variable (a date) based on an entry from a TextBox. The name of the table is tblMTMReportData. It already has column headings. The name of the Excel spreadsheets is as follows: mtm_report_asof_yyyymmdd.xls. Now the date extension "yyyymmdd" varies.

    I have created a userform and on that userform is a TextBox and a Command Button. The textBox and command button are on a page called pagCMDReturnMTMPortfolio. The textbox is called txtDate. The command button is called CommandReturnMTMPortfolio.

    What I would like to happen is for the user to enter a date say 20110419. After that the user presses the button and the macro(or code) retrieves the file based on the date extension. The path to the files and folders are contained in another table.

    I am getting an error : object variable or with block variable not set. I am assuming the object in question is txtDate the TextBox where the user enters a date.

    I tried correcting this error by adding the following snipet:

    Set txtDate = Forms!.Controls.Add("vb.TextBox","txtDate")
    but that does not work at all and causes the computer to crash. Any suggestions: the entire code is below:

    Sub GetMTMPortfolioData()
        Dim appXL As Excel.Application
        Dim strFilePath As String
        Dim strFileName As String
        Dim strFile As String
        Dim asofDATE As String
        Dim strFilePrefix As String
        Dim rstMTM As DAO.Recordset
        Dim strSQL As String
        Dim wbk As Excel.Workbook
        Dim strFullFileName As String
        Dim txtDate As TextBox
             
        On Error GoTo HandleErr
        strFilePrefix = "mtm_report_asof_"
        strSQL = "Select * From tblFilePath Where Name='MTMSummary'"
        Set rstMTM = CurrentDb.OpenRecordset(strSQL)
        strFilePath = rstMTM.Fields("Path")
        
        strFile = Dir(strFilePath)
        
        asofDATE = txtDate.Text
        'asofDATE = Format(asofDATE, "yyyymmdd")
        
        Set appXL = New Excel.Application
        
        strFullFileName = strFilePrefix & asofDATE & ".xls"
        
        Set wbk = appXL.Workbooks.Open(strFilePath & strFullFileName)
        
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tblMTMSummary", strFilePath & strFullFileName, True
        MsgBox "Your MTM file has been imported."
        
    ExitHere:
        On Error Resume Next
        appXL.ScreenUpdating = True
        rstMTM.Close
        Set rstMTM = Nothing
        appXL.Quit
        Set appXL = Nothing
        Exit Sub
    HandleErr:
        MsgBox Err & ": " & Err.Description
        Resume ExitHere
        Resume
        
    End Sub
    The code I have so far is below:
    Last edited by AnthonyWB; 05-23-2011 at 07:47 PM.

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.6.0 RC 1