views:

246

answers:

3

how to create an access form which has import excel file button. and after selecting excel file it automatically creates a table in the database with collumn headers as excel first row and data as excel other rows. if you think i am not putting any effort please give me suggestion or reference and ill do it on my own.

+1  A: 

If we assume that you are able to create a form and wire up a button you have two issues:

  1. The file open dialog.
  2. Triggering the import.

For 1 you should be able to use the standard Microsoft file dialogs - my VB.OLD and Access are spectacularly rusty (no access 2007) but you can reference the appropriate COM assemblies from Access after which it becomes fairly easy.

2 is a bit more interesting - I beleive you can pretty much do this by menu selection from within access in which case, at least as a first step, you should be able to automate the same steps - pretty much anything you can do from a menu you can also do by calling the relevant command from VBA. The more complex solution would be to create VBA logic to create a linked table that links to the Excel file and then do a create table query and then drop the link.

In terms of effort, the form is something one would expect you to be able to do without much help - however automating something like an import from excel is not necessarily obvious.

Murph
+4  A: 

For versions of Access since 2003, you can use the File Dialog to allow the user to browse for the file they want, prior to that, you can use API calls. If this is overkill for you, you can have the user type in the file name and path, but you will have to check that it exists using code (Dir may suit).

It would be best to use TransferSpreadsheet method of the DoCmd object (available in any version of Access from, AFAIK, 1997 onward) to import the spreadsheet. This can be run as VBA (code) or a macro.

Remou
I would recommend against using the Office File Dialog in any version of Access, and instead use the Windows API version. It means you don't have to muck with late binding or maintinaing references. Also, MS has a habit of pulling features out of later versions of Office (e.g., FileSearch object, removed from Office 2007), while the API call will always work.
David-W-Fenton
+1  A: 

An example using Access 2003 would be as follows for selecting a file:

Dim fDialog  As Office.FileDialog
Dim strFile As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
    .InitialFileName = "C:\temp\*.xls"
    .Filters.Clear
    .Filters.Add "Excel file", "*.xls"
    .Filters.Add "All Files", "*.*"
    If .Show = True Then
        strFile = .SelectedItems(1)
    End If

End With

Debug.Print strFile

Note you would need to add a Reference to the Office 12 Object Library

To Import the file you can use the TransferSpreadsheet Function of the DoCmd Object. For E.g.

    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "ExcelImport", strFile, True

The Access table called ExcelImport would have to already exist in the database.

Mark3308
Again, I would say, as I did for Remou's answer, that it's better to avoid dependencies on an Office library when you can do the same thing with Windows APIs and hardly any more code. The API version will always work, but MS may someday choose to remove the file dialog from the Office library (as they did with the FileSearch object in Office 2007).
David-W-Fenton