Function GetVarFromAdventExp(filename As String, var As String, pad As Integer) As String

DoEvents

Dim temp as String, adjvar As String, record As String, VarNotFound As Integer, ff As Integer, TypeOfRecord As Integer, labellocation As Integer, checkfields As Integer
adjvar = var

Select Case LCase$(Right$(filename, 3))

Case "grp"
  checkfields = 6
  labellocation = 201

Case "cpg"
  checkfields = 8
  labellocation = 218

Case "cli"
  checkfields = 53
  labellocation = 420

Case "dex"
  checkfields = 53
  labellocation = 420

Case "pbf"
  checkfields = 72
  labellocation = 420

Case "prf"
  checkfields = 72
  labellocation = 420

Case Else
  labellocation = 420

End Select

temp = ""

If Exists(filename) Then
  VarNotFound = True
  ff = FreeFile
  TypeOfRecord = 36 ' var

  openwithtrap filename, "Input", ff
    While Not EOF(ff) And TypeOfRecord < 38 And VarNotFound

      Line Input #ff, record

      If Left$(record, 1) = "," And Right$(var, 1) = " " Then adjvar = Left(var, Len(var) - 1) + ","
      If Len(record) >= labellocation Then
        TypeOfRecord = Asc(Mid$(record, labellocation, 1))
      End If

      If InStr(record, (adjvar)) <> 0 Then

        VarNotFound = False

      End If

    Wend

    If VarNotFound Then

      temp = ""

    Else

      If Len(record) < labellocation Then

        If Left$(var, 1) = "$" Then temp = RTrim$(LTrim$(Mid$(record, InStr(record, adjvar) + Len(adjvar) + 2, Len(Right(record, Len(record) - (InStr(record, adjvar) + Len(adjvar)))))))
        If Left$(var, 1) = "%" Then temp = RTrim$(LTrim$(Mid$(record, InStr(record, adjvar) + Len(adjvar) + 1, Len(Right(record, Len(record) - (InStr(record, adjvar) + Len(adjvar)))))))
        If Left$(var, 1) = "#" Then temp = RTrim$(LTrim$(Mid$(record, InStr(record, adjvar) + Len(adjvar), Len(Right(record, Len(record) - (InStr(record, adjvar) + Len(adjvar)))))))

      Else

        temp = RTrim$(LTrim$(Mid$(record, labellocation + 23, Len(record$) - (labellocation + 24))))

      End If

   End If

   If Len(temp) < pad Then temp = temp + Space$(pad - Len(temp))

 Close #ff

End If

GetVarFromAdventExp = RemoveQuotesAndTrailingCommas(temp)

End Function