Solved

Reading randomly from text files and displaying it.

Posted on 2002-06-18
6
131 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

756 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