+ Reply to Thread
Results 1 to 23 of 23

Using VBA to pull data from mySQL to excel with Macro button

  1. #1
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Post Using VBA to pull data from mySQL to excel with Macro button

    Hey everyone,

    So I have a materials database made up in mySQL, which holds linked tables for materials, manufacturers and vendors.

    I also have a Bill of Materials (BOM) in excel, in which there are empty columns for manufacturers, vendors, etc.

    The goal is to have a Macro button that, once pressed, it will populate the BOM's empty columns with the appropriate data from the database.

    I have very little experience with VBA and macros, so the information I have gathered is all from forums and the web.

    In VBA, I have created a module for the connection to the database.

    Please Login or Register  to view this content.
    After getting a few debug errors, I am now able to run it without any errors. Although when I click run, nothing really seems to happen, but I am assuming a connection is being made behind the scenes?

    *Not sure if it's useful to mention at this point, I have already set up the ODBC driver and successfully tested the connection to the db, as well as added ActiveX Data Object 2.8 to Reference in VBA

    Back in excel, I added a macro button to the BOM, and inserted the following code:

    Please Login or Register  to view this content.
    Although the code for the button does not do what I intend, it's mainly just to confirm that connections and whatnot are correct. Once I have confirmed that, I will work on the button code to do what I actually want it to..

    I seem to be getting an error at the .ActiveConnection line and not sure why.

    Was wondering if someone could let me know if I have the right idea/heading in the right direction, and possibly, what might be wrong with my code?

    Thanks!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I am using my own connection, so you need to change it to suit you

    Please Login or Register  to view this content.
    This is better

    Please Login or Register  to view this content.
    Last edited by AB33; 02-12-2015 at 10:35 AM.

  3. #3
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    Your own connection would be at the Accesscontr$ line?

    For provider, would I then use Microsoft.ACE.OLEDB.5.3; ? I just copied your formatting and changed it to the version I have... I think I'm lost

    I have found in C:\Users\Me\Documents\My Data Sources the materialsdb.odc but no .accdb file?

  4. #4
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    Hi,

    Is cnMaterialsdb a public variable defined in your module ?
    When are you opening the connection to your mysql db ?

    As i cannot see that in the button click event.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using VBA to pull data from mySQL to excel with Macro button

    No, you need to connect to MYSQL. I am trying to find the link website and will post it shortly.
    This site has all the connection strings. All you need is copy the string and then adjust the file name
    http://www.connectionstrings.com/mysql/

  6. #6
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I posted code for two different modules. The first one I assumed opens the connection. The second one carries out the data import when the button is clicked. Is that not how it would work? I'm very inexperienced in vba.

  7. #7
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    Based on your code it should be something like this :


    Please Login or Register  to view this content.
    Button click event code :

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I have it as:
    Please Login or Register  to view this content.
    As I step through it, it stops at .Open "Run-time error '3709': Application-define or object definer error"

  9. #9
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    The line "public sub cn As ADODB.Connection" goes red with an underline across in the module.

    And it stops at ".ActiveConnection = cn in the button click
    Also, in button click, should I be replacing the line ADOExcelSQLServer with

    "Const Accesscontr$ = "Driver={MySQL ODBC 5.3 UNICODE Driver};Server=localhost;Database=materialsdb;User=rooty;Password=nevergunnagiveyouup;Option=3;"

    as that is my connection type?

    ^^ Ignore the above, I don't know how 'sub' got into the "public cn as.." line. but when I removed it, I have been able to step through the button click module, which automatically steps through the ADOExcelSQLServer function, and then jumps back into the button click function, but seems to stop at rsMaterialsdb.Close with errors.
    Last edited by khelza; 02-12-2015 at 11:31 AM.

  10. #10
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    It should be

    Please Login or Register  to view this content.
    i had edited it after posting.

  11. #11
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    Noted! Thanks

  12. #12
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    why is this line commented ?

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I might have posted this already, not sure why but my comments are not appearing..

    Anyways, the line
    Please Login or Register  to view this content.
    was commented out in AB33's code example. Using yours, it's not commented out, and it's called cn (not cnMaterialsdb)

  14. #14
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I think , let us start working on the piece of code that you have posted in post #8

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I would much rather use the two samples of code you posted because it seems to be somewhat functional.

    I have been able to step through the button click module, which automatically jumps into and steps through the ADOExcelSQLServer function, and then jumps back into the button click function, steps through but seems to stop at rsMaterialsdb.Close with errors.

    Unless you think I should scrap it and go back to AB33's code you just posted?
    If I go use that code instead, I step through and get stopped after rsMaterialsdb.Open

  16. #16
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I have made to tool to query xlsx file that is based on adodb. (File Attached)

    Please look into this code and then we can tweak this to suit your requirements.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    Should I have C:\Analysis Version1.xlsx ?

    What is [Select File] button for?

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using VBA to pull data from mySQL to excel with Macro button

    I am posting the entire code which I used to extract data in access. It works on my PC.
    I think you have a problem with connecting to MYSQL

    Please Login or Register  to view this content.
    Last edited by AB33; 02-12-2015 at 01:06 PM.

  19. #19
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    @Khelaz,

    those options are for querying an excel file.

    If you look into the module code you can see the connect options for excel and csv type of files.

    I want you to copy the code under module 1 .

    Please Login or Register  to view this content.
    and change the same into

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    @AB33 I have been having some success with the previous code. I have been able to run it, and when I look at column O, I see that it has imported the Manufacturers table from O6:P64 (which is the whole table size)

    The code I am currently using for the connection:
    Please Login or Register  to view this content.
    And for the button:

    Please Login or Register  to view this content.
    I had to comment out the rsMaterialsdb.Close line because, when uncommented, I would get an error pointing to that line, but the data would still import to O6. Does that just mean that the connection will stay open now that it is commented out?

  21. #21
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    your code :
    Please Login or Register  to view this content.
    your with statement has the .close so its already close. So do not worry all is fine.

  22. #22
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Using VBA to pull data from mySQL to excel with Macro button

    After this

    Please Login or Register  to view this content.
    closes your mysql connect. I guess your data extraction part is good to go now.

  23. #23
    Registered User
    Join Date
    02-10-2015
    Location
    Canada eh
    MS-Off Ver
    2010
    Posts
    22

    Re: Using VBA to pull data from mySQL to excel with Macro button

    SOLVED! Thanks, you guys have been so helpful! Much appreciated

+ 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. Macro to read data from a MySQL-database
    By Pero in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-02-2015, 02:00 PM
  2. How can i import data from excel csv into mysql
    By baig123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-01-2014, 05:53 AM
  3. Macro button to pull and dump data > help
    By step_one in forum Excel General
    Replies: 8
    Last Post: 08-10-2011, 07:20 PM
  4. validating excel data against a mysql db?
    By spyrule in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-18-2007, 03:51 PM
  5. Import MySQL data to Excel
    By mbeyer in forum Excel General
    Replies: 0
    Last Post: 11-30-2005, 03:10 PM

Tags for this Thread

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