Results 1 to 8 of 8

Thread: HowDoI list files in InputBox prompt???

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47
    I must store directories in a file I have named avail.txt
    I can list the first Directory in the InputBox prompt -
    but do not how to list all available directories.

    So far I have:

    Open App.Path & "\Avail.txt" For Input As #1
    Input #1, direct
    title = "Choose a Directory"
    prompt = "Enter the Directory to be accessed. " & _
    "Existing choices are:'" & direct & "'. Or enter name of " & _
    "new directory to be created."

    HOW to display all direct in file?????:|

  2. #2
    Guest
    Is this what your trying to do?

    Code:
    Open "C:\avail.txt" For Input As #1
    a = "Files:" & Chr$(10) & "" & Input(LOF(1), 1)
        x = InputBox(a)
        Close #1

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47
    Exactly, and - Tell me what the code is saying??

    I am so new - I'm not even green yet!

    I am working from home as an external degree student,
    using only an intro to VB book and a learners edition program (ie: no help files.)
    Being Frustrated would be nice, I'm way beyond that!

  4. #4
    Guest
    Originally posted by Matthew Gates
    Is this what your trying to do?

    Code:
    Open "C:\avail.txt" For Input As #1 'Opens your file
    a = "Files:" & Chr$(10) & "" & Input(LOF(1), 1) 'String that your going to be using in your inputbox
        x = InputBox(a) 'Inputbox using string
        Close #1 'Close the file you opened
    From the VB Help File:

    [/code]
    Opening Files:

    This example illustrates various uses of the Open statement to enable input and output to a file.

    The following code opens the file TESTFILE in sequential-input mode.

    Open "TESTFILE" For Input As #1
    ' Close before reopening in another mode.
    Close #1


    This example opens the file in Binary mode for writing operations only.

    Open "TESTFILE" For Binary Access Write As #1
    ' Close before reopening in another mode.
    Close #1


    The following example opens the file in Random mode. The file contains records of the user-defined type Record.

    Type Record ' Define user-defined type.
    ID As Integer
    Name As String * 20
    End Type

    Dim MyRecord As Record ' Declare variable.
    Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
    ' Close before reopening in another mode.
    Close #1


    This code example opens the file for sequential output; any process can read or write to file.

    Open "TESTFILE" For Output Shared As #1
    ' Close before reopening in another mode.
    Close #1


    This code example opens the file in Binary mode for reading; other processes cannot read file.

    Open "TESTFILE" For Binary Access Read Lock Read As #1
    [/code]

    [/code]
    Inputbox:

    This example shows various ways to use the InputBox function to prompt the user to enter a value. If the x and y positions are omitted, the dialog box is automatically centered for the respective axes. The variable MyValue contains the value entered by the user if the user chooses OK or presses the ENTER key . If the user chooses Cancel, a zero-length string is returned.

    Message = "Enter a value between 1 and 3" ' Set prompt.
    Title = "InputBox Demo" ' Set title.
    Default = "1" ' Set default.
    ' Display message, title, and default value.
    MyValue = InputBox(Message, Title, Default)

    ' Use Helpfile and context. The Help button is added automatically.
    MyValue = InputBox(Message, Title, , , , "DEMO.HLP", 10)

    ' Display dialog box at position 100, 100.
    MyValue = InputBox(Message, Title, Default, 100, 100)
    [/code]

    [/code]
    Input mode:
    See opening files
    [/code]

    I don't exactly understand what you are trying to do anyway. Explain more.

    [Edited by Matthew Gates on 06-18-2000 at 03:17 PM]

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47
    My assignment:
    Create and maintain Telephone directories: write a program and create and maintain telephone directories. Each directory will be a spearate sequential file. The following command buttons should be available:
    1. select a directory to access, a list of directories that have been created should be stored in a separate sequential file and when requested made to open a directory. The available directories should be displayed as part of an InputBox prompt requesting the name of the directory to be accessed - if the user responds with a directory name not listed, the desire to create a new directory should be confirmed, and then the new directory created and added to the list of existing directories.
    2. add name and phone number (given in text box) to the end of the current directory
    3. delete name (as given in text box) from the current directory
    4. sort the current directory into name order
    5. print out names and phone numbers contained in the current directory
    6. terminate the program.

    Of course this is due tomorrow! (one of 4 programs, the other 3 I have completed this weekend).
    I think I can handle 2 to 6, but #1 still has me going - so far i have:
    Private Sub cmdSelect_Click()
    Dim fileName As String, prompt As String, title As String, _
    choice As String, direct As String
    Open App.Path & "\Avail.txt" For Input As #1
    a = "Choose from the following files:" & Chr$(10) & "" & _
    Input(LOF(1), 1) & "or enter a new file name."
    x = InputBox(a, "Choose a file")
    Close #1
    If UCase(x) = "APP.PATH & 'APHONE.TXT'" Then
    Open App.Path & "\Aphone.txt" For Append As #1
    ElseIf UCase(x) = "APP.PATH & '\BPHONE.TXT'" Then
    Open App.Path & "\Bphone.txt" For Append As #1
    ElseIf UCase(x) <> "APP.PATH & '\APHONE.TXT'" Or _
    UCase(x) <> "APP.PATH & '\BPHONE.TXT'" Then
    title = "Directory Request"
    prompt = "The directory you have requested does not " & _
    "exist. Do you want this to be your new directory?"
    response = MsgBox(prompt, vbYesNo, title)
    If response = vbYes Then
    Open x For Append As #1
    Else
    prompt = "You must choose a directory"
    c = MsgBox(prompt, vbOKOnly, "Error")
    End If
    End If
    End Sub

    and it's not working yet!

    [Edited by koperski on 06-18-2000 at 03:52 PM]

  6. #6
    Guest
    I was wondering..if you have the learners edition. That means you can't make an exe right? Isn't that the purpose of making the program, so you can release it? Or is it for your own use only?

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47
    Learners edition - I suppose this is for my use only.
    I am taking VBII as a college course. This is the version that came with my book. I am currently only programming for assignments.

    If you are game - I have another glitch.
    When I try to run this program it errors on ReadData,
    saying "Run time error 9, Subscript out of range"

    yikes... it is getting late! I have to get this done.

    Dim fileName As String

    Private Sub cmdAdd_Click()
    Dim nom As String, phoneNum As String
    nom = txtName.Text
    phoneNum = txtphoneNum.Text
    If (nom <> "") And (phoneNum <> "") Then
    Open fileName For Append As #1
    Write #1, txtName.Text, txtphoneNum.Text
    Close #1
    txtName.Text = ""
    txtphoneNum.Text = ""
    txtName.SetFocus
    Else
    message = "You must enter a name and phone number."
    MsgBox message, , "Information Incomplete"
    txtName.SetFocus
    End If
    End Sub

    Private Sub cmdDelete_Click()
    Dim nom As String, phoneNum As String
    Dim foundFlag As Boolean
    foundFlag = False
    Open fileName For Input As #1
    Open App.Path & "\temp.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, nom, phoneNum
    If UCase(nom) <> UCase(txtName.Text) Then
    Write #2, nom, phoneNum
    Else
    foundFlag = True
    End If
    Loop
    Close #1
    Close #2
    Kill fileName
    Name App.Path & "\temp.txt" As fileName
    If Not foundFlag Then
    MsgBox "The name was not found.", , ""
    txtName.SetFocus
    Else
    txtName.Text = ""
    txtphoneNum.Text = ""
    txtName.SetFocus
    End If
    End Sub

    Private Sub cmdExit_Click()
    End
    End Sub

    Private Sub cmdSelect_Click()
    Dim nom As String, phoneNum As String, x As String
    Dim prompt As String, title As String, q As String
    Open App.Path & "\Avail.txt" For Input As #1
    a = "Choose from the following files:" & Chr$(10) & "" & _
    Input(LOF(1), 1) & "or enter a new directory."
    q = InputBox(a, "Choose a directory")
    Close #1
    If UCase(q) = "WORK NUMBERS" Then
    x = App.Path & "\Aphone.txt"
    ElseIf UCase(q) = "HOME NUMBERS" Then
    x = App.Path & "\Bphone.txt"
    ElseIf UCase(q) <> "WORK NUMBERS" Or _
    UCase(q) <> "HOME NUMBERS" Then
    title = "Directory Request"
    prompt = "The directory you have requested does not " & _
    "exist. Do you want this to be your new directory?"
    response = MsgBox(prompt, vbYesNo, title)
    If response = vbYes Then
    x = App.Path & "\Cphone.txt"
    Else
    prompt = "You must choose a directory"
    c = MsgBox(prompt, vbOKOnly, "Error")
    End If
    End If
    txtName.SetFocus
    fileName = x
    End Sub

    Private Sub cmdSort_Click()
    Dim numFiles As Integer
    numFiles = NumberOfFiles
    ReDim nom(index) As String
    ReDim phoneNum(index) As String
    Call ReadData(nom(), phoneNum(), numFiles)
    Call SortData(nom(), phoneNum(), numFiles)
    Call ShowData(nom(), phoneNum(), numFiles)
    Call WriteData(nom(), phoneNum(), numFiles)
    End Sub

    Private Sub ReadData(nom() As String, phoneNum() As String, _
    numFiles As Integer)
    Dim index As Integer
    'read data from file into arrays
    Open fileName For Input As #1
    For index = 1 To numFiles
    Input #1, nom(index), phoneNum(index)
    Next index
    Close #1
    End Sub

    Private Sub swapData(nom() As String, phoneNum() As String, _
    index As Integer)
    Dim ntemp As String, ptemp As String
    ntemp = nom(index)
    nom(index) = nom(index + 1)
    nom(index + 1) = ntemp
    ptemp = phoneNum(index)
    phoneNum(index) = phoneNum(index + 1)
    phoneNum(index + 1) = ptemp
    End Sub

    Private Function NumberOfFiles() As Integer
    Dim nom As String, phoneNum As String
    Dim n As Integer
    n = 0
    Open fileName For Input As #1
    Do While Not EOF(1)
    Input #1, nom, phoneNum
    n = n + 1
    Loop
    Close #1
    NumberOfFiles = n
    End Function

    Private Sub SortData(nom() As String, phoneNum() As String, _
    numFiles As Integer)
    Dim passNum As Integer, index As Integer
    For passNum = 1 To numFiles - 1
    For index = 1 To numFiles - passNum
    If UCase(nom(index)) < UCase(nom(index + 1)) Then
    Call swapData(nom(), phoneNum(), index)
    End If
    Next index
    Next passNum
    End Sub

    Private Sub ShowData(nom() As String, phoneNum() As String, _
    numFiles As Integer)
    Dim index As Integer
    picDisplay.Cls
    For index = 1 To numFiles
    picDisplay.Print nom(index), phoneNum(index)
    Next index
    End Sub

    Private Sub WriteData(nom() As String, phoneNum() As String, _
    numFiles As Integer)
    Dim index As Integer
    Open fileName For Output As #1
    For index = 1 To numFiles
    Write #1, nom(index), phoneNum(index)
    Next index
    Close #1
    End Sub

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Step 1

    'step 1

    Private Sub Command1_Click()

    Dim intNum, iCount As Integer
    intNum = FreeFile

    Dim sfile, sDir As String
    sfile = "c:\my documents\myDir.txt"

    Dim iArray()
    Dim myList As String
    myList = "Here are the listed directories." & vbCrLf
    myList = myList & "Choose one of these or create a new one." & vbCrLf

    Open sfile For Input As intNum
    'get rec count
    Do While Not EOF(intNum)
    iCount = iCount + 1
    Input #intNum, sDir
    Loop
    Close #intNum

    ReDim iArray(iCount)
    iCount = 0
    Open sfile For Input As intNum
    Do Until EOF(intNum)
    iCount = iCount + 1
    Input #intNum, sDir
    iArray(iCount) = sDir
    myList = myList & iArray(iCount) & vbCrLf
    Loop
    Close #intNum
    Dim myInput
    myInput = InputBox(myList, "Help me out here.")

    Dim sFound As Boolean
    sFound = False

    For iCount = 1 To iCount
    If myInput = iArray(iCount) Then
    sFound = True
    Exit For
    End If

    Next iCount

    If sFound = True Then
    MsgBox "code to open or whatever" & myInput
    End If
    If sFound = False Then
    MsgBox "Code it to make a new dir here...use the function MkDir()"
    End If

    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width