This document describes the process of importing a customer parts list from the template developed during the 4DX process. The document may be a Google Document and shared with the customer while they are filling it in, but will be exported to Excel to work with it.
The file format is very specifically defined. Among the rules are:
For the import, we will create a separate comma delimited file for each Tab in the spreadsheet. To make this simpler, follow these steps:
Sub ExportSheetsToCSV() Dim xWs As Worksheet Dim xPath As String xPath = "<<path>>" Dim xFile As String For Each xWs In ThisWorkbook.Worksheets xWs.Copy xFile = xPath & "\" & xWs.Name & ".csv" Application.ActiveWorkbook.SaveAs Filename:=xFile, FileFormat:=xlCSV, CreateBackup:=False Application.ActiveWorkbook.Saved = True Application.ActiveWorkbook.Close Next End Sub
For some reason, this VBA does not always work. If you get an error message, you can use this version
Sub ExportSheetsToCSV() Dim xPath As String Dim xFile As String xPath = "C:\Users\scott\Desktop\Customer Data\Tabs" 'Application.ActiveWorkbook.Path For Each xWs In ThisWorkbook.Worksheets xFile = xPath & "\" & xWs.Name & ".csv" xWs.SaveAs Filename:=xFile, FileFormat:=xlCSV, CreateBackup:=False Next Application.ActiveWorkbook.Close End Sub
Now that you have the CSV files, you need to get them into the Database. This routine brings in all of the CSV files into their own tables called PartImport.{filename}.
You'll have to add a lot of stored procedures and functions to the database for this to run. See Scott or a consultant for the latest versions.
In order to retrieve the list of files, SQL must make a directory call to read the contents of the folder. By default, this access is denied in SQL since it could be used by malicious software to access the local drive.
To enable these features, run these SQL commands
EXEC sp_configure 'show advanced options', 1 GO RECONFIGURE; GO sp_configure 'xp_cmdshell', 1; GO RECONFIGURE; sp_configure 'Ad Hoc Distributed Queries', 1; GO RECONFIGURE;
You also need to download and install the Access Database Engine(AccessDatabaseEngine_X64.exe). You can get the file using the link below.
https://www.microsoft.com/en-us/download/details.aspx?id=39358.
To import the files as parts involve these steps:
DECLARE @Path VARCHAR(255) = '{fill in}' , @SkipRows VARCHAR(255) = 3 ; EXEC csp_ImportPartSpreadsheetTabs @Path = @Path , @SkipRows = 3 , @DontCommit = 0 -- 1 to test only but not save, 0 to save
If you made a mistake and want to re-import, you can delete all of the parts that were imported that day with this query, fix the data, and then run it again. This will delete anything done that day, whether by you or a user so only run this on a backup of the data.
DELETE FROM Part WHERE ModifiedbyUser = 'csp_ImportPartSpreadsheet' DELETE FROM PricingElement WHERE ModifiedbyUser = 'csp_ImportPartSpreadsheet' DELETE FROM PartInventoryConversion WHERE ModifiedbyUser = 'csp_ImportPartSpreadsheet' DELETE FROM Inventory WHERE ModifiedbyUser = 'csp_ImportPartSpreadsheet' DELETE FROM InventoryLog WHERE ModifiedbyUser = 'csp_ImportPartSpreadsheet'
Not all areas are currently implemented. These include:
Other Notes: