Avatar of SteveL13
SteveL13
Flag for United States of America asked on

Verify that the field header names in an Excel file match those in the resulting table

I want to somehow verify that the field header names in an Excel file match those in the resulting table before performing the import via this line of code:


DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTEMPImportRecords", Me.FileList, True


How can I make sure they match?  If they don't provide a warning message to the user and exit the sub.

Microsoft Access

Avatar of undefined
Last Comment
John Tsioumpris

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Fabrice Lambert

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Fabrice Lambert

Or, 2nd option, use the tabldefs collection to check your columns names (still start by linking your spreadsheet), this can be done with an helper function:
Public sub Program
        '// Ensure the spreadsheet is linked first

    Dim db As DAO.Database
    Set db = CurrentDb

    Dim Source As DAO.TableDef
    Set Source = db.TableDefs("tblTEMPImportRecords ")

    Dim Target As DAO.TableDef
    Set Target = db.TableDefs("TargetTable")

    Dim Field As DAO.Field
    For Each Field In Target.Fields
        If Not (ExistInCollection(Field.Name, Source.Fields) Then
            '// column don't exist, react accordingly
        End If
    Next

        '// Ok to perform the import from here
End Sub

Open in new window

Public Sub ExistIncollection(ByValKey As String, byref Col As Object) As Boolean
   ExistIncollection = ExistIncollectionByVal(Key, Col) Or ExistIncollectionByRef(Key, Col)
End Sub

Private Sub ExistIncollectionByVal(ByValKey As String, byref Col As Object) As Boolean
On Error Goto Error
    Dim Item As Variant
    Item = Col(Key)
    ExistIncollectionByVal = True
Exit Sub
Error:
    ExistIncollectionByVal = False
End Sub

Private Sub ExistIncollectionByRef(ByValKey As String, byref Col As Object) As Boolean
On Error Goto Error
    Dim Item As Variant
    Set Item = Col(Key)
    ExistIncollectionByRef = True
Exit Sub
Error:
    ExistIncollectionByRef = False
End Sub

Open in new window

SteveL13

ASKER
I can't link to the spreadsheet because more than one will be importing. importing into a temporary table.
Fabrice Lambert

I can't link to the spreadsheet because more than one will be importing. importing into a temporary table.
More than one spreadsheet or more than one user ?

In the 1st case, what prevent you from linking / unlinking each spreadsheet ? (link a sheet, process it, unlink it. Rince repeat with other sheets).

In the 2nd case, each user should have a local copy of the front-end, where the temporary table(s) will be created before writing data to the back-end. I don't see any reason to prevent from linking / unlinking either.

Else, maybe import specs (can't answer more as I never use those).
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
John Tsioumpris

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question