CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    195

    Files (still...)

    Me again

    1. If I want to programatically create a new file, and it ALREADY Exists - is there a way/function to test for it?

    2. How do I limit a user's reply to an Input box - eg must only reply with 2 char?

    Thanks again



  2. #2
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Files (still...)

    I'm not really certain about your first question so I'll have to skip it for now, but as for your second question regarding the data retreived frm an input box..

    I would put some error checking code in after the input box is called.


    retval = InputBox "Enter some data: ","Title bar"

    If len(retval) > 2 then
    InputBox "Invalid Entry, Please try again: ", "Title bar"
    End If




    Hope that helps!




  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Files (still...)

    4 ways to find if file exists

    1.
    Private Function FileExistsWithDir(ByVal filename As String)
    Dim file_name As String

    On Error Resume Next
    file_name = Dir$(filename)
    FileExistsWithDir = (file_name <> "")
    End Function

    '----
    FileExists = Not (Dir(filespec)="")

    '// there! filespec is the filename with full path. If the file exists then
    '// FileExits = True, if it does not exist, then FileExists = False.
    '// Simple really.

    2.
    Private Function FileExistsWithOpen(ByVal filename As String)
    Dim fnum As String

    fnum = FreeFile
    On Error GoTo FileDoesntExist
    Open filename For Input As fnum
    Close fnum
    FileExistsWithOpen = True
    Exit Function

    FileDoesntExist:
    FileExistsWithOpen = False
    End Function

    3,
    Private Function FileExistsWithFileLen(ByVal filename As String)
    Dim length As Long

    On Error GoTo FileDoesntExist
    length = FileLen(filename)
    FileExistsWithFileLen = True
    Exit Function

    FileDoesntExist:
    FileExistsWithFileLen = False
    End Function

    4.
    Private Function FileExistsWithAttrib(txt)
    Dim lRetVal As Long

    On Error GoTo FileDoesntExist:
    lRetVal = GetAttr(txt)
    FileExistsWithAttrib = True
    Exit Function

    FileDoesntExist:
    FileExistsWithAttrib = False
    End Function



    >>How do I limit a user's reply to an Input box - eg must only reply with 2 char?
    You can enter info not to the Inputbox but Text box with Max = 2



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Files (still...)

    #1 Checking if a file already exists:

    dim strFile as string
    strFile = Dir("c:\myfile.dat")
    If strFile <> "" then
    ' file exists
    else
    ' file does not exist
    end if



    #2 Max 2 input

    private Sub text1_Change()
    If len(Text1.Text) > 2 then
    Beep ' sound a beep
    Text1.text = Left(Text1.Text,2)
    End If
    End Sub




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

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

Featured