+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Object variable or with block variable not set

  1. #1
    Valued 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.

  2. #2
    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

    On which line does the code crash?

    The snippet you added doesn't work because of the !. An exclamation point and then a period is incorrect syntax. Anyway, this snippet looks like it will add a new textbox to your form, is that what you are trying to do?
    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

  3. #3
    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

    Thanks for your response. I do not want to add a new textbox to my form.

    lPease see the updated code below. There are no bugs per se, however the asofDATE is still not being interpreted correctly. Difficult to explain the error, but a warning pops up stating that -

    ....... mtm_report_asof_<div>date entered in txtDate TextBox </div>.xls

    cannot be found. So I am still having problems.

    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\"
           
        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
        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

  4. #4
    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

    It would seem txtDate is not returning the date. How is this textbox populated? Can you ensure that a date is being shown when the code runs?

    It doesn't look correct that you declare txtDate as a variable. If it is the name of a control on your form, you don't have to declare a variable for it.
    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

  5. #5
    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

    All I would like to do is pass the date that was entered into the TextBox to a variable so that it can be used as part of a file name. How would you do that? Any suggestions?

    Name of TextBox = txtDate
    Name of variable that captures the value entered into txtDate = asofDATE

  6. #6
    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

    Your code looks correct for what you are trying to do, but there's no way to verify since I can't see your db. Put a breakpoint in on that line, and use the immediate window to find out the value of your textbox.

    As a note, unless Access has changed from 03 to 07, you don't need to open an Excel workbook to import it.
    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

  7. #7
    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

    Thanks for the advice. I broke the code right where the asofDATE is assigned. You were right. asofDATE is empty. Furthermore so is strFullFileName. Nothing is passed onto to those variables. Any further suggestion?

  8. #8
    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 should step through the code using F8 to see what gets assigned to each of those variables, and make sure it is what was intended. You can use the immediate window to find out the values of properties on your form.
    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

  9. #9
    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

    Perhaps I am mising qoutes (single or double) around asofDATE in the expression:

    rFullFileName = strFilePrefix & asofDATE & ".xls"
    Not sure? I stepped through the code via F8 and asofDATE = _<div>date entered in txtDate TextBox </div>. So for example if I enter a date of 20110107 then

    asofDATE  = "<div>20110107</div>"
    I am not sure where or what the <div> and </div> are coming from?

  10. #10
    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

    The div are HTML tags. I don't know why Access is adding them, are you using Access 07?

    It happens because you have used rich text format.
    Select the text button and go to properties > Data > Text Format > Select Plain Text from there
    In the database goto table in design mode > select the field > properties (General Tab) Text Format > select "Plain Text", if applicable.
    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

  11. #11
    Valued Forum Contributor alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Lake County, Illinois
    MS-Off Ver
    MS Office 2010, 2007 and 2002
    Posts
    1,155

    Re: Object variable or with block variable not set

    Here is some code that I use regularly to export queries to Excel. While I realize that you already have your code, I also use an unbound text box as part of the code. I thought that seeing this may tip you to a solution.

    Private Sub Command5_Click()
    On Error GoTo Err_Command5_Click
    Dim reportName As String
    Dim theFilePath As String
    
    Select Case Me.Frame6.Value
        Case 1
        reportName = "MonthlyActivity"
        End Select
           
    theFilePath = Me.txtfilepath.Value
    theFilePath = theFilePath & reportName & "_" & Format(Date, "yyyy-mm-dd") & ".xls"
          
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, reportName, theFilePath, True
    
    MsgBox "Look on your desktop for the report."
    
       
    
    Exit_Command5_Click:
        Exit Sub
    
    Err_Command5_Click:
        MsgBox Err.Description
        Resume Exit_Command5_Click
        
    End Sub
    As you can see I use the unbound text box txtfilepath to let the user select where he wishes the file to go. I hope this helps you with your issue.

    Alan

  12. #12
    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

    The code below works. So I will close the thread. However I need to use relative paths. Also there is an error importing the data that leads to lost records. But technically it transfers the spreadsheet.

    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!"
        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

  13. #13
    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 use a file dialog object to have the user select the path each time it is run.
    Again, you don't have to open the workbook before importing it.
    If you are importing to an existing table, the only records lost will be records that don't fit the data type that is stored in each field of the table. This should not affect any future calculations, just be aware that invalid data will show up as null.
    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

  14. #14
    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 am actually losing quite a bit of data. So I will have to look at my fields again and see whats going on. I am not familiar with file dialog.

  15. #15
    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

    Also how can my code be modified to not open the file. Opening the file is all I can think of.

+ 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