Solved

Writing in a file

Posted on 2000-04-02
5
156 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 50 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

16 Experts available now in Live!

Get 1:1 Help Now