Solved

Reading randomly from text files and displaying it.

Posted on 2002-06-18
6
133 Views
Last Modified: 2010-05-02
I have a text file in which there are 5 lines. abc.txt

Question 1
Question 2
Question 3
Question 4
Question 5

I would like my VB program to read randomly from this text file and display the question on the VB form one by one. On the click of button or click of left key of mouse next question is shown.Please tell me how to go about it.
0
Comment
Question by:subo76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 7086828
Option Explicit

Dim sQuestions() As String
Dim c As Long

Private Sub Command1_Click()

    Dim lR As Long
    Dim n As Long
    Dim k As Long
    lR = Int(c * Rnd + 1)
   
    If c = 0 Then MsgBox "no more questions": Exit Sub
   
    n = 0
    k = 0
    While n < lR
        k = k + 1
        If sQuestions(1, k) = "" Then
            n = n + 1
        End If
    Wend
   
    Text1.Text = sQuestions(0, k)
    sQuestions(1, k) = "N"
       
    c = c - 1

End Sub

Private Sub Form_Load()

    Randomize CLng(Now)

    ReDim sQuestions(1, 0)
    Open "c:\questions.txt" For Input As #1
    Do Until EOF(1)
        c = c + 1
        ReDim Preserve sQuestions(1, c)
        Line Input #1, sQuestions(0, c)
       
    Loop

    Close #1
   
End Sub
0
 
LVL 18

Expert Comment

by:deighton
ID: 7086858
the form load event should have been

Private Sub Form_Load()

    Randomize

    ReDim sQuestions(1, 0)
    Open "c:\questions.txt" For Input As #1
    Do Until EOF(1)
        c = c + 1
        ReDim Preserve sQuestions(1, c)
        Line Input #1, sQuestions(0, c)
       
    Loop

    Close #1
   
End Sub
0
 
LVL 3

Expert Comment

by:Elmo_
ID: 7087035
deighton,

I had a look at you code ad tested it.

Just one thing I would like to add is if you have a blank line in the code it will show up as a question.  Throw in an if  statement and it should be fine.

P.s. Nice Code.

Ed.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 3

Expert Comment

by:Elmo_
ID: 7087043
sorry that should be:

if you have a blank line in the text file. It will show up as a question in the text box.
0
 
LVL 1

Author Comment

by:subo76
ID: 7087084
Sorry I forgot to mention that the same line in which I have Question 1 in the text file has answer seperated by tab.
something like this.
Question1 Answer:abc
"there is a tab spacing between the question 1 and answer"

I need to read the question and display it to the user randomly. the code which u sent works fine. Now in the same form I need a checkbox , which if checked should display the answer in the next text box otherwise not.
Thanks in advance

0
 
LVL 18

Accepted Solution

by:
deighton earned 100 total points
ID: 7088070
Option Explicit

Dim sQuestions() As String
Dim c As Long

Private Sub Check1_Click()
With Check1
    If .Value = vbChecked Then
        Text2.Text = Text2.Tag
    Else
        Text2.Text = ""
    End If
End With
   

End Sub

Private Sub Command1_Click()

    Dim lR As Long
    Dim n As Long
    Dim k As Long
    lR = Int(c * Rnd + 1)
   
    If c = 0 Then MsgBox "no more questions": Exit Sub
   
    n = 0
    k = 0
    While n < lR
        k = k + 1
        If sQuestions(1, k) = "" Then
            n = n + 1
        End If
    Wend
   
    Text1.Text = sQuestions(0, k)
    Text2.Tag = sQuestions(2, k)
    If Check1.Value = vbChecked Then
        Text2.Text = Text2.Tag
    End If
    sQuestions(1, k) = "N"
       
    c = c - 1

End Sub

Private Sub Form_Load()

    Dim lSplit As Long

    Randomize CLng(Now)

    ReDim sQuestions(2, 0)
    Open "c:\questions.txt" For Input As #1
    Do Until EOF(1)
        c = c + 1
        ReDim Preserve sQuestions(2, c)
        Line Input #1, sQuestions(0, c)
        lSplit = InStr(sQuestions(0, c), vbTab)
        sQuestions(2, c) = Mid(sQuestions(0, c), lSplit + 1)
        sQuestions(0, c) = Left(sQuestions(0, c), lSplit - 1)
       
    Loop

    Close #1
   
End Sub
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 15 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question