Macro in MS Access to import excel file

spatel

New Member
Joined
Sep 22, 2004
Messages
14
Hello,

I am trying to create a macro in Access that'll import an excel file and append it into a access table. Currently, the macro is appending the records in the specified table howerver, I want the user to have the ability to select the file for import.

My macro is invoked by a button on my form but it'll only import the file that is specified in the path another words its static.

On the form, I want the user to select the .xls file from whatever location and then hit the button to import that file into the access talbe. How do yo do that?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Does the Excel file(s) the user might wish to import have a particular name in common?

Are they located in the same location?

If so one method could be to have a form with a (unbound) combobox which when the form opens it is populated with the names of the files the user can import.

It would also have a command button that when clicked uses the value of the combobox to import the file and append it to the table.

To give a full solution more info is needed.
 
Upvote 0
Thanks for responding!!

I am going to get 2 .xls payroll files at the end of each month (Hours.xls and Dollars.xls). Currently I manually append the files into Hours.tbl, Dollars.tbl respectively in Access.

I want to create a macro which would be invoked by the click event of a button on a Access Form. The macros is going to append each .xls to its respected tables in access.

The problem is when creating the macros the path for the file to be imported is static. I want to create someting on the form where the user has the abiility to select the file that they are going to import and the destination table that its going to be appended into.

Hope this is enough info....
 
Upvote 0
Spatel,

Did you look at the link I provided? That gives you the code that is necessary to do what you are asking, to get a "Open File Dialog" box.
 
Upvote 0
Yes I did.

I pasted the whole code into a new module, but how do invoked that from a button on a form. Like what action I need to do in order to fire that code.
 
Upvote 0
You just make reference to the "GetOpenFile" function name.

Posted below is some code where I use it to browse my files, and store whatever file I select into a variable named "myOpenFileName". This code is attached to the "Click" event of a command button I have named "cmdFileOpenBrowse".

You can then open/import/etc the file using this variable name.

Code:
Private Sub cmdFileOpenBrowse_Click()
'   Browse for open file
    
    Dim myOpenFileName As Variant
    myOpenFileName = GetOpenFile

End Sub
 
Upvote 0
Cool. IT almost works!, thanks

however, I want to append it to the hours table in access so do I still have to call that Macro in my click event after I make refererence to the get file function.

ALso, when the dialgoue box opens up, It only lets me select a .mdb file and I can't change the file type to "ALL" to view all files in the directory cause the file I am importing is an .xls file.

thanks bro...
 
Upvote 0
ALso, when the dialgoue box opens up, It only lets me select a .mdb file and I can't change the file type to "ALL" to view all files in the directory cause the file I am importing is an .xls file.
Under the "GetSaveFile1" function in that code, try changing:
Code:
    strFilter = ahtAddFilterItem(strFilter, _
'                "Access (*.mdb)", "*.MDB")
to
Code:
    strFilter = ahtAddFilterItem(strFilter, _
                  "Microsoft Excel (*.xls)", "*.XLS")
Regarding the append of data to your table, what code are you using to import the file? Most of the import techniques I use will append the data (as matter as fact, I usually need to delete the data with more code if I want to overwrite it).
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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