Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Questions in a Text file

Status
Not open for further replies.

Bubbalouie

Technical User
Mar 25, 2009
107
US
I'm trying to help out the Human Resources people by creating a little app that reads some of the tests they give. I need some help with the logic cause my brain is hurting after 3 days of illogical thinking. I'm reading a text file and dumping the contents into a listbox and it does that fine. I found the text file read code on some website and am trying to hack it to my purposes.

I declared variables in Option Explicit:
Dim Fname$, ff, Raw$, Cloc, Tmp$
Dim Rep, QuestNum, CurrQuest, NextQuest, LastQuest As Integer

and in the form load set them as follows:
CurrQuest = 0
LastQuest = 0
NextQuest = 1

What I would like for it to do is load the text file line by line into the listbox till it hits Question 2: and stop so the user can look at the question and possible answers/explanation. Then click a Command button, clear the listbox and see the next question. Right now, it loads the whole file from start to finish without ever stopping and I just can't figure out how to make it stop, let alone display one at a time on the click of the command button. I'm stripping the number out of the 'Question X:' line and trying to use that to keep track of which one I'm on and to stop loading, but I'm stuck.

yeah, i'm a total vb noob but this stuff is kinda cool...

Code:
Private Sub Command2_Click()

List1.Clear

Fname$ = "C:\HR_Test1.txt"

ff = FreeFile
Open Fname$ For Binary As #ff
Raw$ = String$(LOF(ff), 0)
'Read the entire file in one operation and close it
Get #ff, 1, Raw$
Close #ff

ReDim Txt$(0)

Do
    Cloc = InStr(Raw$, vbCrLf)
    If Cloc > 0 Then  'Parse the data
        ReDim Preserve Txt$(0 To UBound(Txt$) + 1)
        Tmp$ = Left$(Raw$, Cloc - 1)
        Txt$(UBound(Txt$)) = Tmp$
    Else
        Exit Do  'All data has been parsed
    End If
    Raw$ = Right$(Raw$, Len(Raw$) - (Len(Tmp$) + 2))
    
    'on form load LastQuest and CurrQuest are set to 0, NextQuest is set to 1
    
    'get current question number by stripping out number 
    'and converting string to int
    If Left$(Tmp$, 8) = "QUESTION" Then 'And CurrQuest <> LastQuest Then
        CurrQuest = Trim(Tmp$)
        CurrQuest = Right(CurrQuest, 2)
        CurrQuest = Left(CurrQuest, 1)
        CurrQuest = Val(CurrQuest)
    End If
    
    'if current question number is >= than last question number
    'and < than the next question add line
    If Left$(Tmp$, 8) = "QUESTION" Then 'And (CurrQuest > LastQuest And CurrQuest <> NextQuest) Then ' And CurrQuest < NextQuest) Then
        List1.AddItem Tmp$
       
    'ElseIf Left$(Tmp$, 8) = "QUESTION" And (CurrQuest > LastQuest And CurrQuest < NextQuest) Then
        
    End If
    
    
    If Left$(Tmp$, 8) <> "QUESTION" Then 'And CurrQuest >= LastQuest Then
        
        List1.AddItem Tmp$
        LastQuest = CurrQuest
        NextQuest = CurrQuest + 1
    End If
      
Loop

End Sub

This won't load right (line feeds???) but the HR tests load perfect. Format of questions is

QUESTION 1:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id elit diam, et lobortis magna. Nunc a elit leo, a faucibus tellus. Mauris commodo congue

tempus. Fusce ac lorem metus. Nam aliquet est vitae purus ultrices eget blandit velit feugiat. In fringilla, nisi ut pulvinar aliquam, quam tortor molestie

ligula, a aliquet nibh velit egestas quam. In vitae nulla est. Integer lobortis mollis aliquam. Duis malesuada, ante vitae facilisis convallis, nisl neque

tincidunt metus, sollicitudin ultrices justo dui in quam. Nullam euismod lectus tempor nibh hendrerit sagittis. Quisque nec ante odio, ut tincidunt dui.

Pellentesque scelerisque pulvinar sem quis tincidunt. Nullam leo dolor, venenatis et molestie ac, adipiscing sed sem. Aenean eu turpis eget purus tristique

cursus non nec ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non neque sed ipsum malesuada iaculis.

A. ALorem ipsum dolor sit amet, consectetur adipiscing elit.
B. BLorem ipsum dolor sit amet, consectetur adipiscing elit.
C. CLorem ipsum dolor sit amet, consectetur adipiscing elit.
D. DLorem ipsum dolor sit amet, consectetur adipiscing elit.

Answer: D

QUESTION 2:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id elit diam, et lobortis magna. Nunc a elit leo, a faucibus tellus. Mauris commodo congue

tempus. Fusce ac lorem metus. Nam aliquet est vitae purus ultrices eget blandit velit feugiat. In fringilla, nisi ut pulvinar aliquam, quam tortor molestie

ligula, a aliquet nibh velit egestas quam. In vitae nulla est. Integer lobortis mollis aliquam. Duis malesuada, ante vitae facilisis convallis, nisl neque

tincidunt metus, sollicitudin ultrices justo dui in quam. Nullam euismod lectus tempor nibh hendrerit sagittis. Quisque nec ante odio, ut tincidunt dui.

Pellentesque scelerisque pulvinar sem quis tincidunt. Nullam leo dolor, venenatis et molestie ac, adipiscing sed sem. Aenean eu turpis eget purus tristique

cursus non nec ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non neque sed ipsum malesuada iaculis.

A. ALorem ipsum dolor sit amet, consectetur adipiscing elit.
B. BLorem ipsum dolor sit amet, consectetur adipiscing elit.
C. CLorem ipsum dolor sit amet, consectetur adipiscing elit.
D. DLorem ipsum dolor sit amet, consectetur adipiscing elit.
E. None of the above

Answer: A
Explanation
Pellentesque a nunc neque. Proin sagittis feugiat ipsum sit amet fermentum. Pellentesque ut neque augue. Morbi eget sem libero. Sed convallis ullamcorper

lectus, posuere blandit risus dapibus id. Phasellus semper adipiscing sem a vestibulum. Quisque pulvinar ornare facilisis. Vivamus imperdiet felis at risus

gravida vehicula. Nullam lacinia laoreet pharetra. Nulla.

QUESTION 3:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id elit diam, et lobortis magna. Nunc a elit leo, a faucibus tellus. Mauris commodo congue

tempus. Fusce ac lorem metus. Nam aliquet est vitae purus ultrices eget blandit velit feugiat. In fringilla, nisi ut pulvinar aliquam, quam tortor molestie

ligula, a aliquet nibh velit egestas quam. In vitae nulla est. Integer lobortis mollis aliquam. Duis malesuada, ante vitae facilisis convallis, nisl neque

tincidunt metus, sollicitudin ultrices justo dui in quam. Nullam euismod lectus tempor nibh hendrerit sagittis. Quisque nec ante odio, ut tincidunt dui.

Pellentesque scelerisque pulvinar sem quis tincidunt. Nullam leo dolor, venenatis et molestie ac, adipiscing sed sem. Aenean eu turpis eget purus tristique

cursus non nec ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non neque sed ipsum malesuada iaculis.

A. ALorem ipsum dolor sit amet, consectetur adipiscing elit.
B. BLorem ipsum dolor sit amet, consectetur adipiscing elit.
C. CLorem ipsum dolor sit amet, consectetur adipiscing elit.
D. DLorem ipsum dolor sit amet, consectetur adipiscing elit.

Answer: D

QUESTION 4:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id elit diam, et lobortis magna. Nunc a elit leo, a faucibus tellus. Mauris commodo congue

tempus. Fusce ac lorem metus. Nam aliquet est vitae purus ultrices eget blandit velit feugiat. In fringilla, nisi ut pulvinar aliquam, quam tortor molestie

ligula, a aliquet nibh velit egestas quam. In vitae nulla est. Integer lobortis mollis aliquam. Duis malesuada, ante vitae facilisis convallis, nisl neque

tincidunt metus, sollicitudin ultrices justo dui in quam. Nullam euismod lectus tempor nibh hendrerit sagittis. Quisque nec ante odio, ut tincidunt dui.

Pellentesque scelerisque pulvinar sem quis tincidunt. Nullam leo dolor, venenatis et molestie ac, adipiscing sed sem. Aenean eu turpis eget purus tristique

cursus non nec ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non neque sed ipsum malesuada iaculis.

A. ALorem ipsum dolor sit amet, consectetur adipiscing elit.
B. BLorem ipsum dolor sit amet, consectetur adipiscing elit.
C. CLorem ipsum dolor sit amet, consectetur adipiscing elit.
D. DLorem ipsum dolor sit amet, consectetur adipiscing elit.
E. None of the above

Answer: A
Explanation
Pellentesque a nunc neque. Proin sagittis feugiat ipsum sit amet fermentum. Pellentesque ut neque augue. Morbi eget sem libero. Sed convallis ullamcorper

lectus, posuere blandit risus dapibus id. Phasellus semper adipiscing sem a vestibulum. Quisque pulvinar ornare facilisis. Vivamus imperdiet felis at risus

gravida vehicula. Nullam lacinia laoreet pharetra. Nulla.



Thanks In Advance for ANY Ideas!
 
You might find it much easier if you actually read the file one line at a time rather than all at once.

So, something like this will go through the file from start to finish:

Code:
Fname$ = "C:\HR_Test1.txt"

ff = FreeFile
Open Fname$ For Input As #ff
Do While Not EOF(ff)
  Line Input #ff, Raw$
  If (Left$(Raw$, 8) = "QUESTION") Then
    N$ = Trim$(Mid$(Raw$, 9))
    CurrQuest = Val(N$)
    MsgBox "Click OK to continue"
   Else
    List1.AddItem Raw$ 
  End If
Loop
Close #ff

When it reaches a question it'll read the number into CurrQuest and then stop until the user clicks OK. The problem with your code might have been because you define CurrQuest as 0 VB will assume it's always going to be a number but then you try to slice it up like a string. Anyway, try the code above and you might find it easier to work with.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 

You may not know this but if you:
I declared variables in Option Explicit:
Dim Fname$, [red]ff[/red], Raw$, [red]Cloc[/red], Tmp$
Dim [red]Rep, QuestNum, CurrQuest, NextQuest[/red], LastQuest As Integer
Your variables in RED are all Variants, and not Strings (in first line) or Integers (second line) as you hope.

[tt]Dim Fname$[/tt] is the same as [tt]Dim Fname As String[/tt]

But:
Dim Rep, QuestNum, CurrQuest, NextQuest, LastQuest As Integer

makes just LastQuest an Integer, the rest of them as Variants.

Consider:[tt]
Dim Rep As Integer
Dim QuestNum As Integer
Dim CurrQuest As Integer
Dim NextQuest As Integer
Dim LastQuest As Integer[/tt]

or:[tt]
Dim Rep As Integer, QuestNum As Integer, CurrQuest As Integer, NextQuest As Integer, LastQuest As Integer[/tt]

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top