'==========================================================================
'- LOOKUP GET RECORD FROM PIPE DELIMITED TEXT FILE
'- basic code for test purposes
'- NB. At present reads header line as a record. Saves code.
'- Not a problem because we are doing a lookup.
'- Brian Baulsom August 2007
'==========================================================================
Dim MyTextfile As String ' source file
Dim IDlookup As String ' lookup value
'--------------------------------------------------------------------------
'- Fields
Dim Site As String ' Field 1
Dim ID As String ' Field 2
Dim Items As String ' Field 3
'--------------------------------------------------------------------------
'- number of fields to extract (not necessarily the whole line)
Dim FieldCount As Integer
Dim Fieldx As Variant ' field data
'--------------------------------------------------------------------------
'- line of text (1 record)
Dim TextLine As String ' line of text
Dim Pipe As String
'==========================================================================
'- MAIN ROUTINE
'==========================================================================
Sub GET_RECORD()
'----------------------------------------------------------------------
'- initialise variables
MyTextfile = "C:\TEMP\TEST.TXT"
FieldCount = 3
ReDim Fieldx(FieldCount)
Pipe = "|"
'----------------------------------------------------------------------
'- Get lookup value
'- No error check yet. However, will not find invalid value
'- We do convert to upper case though
IDlookup = "AB" ' test value
IDlookup = UCase(InputBox("Please enter ID", " ID LOOKUP", IDlookup))
If IDlookup = "" Then Exit Sub
'----------------------------------------------------------------------
'- open file
Open MyTextfile For Input As #1
'- check each line
Do Until EOF(1)
Line Input #1, TextLine
'-----------------------------------------------------------------
'- run subroutine
GetFieldData
'-----------------------------------------------------------------
'- return values not strictly necessary. Could use Fieldx(1) .. etc
Site = Fieldx(1)
ID = Fieldx(2)
Items = Fieldx(3)
'-----------------------------------------------------------------
'- CHECK FOR MATCH
If ID = IDlookup Then
rsp = MsgBox("FOUND ID : " & IDlookup & vbCr _
& "Site : " & Site & vbCr _
& "Items : " & Items)
GoTo GetOut
End If
Loop
'----------------------------------------------------------------------
'- NO MATCH MESSAGE
MsgBox ("NO MATCH FOUND")
'----------------------------------------------------------------------
GetOut:
Close #1
End Sub
'============ END OF MAIN ROUTINE =========================================
'==========================================================================
'- SUBROUTINE TO PARSE THE TEXT LINE TO GET FIELDS.
'- Called from main routine
'==========================================================================
Private Sub GetFieldData()
Dim Ln, c, c1 ' character positions
Dim f ' field number
'----------------------------------------------------------------------
Ln = Len(TextLine)
f = 1
c1 = 1
'----------------------------------------------------------------------
'- character by character
For c = 1 To Ln
If Mid(TextLine, c, 1) = Pipe Then
Fieldx(f) = Trim(Mid(TextLine, c1, c - c1 - 1))
'- next field
c1 = c + 1
f = f + 1
End If
Next
'- last field
Fieldx(f) = Trim(Mid(TextLine, c1 + 1, Ln - c1))
End Sub
'--------------------------------------------------------------------------