+ Reply to Thread
Results 1 to 12 of 12

Filling data from MySQL db into excel sheet, where ID matches

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

    Post Filling data from MySQL db into excel sheet, where ID matches

    Hey everyone,

    I haven't been able to find any similar examples online to what I'm trying to do.. hoping someone can help out here

    I have a database in MySQL called 'materialsdb' which holds tables for Materials, Manufacturers and Vendors. Materials is the main table of use, linked with foreign keys to Manuf_ID and Vendor_ID.

    EERD.jpg

    I also have an excel workbook for Bill of Materials (BOM). The BOM has columns for part_ID, name, manufacturer, vendor, cost, etc.
    part_ID (actually called CW_ID) and name are the only columns which are populated, the rest are left blank to be filled in from the database.
    The main goal is to be able to check off items in the BOM that will be used for a project, and then click a Macro button, which will populate the empty fields with data from the database tables.
    If item is not checked off, it's columns will not be populated, and possibly filtered so it no longer shows up.

    So far, I have been able to create a button which can import any one of the database tables into specified location in the BOM. However, it just imports the entire table in one location.
    Because both the BOM and the materials table in the db have a column for part_ID, I'm hoping to have it where it will see a match for part_ID and fill in the columns like Manufacturer from the manufacturers table, vendor from vendor table, price from materials table, etc.

    BOM.jpg

    Because the manufacturer and vendor tables are linked to the material tables, I'm not sure if you can use views or joins or something like that, but any help would be appreciated.

    This is what I have so far for the button click:

    Please Login or Register  to view this content.
    Last edited by khelza; 02-12-2015 at 03:59 PM.

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    @khelza ,

    a) You need to loop through the column (CW_ID) of your BOM Sheet.
    b) Pass the sql statement to extract the details for a specific CW_ID
    c) Populate the date in the respective cells

    This loop continues until you hit the last row.

    The issue here is that you might have more than one vendor so you might have more than 1 single row of data for each CW_ID.

    Your sql will be something like this :

    Please Login or Register  to view this content.
    Not sure about your primary_Vendor info.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    Will look into this later as i am signing off now.

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    I will also have to look into this tomorrow! Thanks

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    @xlbiznes, When you say
    Your sql will be something like this :
    Are you talking about an SQL query within mySQL? Or is this written in VBA?
    Last edited by khelza; 02-13-2015 at 11:56 AM.

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    @khelza,

    the query is written in vba code.

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    Here is what I have for the loop:

    Please Login or Register  to view this content.
    Not sure how to go about b) or c)

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    I guess it would be something like this :

    Please Login or Register  to view this content.

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    I really appreciate that! I have added the new code to my existing button click.

    Please Login or Register  to view this content.

    I changed CONN to cn because my other module declares:
    Please Login or Register  to view this content.
    When I run this code, I get a runtime error at line:
    Please Login or Register  to view this content.
    Last edited by khelza; 02-13-2015 at 05:00 PM.

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    Please check the my_sql part if something is wrong with that.

    In order to test the other part of the code, you can try to hard code my_sql string to fetch a couple of fields from your main table and see what happens.

    if it fetches the required data then we need to check the sql statement , which is :

    Please Login or Register  to view this content.

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    It was suggested that I debug.print my_sql to my local window, so I did.

    I get no value for my_sql string as I step through it until
    Please Login or Register  to view this content.
    Then it shows
    ""Select manufactures.manufacturer, materials.model_number, vendors.vendor, materials.cost_usd from manufactures, materials, vendors""
    as the string value in the local window.

    Then stops at line
    Please Login or Register  to view this content.
    with a run-time error. I'm assuming something because of the my_sql string

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

    Re: Filling data from MySQL db into excel sheet, where ID matches

    You sql string :

    Please Login or Register  to view this content.
    seems incomplete, as the where statement is missing as you need to link the tables.

+ 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. using excel to upload data to MySQL
    By jrtaylor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2015, 03:48 AM
  2. please let me know can i Update only selected columns
    By baig123 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-01-2014, 02:27 AM
  3. 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
  4. How to save an Excel sheet with commas to MySQL Database
    By cinci-hal in forum Excel General
    Replies: 0
    Last Post: 11-07-2012, 03:29 PM
  5. Getting filtered MySQL Data in Excel
    By drkilljoy in forum Excel General
    Replies: 0
    Last Post: 05-31-2011, 07:04 AM

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