Clear table contents before importing new data

  • Hello,


    I 've found some fine piece of code to import Exceldata into Access, but I still have one problem: Excel places the new data under the already present data. However, I do not want to append the Exceldata, but to overwrite (part of) the existing data.


    How can I make Excel clear the contents of one or more Access columns (before subsequently importing new data into these cells)?


    Thanks in advance,


    Mede

  • Re: Clear table contents before importing new data


    Mede,


    If you are importing the same number of records then use an Update query and not an append query.


    If you are importing a different number (more or less) of records then you will need to delete the records using a delete query and then append the records.


    Note that if you are doing this often you should compact the database between the delete and append queries. Alternatively, if acceptable to your dataset, you can delete the records when the database is closed and compact the database on close. Then when you open the database force it to append the new records. I will give more info on compaction if you need it. It is often necessary and better practice) to split your database into a front and backend.


    HTH,


    A.

  • Re: Clear table contents before importing new data


    Thanks for your reaction, but I'm not sure how to perform this operation. In my current code (see below), I use an ADO connection to import the Exceldata into a Access table. The number of rows in Excel and Access is identical.


    How can I initiate this update-query within this ADO-transfer? I have indicated in the code where about I think this clearance should happen. And is compaction also relevant for update-queries?


    Thanks in advance!


  • Re: Clear table contents before importing new data


    Is there a reason that you can not link the table in the Database Window and run queries? This would be far easier.


    You state that there are always the same number of records. If this is the case why are you adding and not simply updating the data? This gets more complicated doing it programatically, if you can simply link the excel worksheet and run queries it will be much easier.


    A.

  • Re: Clear table contents before importing new data


    Well, I do not know much about this table linking and found many suggestions about using ADO to import/export data between Access and Excel.


    In short, I want to perform the following functionality:
    The user clicks on a button in Acces
    Data is being exported to Excel
    Excel performs some calculations on a few columns
    Excel exports these updated columns back to Access. overwriting the existing ones.


    At this point, I can perform all functions, except for the overwriting. The adjusted data is simply entered below the existing ones. So if I only could erase the existing records just before importing the newly calculated ones, I'd be done. I'd rather not switch to a completely different method like this table linking now I've come so far, unless there are some good grounds to do so.


    But if I'm not mistaken, ADO connections are faster/more efficient/more flexible than table linking, which would advocate sticking to ADO.


    If I'd be able to execute the clear contents step as well... Could you help me with this very part, or give more details about the updating procedure?


    Many thanks,


    Mede

  • Re: Clear table contents before importing new data


    Mede


    Why are you exporting the data to Excel, doing calculations and then reimporting to Access?


    Couldn't everything be done in Access?


    By the way ADO may be faster than linking tables, but I don't think ADO and looping, which is what you are doing will be.

    Boo!:yikes:

  • Re: Clear table contents before importing new data


    I guess not...I have to run a certain algorithm on the data, which involves many iterations. So far as I know, this is not possible in Access.


    But I'm open for any suggestions...

  • Re: Clear table contents before importing new data


    Mede


    It's hard to say if something isn't possible without knowing what that something is.


    The only reason I can see for exporting to Excel and then importing back to Access would be if you were using specific Excel functionality.

    Boo!:yikes:

  • Re: Clear table contents before importing new data


    Mede,


    1. Speed
    Unless you are using 1000s of records the linked table will be fast enough that you will not notice the difference. Aldo, as Norrie points out, the data exchange may be quicker with ADO, but the processing will probably be far quicker using a query.


    2. Functionality of Excel and Access.
    If you are running a calculation in Excel and not using a specific tool such as the solver or another specialised add-in then it is likely that the calculations can be done in Access. As Norie points out though, without knowing what calculation you are doing in Excel we can not advise.


    Can you post the excel sheet with a bit of explanation?


    A.

  • Re: Clear table contents before importing new data


    Hello,


    The excelsheet is far too large to post on the site, but I am running the macro below in Excel after having imported the "raw" data from Access:



    This generates a column with updated figures, which I'd like to re-import into Access, whilst getting rid of the previous column. But I'm struggling with this very last step...


    Thanks in advance for your ideas,


    Mede

  • Re: Clear table contents before importing new data


    Hello,


    I've managed to comprise my file to an acceptable format, although this required deleting lots of formulae.


    In short, the sheet does the following: launching the "Run"macro starts calculating the desired stock levels for the spare parts listed, based on price and availability measures. The algorithm calculates the added availability if one extra spare would be added on stock and then selects the part where the price per availability relation is the best. (It's a so-called greedy algorithm).


    Subsequently, the total stock value is compared to some norm and continues until the norm is reached.


    For the next run session, first the "Reset" macro should be launched.


    The formulae how the availability is calculated had to be replaced by their respective values to save kb space, but it involves "e"-powers, which I suspect not to be possible in Access.


    Also, the macro where the raw data is imported from Access is left out completely. However, I am interested in exporting it again into Access, replacing the old data.

  • Re: Clear table contents before importing new data


    Something like e^x, where is is the natural logarithm.


    I wonder whether this functionality is available in Access...

  • Re: Clear table contents before importing new data


    Hello,


    Finally, I've found a solution for my problem: I call the Excel-codes/macros from Access and before re-importing the updated data into Excel, I erase the contents of my "old" table by means of a delete query.


    But of course, a new issue has emerged;


    I can surpress the message in Access about"Delete confirmation, but when closing Excel automatically, I'm asked whether or not to save this Excelfile.


    How can I automatically safe and close my Excel workbook (from Access), without any disturbing message?


    Thanks in advance,


    Mede

  • Re: Clear table contents before importing new data


    Quote

    How can I automatically safe and close my Excel workbook (from Access), without any disturbing message?


    Something like:

    Code
    Application.DisplayAlerts = False


    Where Applicationis your reference to Excel.Application object.


    ~j.v.a.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!