Automatically link tables

swendingo

Board Regular
Joined
Sep 11, 2003
Messages
90
Hello,

I have recently started using access. I am not sure if what I need to do is possible.

I have a database that I am using to house employee information and performance.

The application we use to measure quality exports the information into excel but doesn't date/time stamp each entry.

I am looking for a way to program access to automatically link excel files that I drop into a directory. I would name them stats.week9.xls as a standard convention or something like that.

I would just append the records to a table but since there is no time date stamp I would not be able to differentiate one weeks score from the next. And I would like to keep previous weeks.

The first row is always the field name
And field names are always the same

If this is possible I would also like to automatically set a relationship to another table.

Thanks,

Steve
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi Steve,

See if this helps...

I'd recommend that you import the data from Excel into an Access table each week, then use queries to write the week name (you can provide that) and append the data to your main table. You should also have a Year field, so that Week01 from 2007 doesn't get confused down the track with Week01 from 2008.

So the basic steps would be
1. Import new data to a dummy table that you use as a halfway house. This might seem strange but I find that it helps me get around many of the import errors when you pull Excel data into Access.
2. Use a data entry form to enter the year and the week. Note the syntax for the week no: Week01, etc will always sort correctly. Week1 etc won't because of how text is sorted.
3. Run a query that appends the new data to the main table.

There are many advantages to doing it this way. Here are just some:
1. By importing into a pre-existing table, you keep the structure constant. This is a huge benefit when building forms, queries and reports on the data.
2. using a staged import helps you to get the data types right, and do some clean-up if necessary.
3. Access runs much faster on native tables than it does on linked Excel files.
4. If anyone else wants to look at the files in isolation, they will be unable to if you link them to an Access database and then open the database.
5. The latest Service Packs of Access XP and higher WILL NOT let you dit any data in linked Excel files. the data is read-only in Access.

Denis
 
Upvote 0
Thanks Denis,

I am fine with importing instead of linking. And I am fully able to do that. My issue is that I am going to have to pull several spread sheets each week and import them. I would like to be able to automate the import (or Linking) process so that all i have to do is click a button and not go through the wizard.
 
Upvote 0
If you don't want to go through the wizard you can set up a routine that propmts you to browse for a file to open. how you do this depends on which version of Access you use -- it is much easier to code in XP and higher.

You can then tell access which Import Spec to use, so it doesn't need guidance on how to treat headers and fields. to create an import spec, see here.

I'd envisage using a form where you can set date/week/year values, then click a button that launches the rest of the process. So... which Access version do you use?

Denis
 
Upvote 0
Can someone define in elementary terms what "appending" a table does? Like what do you mean when you say "append the table"?
 
Upvote 0
when you append data to your table (thru an append query) you are adding rows to an existing table by means of an action query.

hth,
Giacomo
 
Upvote 0
This worked for me. I am going to play around with the corresponding Import function.


Code:
DoCmd.TransferSpreadsheet transfertype:=acLink, _
            spreadsheettype:=acSpreadsheetTypeExcel5, _
            tablename:="AARWeek2", _
            Filename:="F:\DB\AAR\AAR.week2.xls", _
            hasfieldnames:=True, _
            Range:="Sheet1!"
 
Upvote 0
Here's an import option. Assuming you have a form with a button called cmdImportTextFile, you are using a .csv data source, you have created an import file spec using the previous link I gave you, and the destination table is tblImport:
Code:
Private Sub cmdImportTextFile_Click()

'Requires reference to Microsoft Office 10.0 Object Library.

   Dim fDialog As Office.FileDialog
   Dim varFile As Variant

   'Clear listbox contents.
   Me.FileList.RowSource = ""

   'Set up the File Dialog.
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
   With fDialog
      'Allow user to make multiple selections in dialog box
      .AllowMultiSelect = True
            
      'Set the title of the dialog box.
      .Title = "Please select one or more files"

      'Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "Text Files", "*.TXT,*.CSV"
      .Filters.Add "All Files", "*.*"

      'Show the dialog box. If the .Show method returns True, the
      'user picked at least one file. If the .Show method returns
      'False, the user clicked Cancel.
      If .Show = True Then
         'Loop through each file selected and add it to our list box.
         For Each varFile In .SelectedItems
            DoCmd.TransferText acImportDelim, "your file import spec", "tblImport", varFile, True
         Next
      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With

   'assuming you have an update query for writing the dates:
   'comment out the code below if you have not. 
   'change the name of the update query to suit
   Set dbs = CurrentDb()
   Set qdf = dbs.QueryDefs("qupWriteDates")
   DoCmd.RunSQL qdf.SQL
   Set qdf = Nothing
   Set dbs = Nothing
   Set fDialog = Nothing

End Sub


This is step one. Using the FileDoalog object you can pick the file to import. You will also need to enter the dates, which can be done with an Update query. I have added the code for this at the end of the Sub: modify or delete as required.

Denis
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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