Avatar of WS
WS
Flag for United Arab Emirates asked on

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 = Nothing
End Function


Private 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 True
 
Exit Sub
 
ErrorHandler:
MsgBox "There was an Error: " & Err & ": " & Error(Err)
End Sub

Open in new window

Microsoft AccessVBA

Avatar of undefined
Last Comment
WS

8/22/2022 - Mon
Gustav Brock

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
WS

ASKER
@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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Gustav Brock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Dale Fye

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
PatHartman

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
WS

ASKER
@Dale,
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
ASKER CERTIFIED SOLUTION
Gustav Brock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
WS

ASKER
@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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Gustav Brock

Can't tell. That line of code will always work.

/gustav
WS

ASKER
Thank you everyone.