Results 1 to 5 of 5
  1. #1
    anishkgt is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    10

    Import Text file to access database

    Hi All,

    i am totally new to VBA coding. I've only used macro for my work with access2013. Now i have a requirement for importing a text file into the accessdb.



    I've managed to read the text file (with some help for the coding) and display it on a form. The text is not clearly formated(attached). I wish to automate the import process from a folder C:\MIR\ into a table.
    Code:
        Option Compare Database
        
        Private Sub cmd1_Click()
            
            Dim TextLine As String
            Dim MIR As String
            Dim IATACode As String
            Dim MIRDate As String
            Dim IssueAirline As String
            Dim ALC As String
            Dim AL2C As String
            Dim DateOfTravel As String
            Dim InputGTID As String
            Dim OutputGTID As String
            Dim BkgTkgPCC As String
            Dim IATAnumber As String
            Dim PNR As String
            Dim BkgSignOnTkgSignOn As String
            Dim PNRDate As String
            Dim Passenger As String
            Dim pax As String
            Dim AirSector As String
            Dim sectors As String
            Dim BaseFare As String
            Dim BFC As String
            Dim TotalFare As String
            Dim ActualAMTCol As String
            Dim Tax As String
            Dim Tax1 As String
            Dim PhoneField As String
            Dim FOP As String
            
        If IsNull(txtMIRPath) = True Then
        MsgBox ("Check MIR file path")
        Exit Sub
        End If
        MIR = [Forms]![form1]![txtMIRPath] & ".MIR"
            DoCmd.Hourglass True
            Open MIR For Input As #1 'Open specified file
            Do While Not EOF(1)
                Line Input #1, TextLine
                
                If Left(TextLine, 2) = "T5" Then
                    
                    IATACode = Trim(Mid(TextLine, 5, 4)) 'Reads the IATA Code
                    MIRDate = Trim(Mid(TextLine, 21, 7)) 'Reads MIR Creation date and time
                    IssueAirline = Trim(Mid(TextLine, 38, 24)) 'Reads the Issuing AirLine
                    ALC = Trim(Mid(TextLine, 35, 3)) 'Reads Airline Code
                    AL2C = Trim(Mid(TextLine, 33, 2)) 'Reads Airline two letter code
                    DateOfTravel = Trim(Mid(TextLine, 62, 7)) 'Reads Date of travel
                    InputGTID = Trim(Mid(TextLine, 69, 6)) 'Reads InputGTID
                    OutputGTID = Trim(Mid(TextLine, 75, 6)) 'Reads Output GTID
                    
                    txtIATA.Value = IATACode
                    txtMIRDate.Value = MIRDate
                    txtIssueAirline.Value = ALC & " " & AL2C & " " & IssueAirline
                    txtDateOfTravel.Value = DateOfTravel
                    txtIOGTID.Value = InputGTID & " " & OutputGTID
                    
                    Line Input #1, TextLine 'Start reading next line
                    BkgTkgPCC = Trim(Mid(TextLine, 1, 4)) & " " & Trim(Mid(TextLine, 5, 4)) 'Booking and ticketing PCC respectively
                    IATAnumber = Trim(Mid(TextLine, 9, 7)) 'Reads IATA number assinged by BSP
                    PNR = Trim(Mid(TextLine, 18, 15)) 'Reads PNR
                    BkgSignOnTkgSignOn = Trim(Mid(TextLine, 33, 6)) & " " & _
                    Trim(Mid(TextLine, 39, 1)) & " " & _
                    Trim(Mid(TextLine, 40, 2)) & " " & _
                    Trim(Mid(TextLine, 42, 2)) 'Reads Booking and Ticketing SignOn
                    PNRDate = Trim(Mid(TextLine, 44, 7)) 'Reads PNR creation data
                    txtBookingPCC.Value = BkgTkgPCC
                    txtIATANumber.Value = IATAnumber
                    txtPNR.Value = PNR
                    txtBookingSignOn.Value = BkgSignOnTkgSignOn
                    txtPNRDate.Value = PNRDate
                End If
                
                If Left(TextLine, 3) = Trim("A02") Then 'First A02(PAX) line
                    pax = Trim(Mid(TextLine, 4, 30))
                    TKT = ALC & Trim(Mid(TextLine, 49, 10))
                    Passenger = TKT & " " & pax
                    Line Input #1, TextLine
                    Line Input #1, TextLine
                    
                    Do While Left(TextLine, 3) = ("A02") 'Loops through following A02 (Pax) lines (if any)
                        pax = Trim(Mid(TextLine, 4, 30))
                        TKT = ALC & Trim(Mid(TextLine, 49, 10))
                        Passenger = Passenger & vbNewLine & TKT & " " & pax
                        Line Input #1, TextLine
                        Line Input #1, TextLine
                    Loop
                End If
                txtTKT.Value = Passenger
                
                If Left(TextLine, 3) = "A04" Then  'Reads First A04(Secotor) Line
                    sectors = Trim(Mid(TextLine, 50, 13)) & "-" & Trim(Mid(TextLine, 66, 13))
                    AirSector = sectors
                    Line Input #1, TextLine
                    
                    Do While Left(TextLine, 3) = "A04" 'Loops through following A04(Sectors) lines (if any)
                        sectors = Trim(Mid(TextLine, 50, 13)) & "-" & Trim(Mid(TextLine, 66, 13))
                        AirSector = AirSector & vbNewLine & sectors
                        Line Input #1, TextLine
                    Loop
                End If
                txtSector.Value = AirSector
                
                If Left(TextLine, 3) = "A07" Then 'Reads base fare, Total fare and 5 Tax's
                        BaseFare = Trim(Mid(TextLine, 9, 12))
                        BFC = Trim(Mid(TextLine, 6, 3)) & ": "
                        EQAmt = Trim(Mid(TextLine, 39, 12))
                        TotalFare = Trim(Mid(TextLine, 24, 12))
                        Tax = Trim(Mid(TextLine, 57, 8)) & "+" & Trim(Mid(TextLine, 70, 8)) & "+" & Trim(Mid(TextLine, 83, 8)) & "+" & Trim(Mid(TextLine, 96, 8)) & "+" & Trim(Mid(TextLine, 109, 8))
                End If
                txtBaseFare.Value = BFC & BaseFare & "/-"
                txtEqamt.Value = "QAR: " & EQAmt & "/-"
                txtTax1.Value = "QAR: " & Tax & "/-"
                txtTotalFare.Value = "QAR: " & TotalFare & "/-"
                
                If Left(TextLine, 3) = "A11" Then 'Reads Form of payment
                    FOP = Trim(Mid(TextLine, 4, 2))
                    ActualAMTCol = Trim(Mid(TextLine, 6, 12))
                    txtFOP.Value = FOP
                    txtActualAmt.Value = "QAR: " & ActualAMTCol
                    Line Input #1, TextLine
                End If
                txtFOP.Value = FOP
                
                 If Left(TextLine, 3) = "A12" Then 'Reads Phone field
                    PhoneField = Trim(Mid(TextLine, 10, 50))
                    phoneFieldCont = PhoneField
                    Line Input #1, TextLine
    
                    Do While Left(TextLine, 3) = "A12" 'Reads (any) following Phone fields
                        PhoneField = Trim(Mid(TextLine, 10, 50))
                        Line Input #1, TextLine
                        phoneFieldCont = phoneFieldCont & vbNewLine & PhoneField
                    Loop
                    txtPhoneField.Value = phoneFieldCont
                End If
            Loop
            DoCmd.Hourglass False
            Close #1   ' Close file
        End Sub
    what would be the simplest way to import these fields into a table
    Attached Files Attached Files

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Manually import the text file, in the 'import screen' ,bottom left corner is ADVANCED
    define your text as an IMPORT SPEC.
    SAVE this spec.

    in the import button onClick event
    the event would be:

    DoCmd.TransferText acImportDelim, SPECname, Table, vFile, True

  3. #3
    anishkgt is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    10
    Sorry runman ! idid not understand what you meant. Which import screen are u referring to ?

  4. #4
    anishkgt is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    10
    ok got it but it does not allow me skip characters, i believe it has to be continuous letters. any idea how i can go with this.

  5. #5
    anishkgt is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    10
    Hi Ranman,

    i do not want to read the file again, i've already done that with the code mentioned above. I just need to import those fields into the table attached.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 10-08-2014, 11:05 AM
  2. Replies: 5
    Last Post: 02-12-2014, 12:13 PM
  3. Replies: 12
    Last Post: 04-30-2013, 02:41 AM
  4. Access 2007 import fix width ANSI text file
    By jsama in forum Import/Export Data
    Replies: 0
    Last Post: 03-05-2013, 09:07 PM
  5. Automaically Import Text File in Access Form
    By ract123 in forum Import/Export Data
    Replies: 1
    Last Post: 06-13-2011, 09:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums