Solved

Reading randomly from text files and displaying it.

Posted on 2002-06-18
6
132 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

710 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