+ Reply to Thread
Results 1 to 7 of 7

Inserting data from Excel to Access by VBA

  1. #1
    Registered User
    Join Date
    02-05-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    Excel 2010, Excel 2016
    Posts
    72

    Inserting data from Excel to Access by VBA

    Hello everybody,

    I've just started with Access and some coding around it. Right now, I'm trying to deal with my first issue, which I would like to approach you with.

    I'm trying to put together some copying tool, copying the data from Excel to Access. My code is currently working as long as the "DoubAmount" is a number. Unfortunatelly, there are also the cells containing text in my Excel sheet I source the data from. At the point, when macro reaches these cells (there is a loop going cell by cell through the row), it breaks down. The error message says:

    Run-time error '13':

    Type mismatch
    Would you have by chance any idea, how to redefine the variable "doubAmount" to enable copying over both number and text data? Or where else could be a problem? I've tried to replace the "Double" by "Variant", but it wasn't working either.

    I will post here only the parts of the code, that I'm working with at the moment and I consider crucial.

    First, I've got the declaration of variables:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

    Then, I've defined ranges with the data and stored all of the data into globalXLSmax:
    Please Login or Register  to view this content.
    ("globalXLSdata(i).doubAmount = vAmount(i, 1)" is referenced by debugger as a source of the breakdown")

    globalXLSdata is then later referenced when I "INSERT INTO" the data into the Access:

    Please Login or Register  to view this content.
    Thank you for any help!

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Inserting data from Excel to Access by VBA

    My code is currently working as long as the "DoubAmount" is a number.
    Not unreasonable as you have declared DoubAmount as a Double... and you cannot put the value of a "cell containing text" in a Double - you have to convert to a number first. Can't say what the actual conversion would be like as you don't give an example of the data.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Inserting data from Excel to Access by VBA

    based on your sql string, the database field for doubamount is a numeric data type.... so even if you define the variable as a variant or string, I don't imagine the sql exectution would work?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Registered User
    Join Date
    02-05-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    Excel 2010, Excel 2016
    Posts
    72

    Re: Inserting data from Excel to Access by VBA

    Thank you for your answers! You are completely right. The point is, that the colleague, who started with coding prepared the code only for copying the cells containing numbers. However, he hasn't considered, that there might be some other "text" cells such as links to websites, periodicity classification ("daily", "weekly", etc.) and some other specifications (e.g. "historical values").

    Now, I'm trying to figure out the way, how to avoid breaking the code. At least for now, I would like to try to skip all the columns including text, as the numbers are the most important for me.

    Please Login or Register  to view this content.
    Do you think there is a way, that I could at least convert all the text cells to zeros by addiding similar code as below to the one above.

    Please Login or Register  to view this content.
    This one isn't working as it says also:

    Run-time error '13:

    Type mismatch
    Last edited by lukelucky; 06-28-2017 at 08:48 AM.

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Inserting data from Excel to Access by VBA

    if a cell has text, and you dim the variable as double, you will get that error... however, if you use the "val" function it will return 0.

    For example Cell A1 has "hello" in it:

    Please Login or Register  to view this content.
    Results in Type Mismatch.

    Please Login or Register  to view this content.
    results in i being 0

    Which also means you would not need the if statement to check whether the value is >0
    Last edited by Arkadi; 06-28-2017 at 09:08 AM.

  6. #6
    Registered User
    Join Date
    02-05-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    Excel 2010, Excel 2016
    Posts
    72

    Re: Inserting data from Excel to Access by VBA

    Excellent, such a solution is exactly, what I need at the moment

    Thanks!

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Inserting data from Excel to Access by VBA

    You're welcome

    Please remember to mark the thread as solved if that fixes your issue? Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Inserting multiple records from Excel to Access?
    By JapanDave in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-18-2015, 07:20 PM
  2. trying to push data from excel to access but access not updating
    By todaychris in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2014, 05:06 AM
  3. Excel 2010 userform to access and edit the data in MS ACCESS DB tables
    By anand_y59 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 07:46 AM
  4. Inserting MS Access data into Excel as new rows below existing data
    By mptorr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-17-2012, 09:04 PM
  5. Importing data from Access to Excel, but I need to vary the table from Access
    By Liz L. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-05-2006, 09:15 PM
  6. Inserting data from excel into access
    By JCanyoneer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-21-2005, 01:10 PM
  7. Inserting excel into an access
    By sungen99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2005, 01:05 PM

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