• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Random Txt file

OK guys and gals, i have a project that works just like a tip of the day project but i want to have a series of text files that it will randomly pick tips/quotes from, then randomly pick a quote from the txt file. i have included the code i use to load the project.

General Declarations:


Option Explicit

' The in-memory database of tips.
Dim Tips As New Collection

' Name of tips file
Const TIP_FILE = "TIPOFDAY.TXT"

' Index in collection of tip currently being displayed.
Dim CurrentTip As Long



FORM CODE:


Private Sub Form_Load()
    Dim ShowAtStartup As Long
   
    ' See if we should be shown at startup
    ShowAtStartup = GetSetting(App.EXEName, "Options", "Show Tips at Startup", 1)
    If ShowAtStartup = 0 Then
        Unload Me
        Exit Sub
    End If
       
    ' Set the checkbox, this will force the value to be written back out to the registry
    Me.chkLoadTipsAtStartup.Value = vbChecked
   
    ' Seed Rnd
    Randomize
   
    ' Read in the tips file and display a tip at random.
    If LoadTips(App.Path & "\" & TIP_FILE) = False Then
        lblTipText.Caption = "That the " & TIP_FILE & " file was not found? " & vbCrLf & vbCrLf & _
           "Create a text file named " & TIP_FILE & " using NotePad with 1 tip per line. " & _
           "Then place it in the same directory as the application. "
    End If

   
End Sub


ok so far this will ramdomly pick a quote from the TIPOFTHEDAY.txt file now how do i get it to do the same thing but now ramdomly pick from TIPOFTHEDAY.txt to TIPOFTHEDAY2.txt to TIPOFTHEDAY3.txt and so on??? amount of code will be increased on the amount of code i receive w/ comments telling me what is goin on.

THANK YOU,
sage

0
sage55
Asked:
sage55
  • 6
  • 5
1 Solution
 
sage55Author Commented:
ALSO A LIL BONUS... if you could tell me how to put all the text files in a resource file so there is no txt files outside of the .exe i will make sure you get a good bonus!!! thanks a million
0
 
mcriderCommented:
To randomly open one of your tip files, add this function... You can modify it to add however many tip files you want...

    Function GetRandomTipFileName() As String
        Randomize
        Select Case Int((3 * Rnd) + 1)
            Case 1: GetRandomTipFileName = "TIPOFTHEDAY.TXT"
            Case 2: GetRandomTipFileName = "TIPOFTHEDAY2.TXT"
            Case 3: GetRandomTipFileName = "TIPOFTHEDAY3.TXT"
        End Select
    End Function


Then, using your LoadTips code, you can modify your code like this:


   If LoadTips(App.Path & "\" & GetRandomTipFileName) = False Then


Cheers!
0
 
mcriderCommented:
As for resource files, this is an excerpt from the Microsoft Online Books Documentation.  You can access this documentation by clicking "HELP->Books Online..."


Cheers!


THE EXCERPT:

A resource file is a useful mechanism for separating localizable information from code in Visual Basic.
Note   You can have only one resource file in your project. If you attempt to add more than one resource file, Visual Basic generates an error message.
      
Advantages of Storing Strings in Resource Files
When you are writing Visual Basic code, you can use the LoadResString, LoadResPicture, and LoadResData functions in place of references to string literals, pictures, and data. Storing such elements in a resource file offers two benefits:
   *      Performance and capacity are increased because strings, bitmaps, icons, and data can be loaded on demand from the resource file, instead of all being loaded at once when a form or a module is loaded.
   *      The resources that need to be translated are isolated in one resource file. There is no need to access the source code or recompile the application.
      
      To create a resource file
1      Create a resource source file (*.RC) that contains all the string resources of your application.
The syntax for creating the resource source file is documented in Resource.txt in the \Tools subdirectory of the main Visual Basic directory. This information is also available in the Windows Software Development Kit, as well as on the Microsoft Developer Network CD. You must associate an identifier (ID) with each resource, and then reference each ID in your code.
2      Use a resource compiler to convert the resource source file into a resource file (*.res). You can use the resource compiler (Rc.exe) shipped in the \Tools\Resource\Rc32 subdirectory of the main Visual Basic directory to convert the resource source file.
      
Note   In Visual Basic, the resource whose ID is 1 is reserved for the application icon. Therefore, you cannot have a resource in your .res file with that ID number. Visual Basic generates an error message if your code attempts to load that resource ID.
      
      To localize a resource file
1      Load the resource file in a resource editor. AppStudio, which is shipped with Microsoft Visual C++, can be used to edit the entries.
2      Once the file is loaded, localize the entries. Create as many language versions of the strings, bitmaps, icons, and data as you need.
      
      To add a resource file to your project
1      From the Project menu, choose Add File (CTRL+D).
2      In the Add File dialog box, select Resource Files (*.res) in the Files of type box.
3      Select the resource file you want to add to the project, and click Open.
      
Visual Basic recognizes resource files by the .res file name extension. If the resource file does not have the appropriate file name extension, Visual Basic won't load it. Conversely, if any file uses the .res file name extension, Visual Basic interprets that it is a resource file when adding it to the project. If the file does not follow the standard format for a resource file, Visual Basic generates an error message the first time you attempt to use the resource file support functions (LoadResString, LoadResPicture, and LoadResData), or when you try to make an .exe file. Visual Basic will generate the same error message if you try to add a 16-bit resource file to a project.
Once the resource file is added to the project, the .res file will appear in the Project window. Unlike a form or a module, however, you cannot view the resource file in Visual Basic. The file is still considered a standard resource, as if it were created or used by Microsoft Visual C++ and most other Windows-based development tools. When you choose Make projectname.exe from the File menu, Visual Basic compiles all resources in this file into the .exe file as Windows resources.
The .res file, before and after you compile the .exe file, is a standard Windows resource file, which means the resources contained in the file can be loaded in any standard Windows-based resource editor.
Locking Resource Files
Visual Basic uses file locking on the .res file to prevent problems with multiple applications trying to use the file at the same time. Visual Basic will lock the .res file whenever:
   *      Visual Basic is in run or break mode.
   *      You create an .exe file.

For More Information   For an example of how a resource file can be used to create an application that works in several locales, see "The Automated Teller Machine Sample Application" later in this chapter. For background information about programming with resource files, see "Working with Resource Files" in "More About Programming."
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sage55Author Commented:
MCRIDER

ok now where do i put that in the form load??? what do i need to change or delete for the form load???
0
 
mcriderCommented:
The GetRandomTipFileName function goes in the DECLARATIONS SECTION of the FORM or in a MODULE.


Replace this line in the FORM_LOAD:

     If LoadTips(App.Path & "\" & TIP_FILE) = False Then
       
With this:

     If LoadTips(App.Path & "\" & GetRandomTipFileName) = False Then


Cheers!


0
 
sage55Author Commented:
mcrider,

ok can you tell me how-to make the .res file i have not a clue how to make it and then use it, if you could please tell me how to make it, compile it, then access it from my project and anything else i need to do to access the texts files in the project!!! thanks a million

0
 
sage55Author Commented:
MCRIDER,

the code doesnt work, it doesnt ramdomly pick 3 txt files then gets quotes from them. this is what the WHOLE project is maybe that will help...



Option Explicit

' The in-memory database of tips.
Dim Tips As New Collection

' Name of tips file
Const TIP_FILE = "TIPOFDAY.TXT"

' Index in collection of tip currently being displayed.
Dim CurrentTip As Long




Private Sub DoNextTip()

    ' Select a tip at random.
    CurrentTip = Int((Tips.Count * Rnd) + 1)
   
    ' Or, you could cycle through the Tips in order

'    CurrentTip = CurrentTip + 1
'    If Tips.Count < CurrentTip Then
'        CurrentTip = 1
'    End If
   
    ' Show it.
    frmTip.DisplayCurrentTip
   
End Sub

Function LoadTips(sFile As String) As Boolean
    Dim NextTip As String   ' Each tip read in from file.
    Dim InFile As Integer   ' Descriptor for file.
   
    ' Obtain the next free file descriptor.
    InFile = FreeFile
   
    ' Make sure a file is specified.
    If sFile = "" Then
        LoadTips = False
        Exit Function
    End If
   
    ' Make sure the file exists before trying to open it.
    If Dir(sFile) = "" Then
        LoadTips = False
        Exit Function
    End If
   
    ' Read the collection from a text file.
    Open sFile For Input As InFile
    While Not EOF(InFile)
        Line Input #InFile, NextTip
        Tips.Add NextTip
    Wend
    Close InFile

    ' Display a tip at random.
    DoNextTip
   
    LoadTips = True
   
End Function

Private Sub chkLoadTipsAtStartup_Click()
    ' save whether or not this form should be displayed at startup
    SaveSetting App.EXEName, "Options", "Show Tips at Startup", chkLoadTipsAtStartup.Value
End Sub

Private Sub cmdNextTip_Click()
    DoNextTip
End Sub

Private Sub cmdOK_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Dim ShowAtStartup As Long
   
    ' See if we should be shown at startup
    ShowAtStartup = GetSetting(App.EXEName, "Options", "Show Tips at Startup", 1)
    If ShowAtStartup = 0 Then
        Unload Me
        Exit Sub
    End If
       
    ' Set the checkbox, this will force the value to be written back out to the registry
    Me.chkLoadTipsAtStartup.Value = vbChecked
   
    ' Seed Rnd
    Randomize
   
    ' Read in the tips file and display a tip at random.
    If LoadTips(App.Path & "\" & TIP_FILE) = False Then
        lblTipText.Caption = "That the " & TIP_FILE & " file was not found? " & vbCrLf & vbCrLf & _
           "Create a text file named " & TIP_FILE & " using NotePad with 1 tip per line. " & _
           "Then place it in the same directory as the application. "
    End If

   
End Sub

Public Sub DisplayCurrentTip()
    If Tips.Count > 0 Then
        lblTipText.Caption = Tips.Item(CurrentTip)
    End If
End Sub






maybe that will help!!!
thanks a million
0
 
sage55Author Commented:
MCRIDER,

the code doesnt work, it doesnt ramdomly pick 3 txt files then gets quotes from them. this is what the WHOLE project is maybe that will help...



Option Explicit

' The in-memory database of tips.
Dim Tips As New Collection

' Name of tips file
Const TIP_FILE = "TIPOFDAY.TXT"

' Index in collection of tip currently being displayed.
Dim CurrentTip As Long




Private Sub DoNextTip()

    ' Select a tip at random.
    CurrentTip = Int((Tips.Count * Rnd) + 1)
   
    ' Or, you could cycle through the Tips in order

'    CurrentTip = CurrentTip + 1
'    If Tips.Count < CurrentTip Then
'        CurrentTip = 1
'    End If
   
    ' Show it.
    frmTip.DisplayCurrentTip
   
End Sub

Function LoadTips(sFile As String) As Boolean
    Dim NextTip As String   ' Each tip read in from file.
    Dim InFile As Integer   ' Descriptor for file.
   
    ' Obtain the next free file descriptor.
    InFile = FreeFile
   
    ' Make sure a file is specified.
    If sFile = "" Then
        LoadTips = False
        Exit Function
    End If
   
    ' Make sure the file exists before trying to open it.
    If Dir(sFile) = "" Then
        LoadTips = False
        Exit Function
    End If
   
    ' Read the collection from a text file.
    Open sFile For Input As InFile
    While Not EOF(InFile)
        Line Input #InFile, NextTip
        Tips.Add NextTip
    Wend
    Close InFile

    ' Display a tip at random.
    DoNextTip
   
    LoadTips = True
   
End Function

Private Sub chkLoadTipsAtStartup_Click()
    ' save whether or not this form should be displayed at startup
    SaveSetting App.EXEName, "Options", "Show Tips at Startup", chkLoadTipsAtStartup.Value
End Sub

Private Sub cmdNextTip_Click()
    DoNextTip
End Sub

Private Sub cmdOK_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Dim ShowAtStartup As Long
   
    ' See if we should be shown at startup
    ShowAtStartup = GetSetting(App.EXEName, "Options", "Show Tips at Startup", 1)
    If ShowAtStartup = 0 Then
        Unload Me
        Exit Sub
    End If
       
    ' Set the checkbox, this will force the value to be written back out to the registry
    Me.chkLoadTipsAtStartup.Value = vbChecked
   
    ' Seed Rnd
    Randomize
   
    ' Read in the tips file and display a tip at random.
    If LoadTips(App.Path & "\" & TIP_FILE) = False Then
        lblTipText.Caption = "That the " & TIP_FILE & " file was not found? " & vbCrLf & vbCrLf & _
           "Create a text file named " & TIP_FILE & " using NotePad with 1 tip per line. " & _
           "Then place it in the same directory as the application. "
    End If

   
End Sub

Public Sub DisplayCurrentTip()
    If Tips.Count > 0 Then
        lblTipText.Caption = Tips.Item(CurrentTip)
    End If
End Sub






maybe that will help!!!
thanks a million
0
 
mcriderCommented:
Here's your code back... It has the randomized tipfile incorporated. Replace all of your code with this...


Cheers!


THE CODE:

Option Explicit

' The in-memory database of tips.
Dim Tips As New Collection

' Name of tips file
Dim TIP_FILE As String
'Const TIP_FILE = "TIPOFDAY.TXT"

' Index in collection of tip currently being displayed.
Dim CurrentTip As Long

Private Sub DoNextTip()

    ' Select a tip at random.
    CurrentTip = Int((Tips.Count * Rnd) + 1)
     
    ' Or, you could cycle through the Tips in order

'    CurrentTip = CurrentTip + 1
'    If Tips.Count < CurrentTip Then
'        CurrentTip = 1
'    End If
     
    ' Show it.
    frmTip.DisplayCurrentTip
     
End Sub

Function LoadTips(sFile As String) As Boolean
    Dim NextTip As String   ' Each tip read in from file.
    Dim InFile As Integer   ' Descriptor for file.
     
    ' Obtain the next free file descriptor.
    InFile = FreeFile
     
    ' Make sure a file is specified.
    If sFile = "" Then
        LoadTips = False
        Exit Function
    End If
     
    ' Make sure the file exists before trying to open it.
    If Dir(sFile) = "" Then
        LoadTips = False
        Exit Function
    End If
     
    ' Read the collection from a text file.
    Open sFile For Input As InFile
    While Not EOF(InFile)
        Line Input #InFile, NextTip
        Tips.Add NextTip
    Wend
    Close InFile

    ' Display a tip at random.
    DoNextTip
     
    LoadTips = True
     
End Function

Private Sub chkLoadTipsAtStartup_Click()
    ' save whether or not this form should be displayed at startup
    SaveSetting App.EXEName, "Options", "Show Tips at Startup", chkLoadTipsAtStartup.Value
End Sub

Private Sub cmdNextTip_Click()
    DoNextTip
End Sub

Private Sub cmdOK_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Dim ShowAtStartup As Long
     
    ' See if we should be shown at startup
    ShowAtStartup = GetSetting(App.EXEName, "Options", "Show Tips at Startup", 1)
    If ShowAtStartup = 0 Then
        Unload Me
        Exit Sub
    End If
         
    ' Set the checkbox, this will force the value to be written back out to the registry
    Me.chkLoadTipsAtStartup.Value = vbChecked
     
    ' Seed Rnd
    Randomize
     
    ' Read in the tips file and display a tip at random.
    TIP_FILE = GetRandomTipFileName
    If LoadTips(App.Path & "\" & TIP_FILE) = False Then
        lblTipText.Caption = "That the " & TIP_FILE & " file was not found? " & vbCrLf & vbCrLf & _
           "Create a text file named " & TIP_FILE & " using NotePad with 1 tip per line. " & _
           "Then place it in the same directory as the application. "
    End If

     
End Sub

Function GetRandomTipFileName() As String
    Randomize
    Select Case Int((3 * Rnd) + 1)
        Case 1: GetRandomTipFileName = "TIPOFTHEDAY.TXT"
        Case 2: GetRandomTipFileName = "TIPOFTHEDAY2.TXT"
        Case 3: GetRandomTipFileName = "TIPOFTHEDAY3.TXT"
    End Select
End Function
Public Sub DisplayCurrentTip()
    If Tips.Count > 0 Then
        lblTipText.Caption = Tips.Item(CurrentTip)
    End If
End Sub

0
 
sage55Author Commented:
mcrider,

THANK YOU A MILLION!!! that works perfect! even though i have already graded your answer can you tell me what i have to do to make it check to see if TIPOFTHEDAY2.txt is there or not and if not then move on??? anyways thank you a million, it was perfect!!!
0
 
mcriderCommented:
Replace the Form_Load code currently in you program with this one.  If the randomized tip file doesn't exist, then the file TIPOFTHEDAY.TXT is attempted to be used...


Cheers!


THE CODE:

Private Sub Form_Load()
    Dim ShowAtStartup As Long
     
    ' See if we should be shown at startup
    ShowAtStartup = GetSetting(App.EXEName, "Options", "Show Tips at Startup", 1)
    If ShowAtStartup = 0 Then
        Unload Me
        Exit Sub
    End If
         
    ' Set the checkbox, this will force the value to be written back out to the registry
    Me.chkLoadTipsAtStartup.Value = vbChecked
     
    ' Seed Rnd
    Randomize
     
    ' Read in the tips file and display a tip at random.
    TIP_FILE = GetRandomTipFileName
    If Dir(App.Path & "\" & TIP_FILE) = "" Then TIP_FILE = "TIPOFTHEDAY.TXT"
    If LoadTips(App.Path & "\" & TIP_FILE) = False Then
        lblTipText.Caption = "That the " & TIP_FILE & " file was not found? " & vbCrLf & vbCrLf & _
           "Create a text file named " & TIP_FILE & " using NotePad with 1 tip per line. " & _
           "Then place it in the same directory as the application. "
    End If
End Sub

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now