Solved

Reading randomly from text files and displaying it.

Posted on 2002-06-18
6
126 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now