Any way to validate spreadsheet headers before import?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • google@darincline.com

    Any way to validate spreadsheet headers before import?

    Is there anyway to programatically confirm that specific field names
    exist in the first row of a given Excel spreadsheet from code within
    Access? I have code set up to import data from our spreadsheets from
    HQ into our Access database for further use, using the first row as
    column headings to determine the destination field in the Access table.
    Since it's possible (and probable) that HQ could change the format of
    the spreadsheets without my knowledge, I'd like to be able to confirm
    that the needed fields are present (and named properly) before import.
    I'm not very familiar with examining Excel data from within Access.
    Any suggestions on how to do that?

    Thanks!

  • Ron2005

    #2
    Re: Any way to validate spreadsheet headers before import?

    one way:
    [color=blue]
    >From within Access:[/color]

    Open the spreadsheet
    1) go to the desired worksheet (if they haven't changed the name on
    you) ( In one application I had to go to sheet(1) and etc and getting
    the name and seeing if part of it qualified since they change the tab
    name EVERY MONTH. If you are going to import the sheet you will either
    have to change the tab name to a standard name or change your import
    statement for the change in range name.)
    2) go to cell A1 and see if it is one of the names you require
    a) if it is add 1 to a counter.
    3) go to cell B1 and repeat the step 2 process.
    4) go through as many columns as you reasonably think they can have.
    5) close the spreadsheet
    6) At the end, if the counter does not equal the number of required
    fields you can then display an error message or form or whatever and
    stop the rest of the process.

    Danger of above is that two columns could have the same name and access
    will not like it So you may want to set flags and change your logic
    accordingly. However that then qualifies it for the "Inverse logic
    rule" 90% of the code is for the 1% of the occurances.

    In general there will probably be a problem with the import into an
    existing table if they have added or changed any of the other field
    names. So you will probably want to delete the import table and import
    to a fresh table. (Then you may have a problem if the process is
    aborted since it will not find the import table to delete the next time
    through)

    This is the voice of sad experience talking about the two possibilities
    indicated.

    Hope this gave you some ideas.

    Ron

    Comment

    • John Nurick

      #3
      Re: Any way to validate spreadsheet headers before import?

      I'd do something like this to open an empty recordset whose Fields get
      their names from the column headings:

      Dim S As String
      Dim R As DAO.Recordset
      Dim F As DAO.Field
      Dim FileName As String
      Dim SheetName As String

      ...
      S = "SELECT * FROM [Excel 8.0;HDR=Yes;Dat abase=" _
      & FileName & "].[" & SheetName & "] WHERE False;"

      Set R = CurrentDB.OpenR ecordset(S, dbopensnapshot)
      For Each F in R.Fields
      'do stuff with F.Name
      ...
      Next
      R.Close


      On 28 Mar 2006 12:49:35 -0800, google@darincli ne.com wrote:
      [color=blue]
      >Is there anyway to programatically confirm that specific field names
      >exist in the first row of a given Excel spreadsheet from code within
      >Access? I have code set up to import data from our spreadsheets from
      >HQ into our Access database for further use, using the first row as
      >column headings to determine the destination field in the Access table.
      > Since it's possible (and probable) that HQ could change the format of
      >the spreadsheets without my knowledge, I'd like to be able to confirm
      >that the needed fields are present (and named properly) before import.
      >I'm not very familiar with examining Excel data from within Access.
      >Any suggestions on how to do that?
      >
      >Thanks!
      >[/color]

      --
      John Nurick [Microsoft Access MVP]

      Please respond in the newgroup and not by email.

      Comment

      Working...