Import Select Columns

juneau730

Board Regular
Joined
Jun 7, 2018
Messages
111
Good day all,

I am back seeking a little insight. I currently import an entire excel into a tbl within Access. It works great with no issues, but I don't need all that data. So, I am wondering if there is a simple way to import only the columns I do need.

I the case of the spreadsheet in question I only need columns F, G, I, L and M
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How are you doing the import now?

Also, why do you want to only import some columns? Is it because of the size of storing extra data?
 
Upvote 0
The behind the "why" is that the reports contain a LOT of data that isn't necessary for what we need, but we can't ensure that other users will delete the unnecessary columns before trying to import.

Right now, we are just using a simple DoCmd string.

VBA Code:
 Set objShell = VBA.CreateObject("wscript.shell")
 strGandL = Application.CurrentProject.Path & "\Separated Users\GandL.xlsx"

    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE * FROM [tbl GandL]"
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "tbl GandL", strGandL, True
    DoCmd.SetWarnings True
    Forms![_Navigation Form].NavigationSubform.Form.Requery
 
Upvote 0
One simple way is import the table to a temp (staging) table, then move only the necessary columns:

VBA Code:
    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE * FROM [tbl GandL_TEMP]"
    DoCmd.RunSQL "DELETE * FROM [tbl GandL]"
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "[tbl GandL_TEMP]", strGandL, True
    DoCmd.RunSQL "Insert into [tbl Gandl] (ColA, ColB, ColC, ColD) select ColA, ColB, ColC, ColD from [tbl_Gandl_TEMP]"
    DoCmd.SetWarnings True
    Forms![_Navigation Form].NavigationSubform.Form.Requery

I'm sure there are many other solutions as well.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top