+ Reply to Thread
Results 1 to 19 of 19

Object variable or with block variable not set

  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:

    Please Login or Register  to view this content.
    but that does not work at all and causes the computer to crash. Any suggestions: the entire code is below:

    Please Login or Register  to view this content.
    The code I have so far is below:
    Last edited by AnthonyWB; 05-23-2011 at 07:47 PM.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

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

    Please Login or Register  to view this content.

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    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.

  5. #5
    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 Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    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.

  7. #7
    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 Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    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.

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

    Please Login or Register  to view this content.
    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

    Please Login or Register  to view this content.
    I am not sure where or what the <div> and </div> are coming from?

  10. #10
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    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.

  11. #11
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    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.

    Please Login or Register  to view this content.
    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
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

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

    Please Login or Register  to view this content.

  13. #13
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    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.

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

  16. #16
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    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.

    Please Login or Register  to view this content.
    Can I recommend losing the Resume and Resume Next on the error handling?
    Last edited by davegugg; 05-24-2011 at 12:33 PM.

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

    Please Login or Register  to view this content.

  18. #18
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Object variable or with block variable not set

    Did you try my above solution???

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