Solved

Writing in a file

Posted on 2000-04-02
5
161 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
[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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

688 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