Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

So yesterday’s form code: Text File Import / Export Recipe code | Access JumpStart refers to functions in a module called AppProcedures. What’s in this module?

  • Used with the first form button: GetRawFileName() Function returns a string with a full path and a file name which we will use to read the file. This simply is a passthrough function to my HALfile library which opens a system file open dialog to get the file name and validates the input. The function will return a blank string if the file was invalid or the user cancels the dialog.
  • The Function GetRawEDICustomer creates each type of object using the iImportFile interface and checks for it’s identifier string to return the appropriate iImportFile object base on the actual file.
  • Used with the second form button: GetOutputFileName Function which returns a string with a full path and file name to the file the user wants to save the output file as. Again, this uses a HALfile library function to get the final result and the argument to the function sets the default.
  • Once an output filename is specified, continues processing the second form button click: ConvertRawEDIFileToAccessImportFile is a function which takes the raw file name (from the GetRawFileName function) and the output file path and name (from the GetOutputFileName function) and then runs through and converts each line of the import file updating the status field for each record.
Option Compare Database 
Option Explicit 

Public Function GetRawFileName() As String 
   GetRawFileName = HALFile.dialogOpenFileName( "",CurrentProject.Path, "Select Raw EDI file", "All Documents", "*.*") 
End Function 

Public Function GetOutputFileName(Optional OutputFile As String =  "") As String 
   Dim FileName As String: If OutputFile <>  "" Then FileName = HALFile.GetFilenameFromPath(OutputFile) 
   Dim FilePath As String: If OutputFile <>  "" Then FilePath = Left(OutputFile,InStrRev(OutputFile, "\")) 
   If Not HALFile.FolderExists(FilePath) Then FilePath = CurrentProject.Path 
   GetOutputFileName = HALFile.dialogSaveAsFileName(FileName,FilePath, "Save Export file as", "All Documents", "*.*") 
End Function 

Public Sub ConvertRawEDIFileToAccessImportFile(RawFileName As String,Customer As iImportFile) 
   Dim RawLines As Variant,i As Long,RawLine As String,OutputLines As String,OutputFileName As String,Status As String 
   RawLines = Split(HALFile.QuickRead(RawFileName),Customer.LineSeparator) 
   Status = Form_frmConvert.txtStatus.Value 
   For i = 0 To UBound(RawLines) 
      Form_frmConvert.txtStatus = Status & vbCrLf &  "Line " & (i + 1) &  " of " & (UBound(RawLines) + 1) 
      Form_frmConvert.Repaint 
      OutputLines = OutputLines & Customer.ConvertLine(CStr(RawLines(i))) 
   Next i 
   OutputFileName = GetOutputFileName(Customer.DefaultFileName) 
   If OutputFileName <> Customer.DefaultFileName() Then _ 
      Form_frmConvert.txtStatus =  "The import routine expects the file at this location: " & vbCrLf & vbCrLf & Customer.DefaultFileName() & vbCrLf & vbCrLf & _ 
         "Please copy your saved file to that path with that name before you import." & vbCrLf & vbCrLf _ 
   Else _ 
      Form_frmConvert.txtStatus =  "The file has been created at the import location, you are ready to import!" & vbCrLf & vbCrLf 
   If Len(OutputLines) <> 0 Then HALFile.QuickWrite OutputFileName,OutputLines 
Exit_Sub: 
End Sub 

Public Function GetRawEDICustomer(RawFile As String) As iImportFile 
   Dim retVal As iImportFile,FileContents As String,Customers(1) As iImportFile,i As Integer 
   FileContents = HALFile.QuickRead(RawFile) 
   Set Customers(0) = New TMS 
   Set Customers(1) = New GMService 
   'Set Customers(2) = New HondaLexicom
   'Set Customers(3) = New SubaruLexicom
   For i = 0 To 1 
      If InStr(FileContents,Customers(i).IdentifierString) <> 0 Then Set retVal = Customers(i) 
      If Not (retVal Is Nothing) Then Exit For 
   Next i 
   Set GetRawEDICustomer = retVal 
End Function 

Here is the iImportFile interface which is defined in each customer class using the keyword “Implements”:

Option Compare Database 
Option Explicit 

Public Function DefaultFileName() As String 
End Function 

Public Function ConvertLine(RawLine As String) As String 
End Function 

Public Function IdentifierString() As String 
End Function 

Public Function CustomerName() As String 
End Function 

Public Property Get LineSeparator() As String 
End Property 

In my email tomorrow we’ll take a look at some of the code that implements the interface and maybe at the library functions I’m using for the file dialogs.