Fixed Format by Starting Characters

JDeezy

Registered User.
Local time
Today, 05:31
Joined
Sep 4, 2009
Messages
54
Ok, I have this file that came through that is, I think you would call it free fixed width format. It has fixed width fields but if a field is missing it shifts it over to the left. But all the fields have characters at the front to identify them for example AC120.

If anybody has any ideas on how to program a file like this your help would be greatly appreciated. Thanks in advance.
 
How about posting an example of your data to better see what you are dealing with?
 
Well, I dont really have an example, just a layout. But basically say its has Field 1- Name, Field 2-Address, Field 3- Shipping address, Field 4-Bankruptcy info, and Field 5- Connection status. And each one of these fields have identifying characters at the start of the field such as: Field 1- AC120, Field 2- AC130, Field 3- BC120, Field 4- BC130, and Field 5- AF120. And all of these fields are 20 characters long. Now if any of these fields are not available, it then shifts everything to the left. Example: AC120(20 characters) AC130(20 characters) AF120(20 characters). I need to put each field in the correct column in a table but a simple file spec wont do it. I hope that helps :-)
 
So each field is actually 25 characters if you include the 5 ID code characters right? Do you have the complete spec's so we can create the needed table and then the code to read the input file and place the values in the correct field?
 
Yes, 25 characters in all. I really just need an example of coding and I can figure out the rest. It doesnt seem that difficult but I just dont know how to get started.
 
Great! I'll see if I can whip up some sample code to get you started.
 
This is strictly AIR CODE and not tested but hopefully you get the idea.
Code:
Option Compare Database
Option Explicit

Dim TextLine As String
Dim FieldCount As Integer

Private Sub SpecialImport_Click()

   Dim db As DAO.Database
   Dim rs As DAO.Recordset
   Dim InFile As String
   Dim Counter As Integer

   '-- I'll leave it to you to point to the correct input file in InFile. I normally use this API...
   '-- ...http://www.mvps.org/access/api/api0001.htm
   'InFile = the correct file to import

   Open InFile For Input As #1   ' Open the import file.
   Set db = CurrectDB
   Set rs = db.OpenRecordset("TheTableToFillWithTheImportData")

   With rs
      While Not EOF(1)
         If GetRecord Then    '-- Another record available
            .AddNew           '-- Start a new record in the import table
            For Counter = 1 To FieldCount
               Select Case left(GetField(Counter), 5)    '-- Walk through the fields in the input
                  Case "AC120"
                     '-- Name Field
                     !NameField = Mid(GetField(Counter), 6)
                  Case "AC130"
                     '-- Address field
                     !AddressField = Mid(GetField(Counter), 6)
                  Case "BC120"
                     '-- Shipping Address
                     !ShipField = Mid(GetField(Counter), 6)
                  Case "BC130"
                     '-- Bankruptcy Info
                     !BankField = Mid(GetField(Counter), 6)
                  Case "AF120"
                     '-- Connection Status
                     !ConnField = Mid(GetField(Counter), 6)
                  Case Else
                     '-- Unknown field identity
               End Select
            Next Counter      '-- Loop for the next field
            .Update           '-- Fields done, save the record
         End If
      Wend
      .Close
   End With
   Set rs = Nothing
   Close #1      ' Close file.
   MsgBox "Import complete!"
End Sub

Function GetField(FieldNumber As Integer) As String
'-- Return the field in the TextLine buffer pointed to by FieldNumber
'-- FieldNumber is guaranteed to be a valid value.
   GetField = Mid(TextLine, FieldNumber * 25, 25)

End Function

Function GetRecord() As Boolean
   If Not EOF(1) Then
      Line Input #1, TextLine '-- Read the next line into variable.
      GetRecord = True        '-- Indicate record available
      FieldCount = Len(TextLine) \ 25     '-- Integer division
   Else
      GetRecord = False       '-- Indicate no more records
   End If

End Function
 
For some reason, I cant reference the DAO library. I cant even click the reference button on the tools menu. I hate work computers you cant upgrade. Any ideas with that aspect?
 
Maybe I should have also added, I am being FORCED to use access 2002.
 
For some reason, I cant reference the DAO library. I cant even click the reference button on the tools menu. I hate work computers you cant upgrade. Any ideas with that aspect?
You can not access Tools>References while the debugger is running.
 
Ahaha I see. Still kind of new to VB. Starting to figure stuff out slowly but surely.
 
Great! Just post back when you get stuck on this process.
 
I tried to manipulate your code but I still cant get it to work. Would it be ok if I sent you some file specs and sample file through e-mail?
 
I don't think you would succeed in getting Allan's e-mail address to send your db to ;)

It would be useful if you explained exactly what part of the code you don't understand and what line is being highlighted as having an error in the VBA editor.
 
Actually I PM'd him with my addy but I'm headed out the door right now so I won't be able to look at it for a while.
 
seriously

where does the text file come from?

the easiest solution is to get an extra five chars added to the start of the blanks

even just ????? if you havent got the true code - then you wont have any complex stuff to do in access.

even load it into excel, and do it there.
 

Users who are viewing this thread

Back
Top Bottom