Solved

Writing in a file

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA/SQL - Connect to SQL server and pull data 4 150
pop out of webbrowser1 control vba6 5 50
checkbox to hide entire section 10 75
add criteria to query in VB, Access 2003 2 47
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

739 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