Solved

Reading randomly from text files and displaying it.

Posted on 2002-06-18
6
127 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

919 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

21 Experts available now in Live!

Get 1:1 Help Now