Results 1 to 17 of 17

Thread: Excel to Access using VBA MS access

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Question Excel to Access using VBA MS access

    Hi,

    Can anyone help me how to export specific excel file with only selected column to a specific table in MS access?

    I can do that using VBA ms excel but i want to try it in MS Access VBA code.
    The step is selecting 1st the ms Access database to be updated and then to follow selecting MS excel file that will be the source of data.

    Thanks!
    "Its easy to teach FAITHFUL MAN to be ABLE than ABLE MAN to be FAITHFUL"

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel to Access using VBA MS access

    How about using Access to import the range?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: Excel to Access using VBA MS access

    Can...

    All I want is to move data form excel to specific MS access table.

    Meaning there is diff MS access tool (VBA) in between the 2 (MS excel and MS access)
    "Its easy to teach FAITHFUL MAN to be ABLE than ABLE MAN to be FAITHFUL"

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel to Access using VBA MS access

    Ok, lets restart here.

    Where do you need to execute (button click or whatever) the transfer from?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel to Access using VBA MS access

    Hi Ash

    What I could gather from your post is that you are having trouble opening the relevant Excel File and then getting the relevant values from that file to store in the access table...

    If I have understood you correctly then this should help...

    Paste this code in the click event of a commanbutton say(cmd1)

    I am not writing the code to update the table as I am assuming that you know how to do that...

    vb Code:
    1. Option Compare Database
    2.  
    3. Private Sub cmd1_Click()
    4.  
    5. 'Opening Excel From Access
    6. Dim oXLApp As Excel.Application
    7. 'Declare the object variables
    8. Dim oXLBook As Excel.Workbook
    9. Dim oXLSheet As Excel.Worksheet
    10.  
    11. Set oXLApp = New Excel.Application
    12.  
    13. 'Open the relevant Excel File
    14. 'Replace it with your file name
    15. Set oXLBook = oXLApp.workbooks.Open("C:\Temp\myfile.xls")
    16.  
    17. oXLApp.Visible = True
    18. 'Show it to the user
    19. 'Comment the above if you don't want the user to see the Excel File
    20.  
    21. '****************** updating the Table ***********************
    22.  
    23. '********** From Excel file, Say from Cell C1:C10 ************
    24. For i = 1 To 10 'Change 10 to the relevant row number
    25.  
    26. 'Update the relevant table field using a loop by storing the
    27. 'Range("C" & i).Value
    28.  
    29. Next i
    30.  
    31. MsgBox "Table Updated"
    32.  
    33. Set oXLSheet = Nothing
    34. 'Disconnect from all Excel objects
    35. Set oXLBook = Nothing
    36. Set oXLApp = Nothing
    37.  
    38. End Sub

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel to Access using VBA MS access

    Koolsid, that code is to be executed from within Access. I dont think that will be what the poster wants.
    The step is selecting 1st the ms Access database to be updated and then to follow selecting MS excel file that will be the source of data.
    Which is why I havent written any code yet.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel to Access using VBA MS access

    I can do that using VBA ms excel but i want to try it in MS Access VBA code.
    The step is selecting 1st the ms Access database to be updated and then to follow selecting MS excel file that will be the source of data.
    Hi Rob

    What I understood from the quote is that : From within Access, he wants to access the table and then access the Excel File to pull the data...

    I might be wrong...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel to Access using VBA MS access

    Yes, but what threw me off was "The step is selecting 1st the ms Access database to be updated ..."

    Maybe doing it in some other environment or selecting another access db from within an access db or ???
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Question Re: Excel to Access using VBA MS access

    I will not make it very confusing. Actually I have 3 files as follows:

    1) MS Access Database (where all the data from excel be transferred here)
    2) Multiple excel file ( I have raw data from excel, different workbooks, and the data will be moved to each table inside MS access Database)
    3) MS Access Tool : ( this is the one containing a code to update the MS Access database by selecting MS Access database to be updated and then one select multiple excel file workbook. Inside the code I will chooses the file name of each excel file so that I can move the data inside each table inside MS access database)

    I hope it not make everybody confused on this. My apology if this not that clear on my first post...... You see i just want to create a database with many tables that contain data's from ms excel.... All the data I have is in MS excel and I just want to put it inside the database... I can do it in MS excel like this:

    VBA excel:

    Set rS = db.OpenRecordset("RECORD_NAME", dbOpenTable)
    On Error Resume Next
    r = 6
    Do While Len(Range("A" & r).Formula) > 0

    With rS
    .AddNew
    '.Fields("Date") = Range("A" & r).Value
    '.Fields("PLMN name") = Range("B" & r).Value


    .Fields("Date") = Range("A" & r).Value
    .Fields("name") = Range("B" & r).Value
    .Fields("Surname") = Range("C" & r).Value
    .update
    End With

    r = r + 1 ' next row
    Loop
    rS.Close
    Set rS = Nothing
    db.Close
    Set db = Nothing

    You see I can select specific column in MS excel and move it to specific column in a specific table.... My problem now is I want to migrate the code inside no.3 (MS Access Tool). Now everybody is wondering why I need to select a specific database.. The reason is we just need the tool to update it. And after 1 month data, I will discharge this database and create a new one again... So i dont want to create a code inside this database since after a month a new one is needed... It will just serve as a storage of data (like swl server) and then there is another tool I have to process all the raw data in this MS Access database.

    Thank a lot for your concern.... Hope you all can help me with this...Best regards...
    Last edited by Asher David; Mar 31st, 2007 at 01:03 AM. Reason: wrong grammar
    "Its easy to teach FAITHFUL MAN to be ABLE than ABLE MAN to be FAITHFUL"

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel to Access using VBA MS access

    See this FAQ thread for the built in function in Access for importing a Excel spreadsheet.

    http://vbforums.com/showthread.php?t=402075
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Junior Member
    Join Date
    Apr 2007
    Posts
    28

    Re: Excel to Access using VBA MS access

    Hey Kool,

    I get a compile error from the very 1st line
    Dim oXLApp As Excel.Application
    Its says "User-defined type not defined"
    Is there some switch or something that needs to be set for this work?

    <>Sulabh

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel to Access using VBA MS access

    Welcome to the Forums.

    You need to add a reference to Excel xx.0 Object Library
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    Junior Member
    Join Date
    Apr 2007
    Posts
    28

    Re: Excel to Access using VBA MS access

    Thanks for the welcome,

    but I still have this issue so I looked at your example "Excel 2003 And Early Binding Automation Code Example"
    Copied and pasted it. Does that code work for VBA in access? cause thats what im using. So anyway I get the error it just moved from the sub command button to the sub form load when i double click the form. What up... I don't know man... is there some other step im missing?


  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel to Access using VBA MS access

    It only deals with Excel so adding the Reference via the menu in the VBA IDE behind the form is all that is required.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    Junior Member
    Join Date
    Apr 2007
    Posts
    28

    Re: Excel to Access using VBA MS access

    Quote Originally Posted by RobDog888
    It only deals with Excel so adding the Reference via the menu in the VBA IDE behind the form is all that is required.

    In the VBA code screen go to Tools -> References ->Click Microsoft Excel 12.0 Object Library. Done.




    I figured it out thanks.

    <>Sulabh
    Last edited by SulPal; Apr 5th, 2007 at 10:48 AM.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: Excel to Access using VBA MS access

    Hi Robb,

    But does this code cannot select the column I want to copy (specific column). You see let say I have 20 columns with data's but I want to copy the specific column only.
    I tried this one but it seems it can copy all the excel sheets but not specific cell.

    Thanks!
    Last edited by Asher David; Apr 19th, 2007 at 05:45 AM.
    "Its easy to teach FAITHFUL MAN to be ABLE than ABLE MAN to be FAITHFUL"

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: Excel to Access using VBA MS access

    anymore help about by previous query?

    Thanks!
    "Its easy to teach FAITHFUL MAN to be ABLE than ABLE MAN to be FAITHFUL"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width