Forum Discussion
Sean Sodha
Apr 25, 2018Copper Contributor
Excel Data Exported to Microsoft Access --> Object Required
Hi everyone. Within Excel, I am trying to create a table in Microsoft Access, and then export a range of data in Excel into that Access table. I am successful in creating a table in Microsoft Access....
Matt Mickle
Apr 25, 2018Bronze Contributor
This works for me from the database itself. It seems much simpler....
Option Compare Database Sub Export() Dim strPathFile As String Dim strPath As String Dim database_table_name As Variant database_table_name = InputBox("Please Give a Name To The Test You Are Going to Export (No Spaces): ") strPath = "C:\Users\mmickle1\Documents\Test1.xlsm" CurrentDb.Execute "CREATE TABLE " & database_table_name & " ([Dates] datetime NULL , " & _ "[Demand] DOUBLE , " & _ "[Inventory Position] DOUBLE, " & _ "[On Order] DOUBLE, " & _ "[On Hand] DOUBLE)" DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _ database_table_name, strPath, True, "I1:M100" End Sub
However if you are getting an object required error maybe try changing this:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _ database_table_name, strPath, True, "I1:M100"
to this:
connObj.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _ database_table_name, strPath, True, "I1:M100"
- Sean SodhaApr 30, 2018Copper Contributor
Hmm it gives me an "Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another". Any advice?
- Matt MickleApr 30, 2018Bronze Contributor
Try this VBA code block instead.... You will need to add the reference to the Access Object Library as mentioned in the code comments. In addition if the file you are running the code out of is not "C:\Users\jt\Desktop\Inventory Policy Analysis.xlsm" then you will need to update the FileName Parameter :
Option Explicit Sub AccImport() Dim acc As New Access.Application Dim database_table_name As Variant 'You need to set a reference to the below Object Library 'In the Visual Basic Editor do this: 'Tools > References > Microsoft Access Data XX.X Object Library 'The XX.X will be your version of Access......something like this 12.0, 14.0, 16.0 etc.... database_table_name = InputBox("Please Give a Name To The Test You Are Going to Export (No Spaces): ") With acc .OpenCurrentDatabase "C:\Users\jt\Desktop\Microsoft_Access\HSS_Test.accdb" .DoCmd.RunSQL "CREATE TABLE " & database_table_name & " ([Dates] datetime NULL , " & _ "[Demand] DOUBLE , " & _ "[Inventory Position] DOUBLE, " & _ "[On Order] DOUBLE, " & _ "[On Hand] DOUBLE)" 'In the below DoCmd.TransferSpreadsheet statement if your filename is not the file you are working in then 'you will need to update the Filename parameter to your filepath.... 'i.e. Filename:= "C:\Users\jt\Desktop\Inventory Policy Analysis.xlsm" .DoCmd.TransferSpreadsheet _ TransferType:=acImport, _ SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _ TableName:=database_table_name, _ Filename:=Application.ActiveWorkbook.FullName, _ HasFieldNames:=True, _ Range:="Simulation$I1:M100" .CloseCurrentDatabase .Quit End With Set acc = Nothing End Sub
- Sean SodhaMay 03, 2018Copper Contributor
Thank you so much! I was able to get it to work!!