home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / misc / samples2 / parse.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-07-24  |  8.0 KB  |  225 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   5820
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1770
  7.    ClientWidth     =   7365
  8.    Height          =   6510
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   5820
  12.    ScaleWidth      =   7365
  13.    Top             =   1140
  14.    Width           =   7485
  15.    Begin CommandButton Command2 
  16.       Caption         =   "Create File"
  17.       Height          =   550
  18.       Left            =   615
  19.       TabIndex        =   5
  20.       Top             =   5000
  21.       Width           =   1600
  22.    End
  23.    Begin CommandButton Command1 
  24.       Caption         =   "Parse File"
  25.       Height          =   550
  26.       Left            =   4545
  27.       TabIndex        =   4
  28.       Top             =   5000
  29.       Width           =   1600
  30.    End
  31.    Begin TextBox Text2 
  32.       Height          =   3555
  33.       Left            =   4110
  34.       MultiLine       =   -1  'True
  35.       TabIndex        =   2
  36.       Top             =   1185
  37.       Width           =   3120
  38.    End
  39.    Begin TextBox Text1 
  40.       Height          =   3555
  41.       Left            =   120
  42.       MultiLine       =   -1  'True
  43.       TabIndex        =   0
  44.       Top             =   1185
  45.       Width           =   3840
  46.    End
  47.    Begin Label Label2 
  48.       Caption         =   "Formatted:"
  49.       Height          =   240
  50.       Left            =   4095
  51.       TabIndex        =   3
  52.       Top             =   945
  53.       Width           =   1665
  54.    End
  55.    Begin Label Label1 
  56.       Caption         =   "Unformatted:"
  57.       Height          =   255
  58.       Left            =   135
  59.       TabIndex        =   1
  60.       Top             =   885
  61.       Width           =   1605
  62.    End
  63.    Begin Menu mnuExit 
  64.       Caption         =   "Exit"
  65.    End
  66. 'Dimension an array to hold each record
  67. 'read from the data file.
  68. 'You might prefer to use a dynamic area, rather
  69. 'than explicitly stating the size of the array.
  70. Dim RecordItem(10) As String
  71. 'Declare a variable to hold the data file's name
  72. Dim FileName As String
  73. 'Declare a variable to indicate a carriage return/line feed
  74. Dim CRLF As String
  75. 'By dimensioning variables here, they are available to all
  76. 'procedures within the form.
  77. Sub Command1_Click ()
  78.     'Now that the file is created, read the contents of it into an array
  79.     ReadDataFile
  80.     'CRLFs have now been substituted for each comma in the string, with the
  81.     'exception of the first comma.  Assign each record to Text2.
  82.     For I = 1 To 10
  83.         Text2 = Text2 + RecordItem(I)
  84.     Next I
  85. End Sub
  86. Sub Command2_Click ()
  87.     'First, we need to create a data file
  88.     'Comment out the kind of file that you DON'T want to create
  89.     CreateDataFile  'creates a data file WITHOUT quotation marks
  90.     'CreateDataFileWithQuotes    'creates file WITH quotation marks
  91.     'Now, enable the command button
  92.     Command1.Enabled = True
  93. End Sub
  94. Sub CopyToArray (LineToFormat As String)
  95.     'This routine does the actual formatting of the string and then
  96.     'copies it to the array.  Basically all that is happening, is
  97.     'a carriage return/line feed is being substituted for each comma
  98.     'after the first comma is found.  You can use this same procedure
  99.     'for substituting any character for any other character simply by
  100.     'modifying the line containing INSTR to find the character that you
  101.     'want to change.  Notice the similarity between finding the quotation
  102.     'mark and finding a comma.
  103.     'Dimension a variable for the array's index and preserve its
  104.     'value between calls
  105.     Static Index As Integer
  106.     'Increment the value stored in Index
  107.     Index = Index + 1
  108.     'Dim a variable to hold the postion of each comma in the string
  109.     Dim CommaPos As Integer
  110.     'Dim a variable to hold the postion of each quotation mark in the string
  111.     Dim QuotePos As Integer
  112.     'Dim another variable to use as a flag for the first comma
  113.     'This flag is initially false, after the first comma is found,
  114.     'it is changed to true, meaning that we have already found the
  115.     'first comma in the string
  116.     Dim FirstComma As Integer
  117.     'Begin loop that will parse through the string one character at a time,
  118.     'searching for both commas and quotation marks
  119.     For I = 1 To Len(LineToFormat)
  120.         'Get the next character in the string
  121.         char$ = Mid$(LineToFormat, I, 1)
  122.         Temp$ = Temp$ + char$
  123.         'First we'll test for quotation marks
  124.         '34 is the ascii value for a quotation mark
  125.         'Since all quotation marks are being removed,
  126.         'we need to always start searching from the 1st position
  127.         QuotePos = InStr(1, Temp$, Chr$(34))
  128.         If QuotePos Then
  129.             'Simply remove the last character from the string
  130.             Temp$ = Left$(Temp$, QuotePos - 1)
  131.         End If
  132.         
  133.         'Now test for a comma.  This is just a bit more complicated
  134.         'because we need to skip over the first comma that is found
  135.         '44 is the ascii value for a comma.  Because we may have removed
  136.         'a quotation mark, we must start the search at the last
  137.         'postion in the string
  138.         CommaPos = InStr(Len(Temp$), Temp$, Chr$(44))
  139.         'Change FirstComma to true only if a comma has been
  140.         'found and the flag is false, meaning this is the first
  141.         'time we have encountered a comma
  142.         If CommaPos And FirstComma = False Then
  143.             FirstComma = True
  144.         ElseIf CommaPos And FirstComma = True Then
  145.             'Substitute a CRLF for the last character in the string
  146.             Temp$ = Left$(Temp$, CommaPos - 1) + CRLF
  147.         End If
  148.     Next I
  149.     'Assign the temp string to the array and append 2 more CRLFs
  150.     RecordItem(Index) = Temp$ + CRLF + CRLF
  151. End Sub
  152. Sub CreateDataFile ()
  153.     'Creates a data file which does NOT contain quotation marks.
  154.     'The commas need to be a part of the string that is written.
  155.     Item1$ = "Doe, John,555-1435,Thrillseeker"
  156.     Item2$ = "Doe, Jane,555-7899,couch potato"
  157.     FileNum = FreeFile
  158.     FileName = App.Path + "\datafile.dat"
  159.     Open FileName For Output As FileNum
  160.     Print #FileNum, Item1$
  161.     Print #FileNum, Item2$
  162.     Close FileNum
  163. End Sub
  164. Sub CreateDataFileWithQuotes ()
  165.     'Creates a data file which DOES contain quotation marks.
  166.     'The commas need to be a part of the string that is written.
  167.     Item1a$ = "Doe, John"
  168.     Item1b$ = "555 - 1435"
  169.     Item1c$ = "Thrillseeker"
  170.     Item2a$ = "Doe, Jane"
  171.     Item2b$ = "555-7899"
  172.     Item2c$ = "couch potato"
  173.     FileNum = FreeFile
  174.     FileName = App.Path + "\datafile.dat"
  175.     Open FileName For Output As FileNum
  176.     Write #FileNum, Item1a$, Item1b$, Item1c$
  177.     Write #FileNum, Item2a$, Item2b$, Item2c$
  178.     Close FileNum
  179. End Sub
  180. Sub Form_Load ()
  181. '******************************************************************
  182. 'Original message
  183. 'Example
  184. 'CSV Format
  185. '"Doe, John","555-1435","Thrillseeker"
  186. '"Doe, Jane","555-7899","couch potato"
  187. 'convert to
  188. 'Doe, John
  189. '555-1435
  190. 'Thrillseeker
  191. 'Doe, Jane
  192. '555-7899
  193. 'Couchpotato
  194. '******************************************************************
  195.     'Define a carriage return/line feed
  196.     CRLF = Chr$(13) + Chr$(10)
  197.     'Disable the command button until after the file
  198.     'has been created.
  199.     Command1.Enabled = False
  200. End Sub
  201. Sub Form_Unload (Cancel As Integer)
  202.     End
  203. End Sub
  204. Sub mnuExit_Click ()
  205.     Unload Me
  206. End Sub
  207. Sub ReadDataFile ()
  208.     'Reads each line of the data file and places it into
  209.     'an array named RecordItem.
  210.     FileNum = FreeFile
  211.     Open FileName For Input As FileNum
  212.     I = 1
  213.     Do Until EOF(FileNum)
  214.         Line Input #FileNum, A$
  215.         'Write each record, as it appears in the data file
  216.         'to the first text box
  217.         Text1 = Text1 + A$ + CRLF
  218.         'Now call the procedure to format the string and
  219.         'copy it to the array, passing the string to the procedure
  220.         CopyToArray A$
  221.         I = I + 1
  222.     Loop
  223.     Close FileNum
  224. End Sub
  225.