MS Access - Import data from Excel to Access using Criteria
Hi,
I have a form that import data from excel from Access. I have written a code that Import data but i want to Import data on the basis of criteria as if the field datatype has text as "Cap" it should import that data to table "tblCap", if it have text as "Gen" it should import all those rows to "tblGen", is there any way to do that?
Attach is a code for import.
Thank you.
Function selectFile() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd If .Show Then selectFile = .SelectedItems(1) Else 'stop execution if nothing selected End End If End With Set fd = NothingEnd FunctionPrivate Sub Command0_Click()On Error GoTo ErrorHandler 'disable ms access warnings DoCmd.SetWarnings False 'Delete existing data in table 'This is optional. If you are combining spreadsheets 'in your table, don't run this query 'DoCmd.RunSQL "DELETE * FROM names" 'load spreadsheet DoCmd.TransferSpreadsheet acImport, 8, "tblCapacity", selectFile(), True MsgBox "Import Successful" 're-enable ms access warnings DoCmd.SetWarnings TrueExit SubErrorHandler:MsgBox "There was an Error: " & Err & ": " & Error(Err)End Sub
Don't import, but link the Excel data (use acLink).
Then use this linked tables as source in a simple select query where you filter (and modify) data as needed for clean-up.
Now use this query as source when you import the data.
/gustav
WS
ASKER
@Gustav, in Linking how will i will be able to pick file?
Also how to filter in query? Like how will i separate data for example 5 records are for tblCap and 15 for tblGen so how i am going to append those to their original or main table?
Gustav Brock
> Linking how will i will be able to pick file?
Same as for import.
> how to filter in query?
Well, like any other query. You have the data and know what you wish include or exclude and to import. So that's up to you.
@Gustav, As i need to append via query so in my operational db there are 5 tables in that case i have to make 5 query , isn't there a way that this all can be done via VBA code?
Also how i will apply criteria?
Gustav Brock
Yes, but - given from experience - you will need to create those five queries anyway for debugging and checking the collected data from your sample data.
So, when success, you will have the five select queries ready for use as source in your append queries.
/gustav
WS
ASKER
isn't there any other way? as my user don't want to have so much queries.
Running these append queryies would simply be part of the import process.
if that's the case then it's fine but how i could do that, how 5 append queries would run behind each other?
@Pathartman, yeah you are right it shouldn't be 8.
Also my question from all of you will again be how i will apply criteria? Like the code i wrote it is exporting data but how to apply criteria that on the basis on field data type with specific text in it it should append data?
WS
ASKER
I hope this may explain more what i am trying to achieve. Attach is a sample DB with sample Excel file. You can have a look at form Import and have a look at field Datatype. In the field Datatype there are records for "Table1","Table2", and "Table3", how will i apply the criteria to append record to their respective tables? Wsm93--1-.accdb Book1---Copy.xlsx
@Gustav, how can i show a message box that after all this the data is added. I wrote below calling as MsgBox "Data Added Successfully!" but it's not working, any idea?
Then use this linked tables as source in a simple select query where you filter (and modify) data as needed for clean-up.
Now use this query as source when you import the data.
/gustav