?
Solved

Writing in a file

Posted on 2000-04-02
5
Medium Priority
?
165 Views
Last Modified: 2010-05-02
Hi there!

I want a system that writes down 5 names and a number to a file.

Here is an example:
I type in these in a textbox: (the numbers are random, which I DON'T type myself)

Mike    (45)
John     (2)
Susan  (15)
Kim      (8)
Lisa      (293)

And when I open the file in a textbox, I want to get the following result:
(The numbers ARE visible)

Lisa     293
Mike     45
Susan    15
Kim      8
John     2

As you can see, the name that got the highest number, comes up first etc.

Thank you!
//Caminman
0
Comment
Question by:Caminman
5 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2677677
How large should the random numbers be ?
0
 

Expert Comment

by:kplonk
ID: 2677740
dose this have to be a file or are you simply trying to put some name sin a random order,
0
 

Accepted Solution

by:
mfclaes earned 200 total points
ID: 2677805
Hi,

because I'm not sure where you get your random numbers, I added them in the first listbox

this sample needs 2 listboxes (list1, list2)and 3 commandbuttons (command1,... 3)

it may not be as you wanted, I think it has the functionality that you want

have fun



Option Explicit

Dim strFilepath As String

Private Sub Command1_Click()
    Call write_file
End Sub

Private Sub Command2_Click()
    Call reed_file
End Sub

Private Sub Command3_Click()
    Call init
End Sub

Private Sub Form_Load()

Call init

End Sub

Sub init()
    Randomize
    Dim intCounter As Integer
    Dim lngNumber(4) As Long
    For intCounter = 0 To 4
        lngNumber(intCounter) = Int((10000 * Rnd) + 1)
        DoEvents
    Next intCounter
   
    List1.Clear
    List1.AddItem "Mike" & Chr(44) & lngNumber(0)
    List1.AddItem "John" & Chr(44) & lngNumber(1)
    List1.AddItem "Susan" & Chr(44) & lngNumber(2)
    List1.AddItem "Kim" & Chr(44) & lngNumber(3)
    List1.AddItem "Lisa" & Chr(44) & lngNumber(4)

    Dim strName As String
    strName = "myfile"
    strFilepath = App.Path
   
    If Right(strFilepath, 1) <> "\" Then strFilepath = strFilepath & "\"
    strFilepath = strFilepath & strName & ".txt"
   
End Sub


Sub reed_file()
    On Error Resume Next
    Dim strUserName(4) As String
    Dim lngNumber(4) As Long
    Dim intCounter As Integer
    Dim intTimes As Integer
    Dim varBuffer(1) As Variant
   
    List2.Clear
   
    Open strFilepath For Input As #1
    Do While Not EOF(1)
    For intCounter = 0 To 4
        Input #1, strUserName(intCounter), lngNumber(intCounter)
        Next intCounter
    Loop
    Close #1
    For intTimes = 1 To 4
        For intCounter = 4 To 1 Step -1
            If CLng(lngNumber(intCounter)) < CLng(lngNumber(intCounter - 1)) Then
                varBuffer(0) = strUserName(intCounter)
                varBuffer(1) = lngNumber(intCounter)
                lngNumber(intCounter) = CLng(lngNumber(intCounter - 1))
                lngNumber(intCounter - 1) = varBuffer(1)
                strUserName(intCounter) = strUserName(intCounter - 1)
                strUserName(intCounter - 1) = varBuffer(0)
            End If
        Next intCounter
       Next intTimes
       
    For intCounter = 0 To 4
        List2.AddItem strUserName(intCounter) & Chr(9) & lngNumber(intCounter)
    Next intCounter
   
End Sub

Sub write_file()
    On Error GoTo errorhandler
    Dim strUserName(4) As String
    Dim lngNumber(4) As Long
    Dim intCounter As Integer
   
    Randomize
    For intCounter = 0 To 4
        lngNumber(intCounter) = Int((10000 * Rnd) + 1)
        DoEvents
    Next intCounter
   
    Open strFilepath For Output As #1
        For intCounter = 0 To List1.ListCount - 1
            Print #1, Trim(List1.List(intCounter))        ' ; ","; lngNumber(intCounter)
        Next intCounter
        Close #1
   
    Exit Sub
errorhandler:
    Resume Next
End Sub

0
 
LVL 1

Expert Comment

by:jonder
ID: 2677811
When you open the file to display your sorted names. Do you have to use a text box?  If so load your name, value into a ReDimensioned  array, then bubble sort. Then display your sorted selection to your textbox.
0
 
LVL 1

Author Comment

by:Caminman
ID: 2677958
Hi, and thank you for answering so quickly!

My description wasn't the best, but anyhow, the function of the codes were excellent, even if i have to modify them a bit.

Once again, Thank you! :)

I'll return if I have further problems!

//Caminman
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

864 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