How can I modify a saved Microsoft Access 2007 or 2010 Import Specification?

后端 未结 10 731
挽巷
挽巷 2020-12-02 18:31

Does anyone know how to modify an existing import specification in Microsoft Access 2007 or 2010? In older versions there used to be an Advanced button presented during the

相关标签:
10条回答
  • 2020-12-02 18:53

    I used Mike Hansen's solution, it is great. I modified his solution in one point, instead of replacing parts of the string I modified the XML-attribute. Maybe it is too much of an effort when you can modify the string but anyway, here is my solution for that. This could easily be further modified to change the table etc. too, which is very nice imho.

    What was helpful for me was a helper sub to write the XML to a file so I could check the structure and content of it:

    Sub writeStringToFile(strPath As String, strText As String)
        '#### writes a given string into a given filePath, overwriting a document if it already exists
            Dim objStream
            
            Set objStream = CreateObject("ADODB.Stream")
            objStream.Charset = "utf-8"
            objStream.Open
            objStream.WriteText strText
            objStream.SaveToFile strPath, 2
        End Sub
    

    The XML of an/my ImportExportSpecification for a table with 2 columns looks like this:

    <?xml version="1.0"?>
    <ImportExportSpecification Path="mypath\mydocument.xlsx" xmlns="urn:www.microsoft.com/office/access/imexspec">
        <ImportExcel FirstRowHasNames="true" AppendToTable="myTableName" Range="myExcelWorksheetName">
            <Columns PrimaryKey="{Auto}">
                <Column Name="Col1" FieldName="SomeFieldName" Indexed="NO" SkipColumn="false" DataType="Double"/>
                <Column Name="Col2" FieldName="SomeFieldName" Indexed="NO" SkipColumn="false" DataType="Text"/>
            </Columns>
        </ImportExcel>
    </ImportExportSpecification>
    

    Then I wrote a function to modify the path. I left out error-handling here:

    Function modifyDataSourcePath(strNewPath As String, strXMLSpec As String) As String
    '#### Changes the path-name of an import-export specification
        Dim xDoc As MSXML2.DOMDocument60
        Dim childNodes As IXMLDOMNodeList
        Dim nodeImExSpec As MSXML2.IXMLDOMNode
        Dim childNode As MSXML2.IXMLDOMNode
        Dim attributesImExSpec As IXMLDOMNamedNodeMap
        Dim attributeImExSpec As IXMLDOMAttribute
    
        
        Set xDoc = New MSXML2.DOMDocument60
        xDoc.async = False: xDoc.validateOnParse = False
        xDoc.LoadXML (strXMLSpec)
        Set childNodes = xDoc.childNodes
     
        For Each childNode In childNodes
               If childNode.nodeName = "ImportExportSpecification" Then
                    Set nodeImExSpec = childNode
                    Exit For
                End If
        Next childNode
        
        Set attributesImExSpec = nodeImExSpec.Attributes
        
        For Each attributeImExSpec In attributesImExSpec
            If attributeImExSpec.nodeName = "Path" Then
                attributeImExSpec.Value = strNewPath
                Exit For
            End If
        Next attributeImExSpec
        
        modifyDataSourcePath = xDoc.XML
    End Function
    

    I use this in Mike's code before the newSpec is executed and instead of the replace statement. Also I write the XML-string into an XML-file in a location relative to the database but that line is optional:

    Set myNewSpec = CurrentProject.ImportExportSpecifications.item("TemporaryImport")
        myNewSpec.XML = modifyDataSourcePath(myPath, myNewSpec.XML)
        Call writeStringToFile(Application.CurrentProject.Path & "\impExpSpec.xml", myNewSpec.XML)
        myNewSpec.Execute
    
    0 讨论(0)
  • 2020-12-02 18:54

    Below are three functions you can use to alter and use the MS Access 2010 Import Specification. The third sub changes the name of an existing import specification. The second sub allows you to change any xml text in the import spec. This is useful if you need to change column names, data types, add columns, change the import file location, etc.. In essence anything you want modify for an existing spec. The first Sub is a routine that allows you to call an existing import spec, modify it for a specific file you are attempting to import, importing that file, and then deleting the modified spec, keeping the import spec "template" unaltered and intact. Enjoy.

    Public Sub MyExcelTransfer(myTempTable As String, myPath As String)
    On Error GoTo ERR_Handler:
        Dim mySpec As ImportExportSpecification
        Dim myNewSpec As ImportExportSpecification
        Dim x As Integer
    
        For x = 0 To CurrentProject.ImportExportSpecifications.Count - 1
        If CurrentProject.ImportExportSpecifications.Item(x).Name = "TemporaryImport" Then
            CurrentProject.ImportExportSpecifications.Item("TemporaryImport").Delete
            x = CurrentProject.ImportExportSpecifications.Count
        End If
        Next x
        Set mySpec = CurrentProject.ImportExportSpecifications.Item(myTempTable)
        CurrentProject.ImportExportSpecifications.Add "TemporaryImport", mySpec.XML
        Set myNewSpec = CurrentProject.ImportExportSpecifications.Item("TemporaryImport")
    
        myNewSpec.XML = Replace(myNewSpec.XML, "\\MyComputer\ChangeThis", myPath)
        myNewSpec.Execute
        myNewSpec.Delete
        Set mySpec = Nothing
        Set myNewSpec = Nothing
        exit_ErrHandler:
        For x = 0 To CurrentProject.ImportExportSpecifications.Count - 1
        If CurrentProject.ImportExportSpecifications.Item(x).Name = "TemporaryImport" Then
            CurrentProject.ImportExportSpecifications.Item("TemporaryImport").Delete
            x = CurrentProject.ImportExportSpecifications.Count
        End If
        Next x
    Exit Sub    
    ERR_Handler:
        MsgBox Err.Description
        Resume exit_ErrHandler
    End Sub
    
    Public Sub fixImportSpecs(myTable As String, strFind As String, strRepl As String)
        Dim mySpec As ImportExportSpecification    
        Set mySpec = CurrentProject.ImportExportSpecifications.Item(myTable)    
        mySpec.XML = Replace(mySpec.XML, strFind, strRepl)
        Set mySpec = Nothing
    End Sub
    
    
    Public Sub MyExcelChangeName(OldName As String, NewName As String)
        Dim mySpec As ImportExportSpecification
        Dim myNewSpec As ImportExportSpecification
        Set mySpec = CurrentProject.ImportExportSpecifications.Item(OldName)    
        CurrentProject.ImportExportSpecifications.Add NewName, mySpec.XML
        mySpec.Delete
        Set mySpec = Nothing
        Set myNewSpec = Nothing
    End Sub
    
    0 讨论(0)
  • 2020-12-02 18:54

    When I want to examine or change an import / export specification I query the tables in MS Access where the specification is defined.

    SELECT 
        MSysIMEXSpecs.SpecName,
        MSysIMexColumns.*
    FROM 
        MSysIMEXSpecs
        LEFT JOIN MSysIMEXColumns 
        ON MSysIMEXSpecs.SpecID = MSysIMEXColumns.SpecID
    WHERE
        SpecName = 'MySpecName'
    ORDER BY
        MSysIMEXSpecs.SpecID, MSysIMEXColumns.Start;
    

    You can also use an UPDATE or INSERT statement to alter existing columns or insert and append new columns to an existing specification. You can create entirely new specifications using this methodology.

    0 讨论(0)
  • 2020-12-02 18:57

    Why so complicated?

    Just check System Objects in Access-Options/Current Database/Navigation Options/Show System Objects

    Open Table "MSysIMEXSpecs" and change according to your needs - its easy to read...

    0 讨论(0)
  • 2020-12-02 18:57

    I have just discovered an apparent bug in the whole Saved Import/XML setup in Access. Also frustrated by the rigidity of the Saved Import system, I created forms and wrote code to pick apart the XML in which the Saved Import specs are stored, to the point that I could use this tool to actually create a Saved Import from scratch via coded examination of a source Excel workbook.

    What I've found out is that, while Access correctly imports a worksheet per modifications of default settings by the user (for example, it likes to take any column with a header name ending with "ID" and make it an indexed field in the resulting table, but you can cancel this during the import process), and while it also correctly creates XML in accordance to the user changes, if you then drop the table and use the Saved Import to re-import the worksheet, it ignores the XML import spec and reverts back to using its own invented defaults, at least in the case of the "ID" columns.

    You can try this on your own: import an worksheet Excel with at least one column header name ending with "ID" ("OrderID", "User ID", or just plain "ID"). During the process, be sure to set "Indexed" to No for those columns. Execute the import and check "Save import steps" in the final dialog window. If you inspect the resulting table design, you will see there is no index on the field(s) in question. Then delete the table, find the saved import and execute it again. This time, those fields will be set as Indexed in the table design, even though the XML still says no index.

    I was pulling my hair out until I discovered what was going on, comparing the XML I built from scratch with examples created through the Access tool.

    0 讨论(0)
  • 2020-12-02 19:08

    Tim Lentine's answer seems to be true even in the full release. There is just one other thing I would like to mention.

    If you complete your import without going into "Advanced..." and saving the spec, but you do save the import for reuse at the end of the wizard (new feature AFAIK), you will not be able to go back and edit that spec. It is built into the "Saved Import". This may be what Knox was referring to.

    You can, however, do a partial work around:

    1. Import a new file (or the same one all over again) but,
    2. This time choose to append, instead of making a new
    3. Click OK.
    4. Go into "advanced" All your column heading and data-types will be there.
    5. Now you can make the changes you need and save the spec inside that dialog. Then cancel out of that import (that is not what you wanted anyway, right?)
    6. You can then use that spec for any further imports. It's not a full solution, but saves some of the work.
    0 讨论(0)
提交回复
热议问题