Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

Implementing an interface can only be done from a class module. The class module uses the “Implements” keyword at the top and won’t compile unless you create all the appropriate functions from the interface. Yesterday I showed the code in the interface class module. Check out yesterday’s message in the archive: Text File Import / Export Recipe code part 2 (AppProcedures module) | Access JumpStart

This is the interesting part of the recipe because this is where I’m doing all the different translation of imported text lines to exported text lines. I can use the rest as a framework and don’t even hardly need to look at (with the exception of adding a line for a new class module to the list of supported class modules in the GetRawEDICustomer function in the helper module and increasing the loop number).

So let’s take a look at the TMS customer class.

The main function that does the bulk of the work is iImportFile_ConvertLine. This function takes a raw line as an argument and returns a string for the converted line.

The iImportFile Interface has a blank function called ConvertLine.

Because we are using the “Implements iImportFile” in this class, Access will require the interface_functionName format to be used to indicate the functions that match the interface. I have other emails in the archive I wrote on using interfaces, so get in and search for Interface to find them.

Option Compare Database 
Option Explicit 

Implements iImportFile 
Dim lastLine As String 
Const LineSeparator As String = vbCrLf 
Const FieldSeparator As String =  "*"

Private Function iImportFile_ConvertLine(RawLine As String) As String 
   Dim retVal As String,aFields As Variant 
   RemoveAsteriskFromEndOfLine RawLine 
   aFields = Split(RawLine,FieldSeparator) 
   If UBound(aFields) = -1 Then GoTo Exit_Function 
   Select Case aFields(0) 
      Case  "ISA", "ST", "GS", "CTT", "SE", "GE", "IEA"
         GoTo Unknown_Line 
      Case  "BSS"
         retVal = retVal & BuildLine( "//STX12//,862,TMS SERVICE",CStr(aFields(2)), "       ",CStr(aFields(2)), ",,,TO2") 
         retVal = retVal & BuildLineNoCrLf( "01",CStr(aFields(2)),CStr(aFields(3)),CStr(aFields(5)),CStr(aFields(6)),CStr(aFields(7)),CStr(aFields(11))) &  ","
      Case  "N1"
         Select Case aFields(1) 
            Case  "ST", "BY"
               retVal = retVal & BuildLineNoCrLf(CStr(aFields(4))) &  ","
            Case  "SU"
               retVal = retVal & BuildLine(CStr(aFields(4))) 
         End Select 
      Case  "LIN"
         retVal = retVal & BuildLineNoCrLf( "02",CStr(aFields(3)),CStr(aFields(5)),CStr(aFields(7)),CStr(aFields(9))) &  ","
      Case  "UIT"
         retVal = retVal & BuildLineNoCrLf(CStr(aFields(1))) &  ","
      Case  "REF"
         retVal = retVal & BuildLine(CStr(aFields(2))) 
      Case  "FST"
         retVal = retVal & BuildLine( "03",Format(CStr(aFields(1)), "+00000000"),CStr(aFields(4))) 
      Case  "TD5"
         retVal = retVal & BuildLine( "04",CStr(aFields(1)),CStr(aFields(4))) 
      Case Else 
         ' Do Nothing Eventually, for now just pass it through
         GoTo Unknown_Line 
   End Select 
Exit_Function: 
   lastLine = retVal 
   iImportFile_ConvertLine = retVal 
   Exit Function 
Unknown_Line: 
   'retVal = retVal & RawLine & vbCrLf
   GoTo Exit_Function 
End Function 

Private Function iImportFile_CustomerName() As String 
   iImportFile_CustomerName =  "TMS"
End Function 

Private Function iImportFile_DefaultFileName() As String 
   Dim retVal As String 
   retVal =  "C:\PROGRAM FILES (X86)\INOVIS\TRUSTEDLINK\MAPDATA\dx-xf-vv.080"
   iImportFile_DefaultFileName = retVal 
End Function 

Private Function iImportFile_IdentifierString() As String 
   Dim retVal As String 
   retVal =  "GS" & FieldSeparator &  "SS" & FieldSeparator &  "009595505" & FieldSeparator 
   iImportFile_IdentifierString = retVal 
End Function 

Private Function RemoveLastChar(str As String) As String 
   RemoveLastChar = Left(str,Len(str) - 1) 
End Function 

Private Sub RemoveAsteriskFromEndOfLine(ByRef RawLine As String) 
   If Len(RawLine) > 0 Then RawLine = RemoveLastChar(RawLine) 
End Sub 

Private Function BuildLine(ParamArray str() As Variant) As String 
   BuildLine = Join(str, ",") & vbCrLf 
End Function 

Private Function BuildLineNoCrLf(ParamArray str() As Variant) As String 
   Dim retVal As String 
   retVal = Join(str, ",") 
Exit_Function: 
   BuildLineNoCrLf = retVal 
End Function 

Private Property Get iImportFile_LineSeparator() As String 
    iImportFile_LineSeparator = LineSeparator 
End Property 

When I need other helper functions, I can call them whatever I want, no need to prepend anything with “iImportFile_”.

When I have a new file to import and translate for export, I can simply copy this class module and modify it with the translation code for the new file. I also want to updated the IdentifierString implemented function which the helper module uses to identify whether this is the correct module to use for converting the file.