CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2000
    Location
    IL, U.S.A.
    Posts
    218

    Dialog Box to open a text file.

    I have this code to open a text file for Input.

    Dim intFileNum As Integer, strName As String
    Dim strFilter As String, NumberCorrect As Integer
    Dim intNumberOfQuestions As Integer
    Dim intLoadQuestions As Integer
    Dim intLoadFields As Integer
    Dim strTestName As String
    Const conTitle As String = "Your Name"
    Const conPrompt As String = "Enter your name:"



    'frmQuizzerSplash.Show vbModal

    strName = InputBox(conPrompt, conTitle)

    intFileNum = FreeFile
    strFilter = "Text Files (*.txt)|*.txt|" & "All files (*.*)|*.*"
    cdlOpenfile.Filter = strFilter
    cdlOpenfile.ShowOpen
    g_strFileName = cdlOpenfile.FileName
    'On Error GoTo FileOpenErr
    Open g_strFileName For Input As #intFileNum

    When I hit cancel on the Dialog box I get this error: Path/File Access Error

    And it highlights this line: Open g_strFileName For Input As #intFileNum

    Is there some more code I should have to make the dialog box just cancel?


  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Dialog Box to open a text file.

    Set the CancelError property of the cdlOpenfile common dialog box to True. Then in your code, place an On Error Goto ErrHndlr before the ShowOpen method as follows:




    strName = InputBox(conPrompt, conTitle)

    intFileNum = FreeFile
    strFilter = "Text Files (*.txt)|*.txt|" & "All files (*.*)|*.*"
    cdlOpenfile.Filter = strFilter
    on error GoTo ErrHndlr 'Start the error handling
    cdlOpenfile.ShowOpen
    on error Goto 0 'Stop the error handling
    g_strFileName = cdlOpenfile.FileName
    Open g_strFileName for input as #intFileNum
    ...
    More code here
    ....
    Exit Sub
    ErrHndlr:
    ' error handling code here, or you can leave it empty
    End Sub






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