Re: Listbox to delimited text file



Greg from Kentucky wrote:
I am mostly an amateur with VB6 but I would like some advice about two problems. I have run out of help materials and have no other choice but to ask for help.

First: I have several single-line textboxes, a Combo Box, and some calculated labels that the user enters his data into, I send the contents of each to a Listbox with a CommandButton like this:

lst1.AddItem Format(Out, txtOut.text) & vbTab & Format(Vehicle, comboVehicle) & vbTab & Format(Number, txtNumber.text) & vbTab & Format(txtSerial.text, Serial) . . .

This works fine and is a perfect preview for the user. My wish is to export all of this data into a plain text file and change the tab spaces (Listbox columns) into semicolons, leaving the rows intact and putting a carriage return/break to represent each row. I need it in semicolon delimited format so it can be imported by another program that I have no control over.


like this?

For i = 0 To lst1.ListCount - 1
s = s & Replace(lst1.List(i), vbTab, ";") & vbCrLf
Next

something pretty similar to that should get everything you want into a string.



The CommandButton resets all the TextBoxes, ComboBox, and Labels at the same time it populates the Listbox and moves the cursor back to the first Textbox for the user to enter a new record.

I am not partial to the Listbox - I only used it because I knew how and it gives immediate feedback to the user that his entry was made correctly and gives him an option to remove the entire line/row if it is wrong.

I am open to using a MSFlexGrid, ListBox, RichTextBox, or even a DataBase of some sort, I just don't know how. I am open to all suggestions!

Second: I would like some advice on doing the actual writing and saving of the Text file, assuming someone out there can help me with the first part.

I'm partial to wrapping text file stuff into all-at-once procedures:


Public Function PutFile(sFn As String, sData As String) As Long
'write file from string
'don't forget - this just writes the first so many bytes -
' if file already existed and was longer, other bytes will remain.
Dim fh As Integer 'file I/O handle
fh = FreeFile
Open sFn For Binary Access Write Lock Read Write As fh
Put #fh, , sData
Close #fh
PutFile = Len(sData)
End Function


Public Function GetFile(sFn As String) As String
'read entire file into a string
Dim fh As Integer 'file I/O handle
fh = FreeFile
Open sFn For Input Access Read Lock Write As fh
GetFile = Input$(LOF(fh), fh)
Close #fh
End Function


I find that until file sizes get well up into the dozens of megabytes,
all-at-once outperforms line-by-line I/O pretty dramatically.




Bob
--
.