By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

Creating database records from a Text File

P: 4
I have a program that I download a text file that has multiple lines in the following style:

This is the title.

Verse 1
This is the first line
This is the second line

Verse 2
This is the first line
This is the second line

I can parse the text file to some degree but what I really need is to be able to read the first line and add a new record and then read the next line and know if the next line is Verse 1 or Verse 2 and then add the next lines to the record. I am not sure how to loop through correctly.

Thank you for your time and information.


Sincerely,

Keith Kelly
Sep 5 '07 #1
Share this Question
Share on Google+
2 Replies


VBPhilly
P: 95
I have a program that I download a text file that has multiple lines in the following style:

This is the title.

Verse 1
This is the first line
This is the second line

Verse 2
This is the first line
This is the second line

I can parse the text file to some degree but what I really need is to be able to read the first line and add a new record and then read the next line and know if the next line is Verse 1 or Verse 2 and then add the next lines to the record. I am not sure how to loop through correctly.

Thank you for your time and information.


Sincerely,

Keith Kelly
Looks like you have a predictable format such as:

Verse1
Line1
Line2

You'll need to loop the file, as normal, and simply look for "Verse". This is your record begin flag.
Then, you use counting to get the rest of the data. Line1, Line2.

Then, do the same thing until EOF.

Ide help more if I had a data sample. Show us sample data.
Sep 5 '07 #2

P: 4
Thanks for replying.

Here is the code that I am using. It is rough. Here is the actual text file lines.

I have a large text box that I first pasre the text file into to look at. Then I have a command button that actually parses the textbox.

command button code to parse the text file into the textbox

Dim nfilenum As Integer
Dim llinecount As Integer
Dim snextline As String
Dim stext As String

nfilenum = FreeFile
CommonDialog1.InitDir = "g:\vb projects\icasttest\songs\"
CommonDialog1.Filter = "*.txt"
CommonDialog1.ShowOpen
gPath = CommonDialog1.FileName

Open gPath For Input As nfilenum
' Read the contents of the file
Do While Not EOF(nfilenum)
Line Input #nfilenum, snextline
snextline = snextline & vbCrLf
stext = stext & snextline
Loop
Text1.Text = stext
close nfilenum


command button code to parse the textbox and create the database records

Dim pos As Integer
Dim entry() As String

entry = Split(Text1.Text, vbCrLf & vbCrLf)
pos = 0

Do While pos < UBound(entry)
If Trim$(entry(pos)) <> "" Then
database.addnew
database.recordset!field = trim$entry(pos))
End If
pos = pos + 1
Loop



The problem I can't get over is once I find the "Verse 1" header I do not know how to continue adding to the database until it gets to the next vbcrlf.

Here is the actual text file:

Here I Am To Worship


Verse 1
Light of the world
You stepped down into darkness
Opened my eyes let me see
Beauty that made
This heart adore You
Hope of a life spent with You


Chorus 1
So here I am to worship
Here I am to bow down
Here I am to say that You're my God
And You're altogether lovely
Altogether worthy
Altogether wonderful to me


Verse 2
King of all days
Oh so highly exalted
Glorious in heaven above
Humbly You came
To the earth You created
All for love's sake became poor


Misc 1
(BRIDGE)
And I'll never know how much it cost
To see my sin upon that cross
And I'll never know how much it cost
To see my sin upon that cross



Thank you very much.

Sincerely,

Keith Kelly
Sep 6 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.