471,579 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,579 software developers and data experts.

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!

Mar 28 '06 #1
2 6811
one way:
From within Access:


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

Mar 28 '06 #2
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;Database=" _
& FileName & "].[" & SheetName & "] WHERE False;"

Set R = CurrentDB.OpenRecordset(S, dbopensnapshot)
For Each F in R.Fields
'do stuff with F.Name
...
Next
R.Close
On 28 Mar 2006 12:49:35 -0800, go****@darincline.com wrote:
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!


--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

Mar 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stan Brown | last post: by
1 post views Thread by socasteel21 via AccessMonster.com | last post: by
TMS
2 posts views Thread by TMS | last post: by
3 posts views Thread by D.Stone | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.