Click to See Complete Forum and Search --> : Dialog Box to open a text file.


Drew
March 17th, 2001, 11:40 PM
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?

shree
March 18th, 2001, 08:00 AM
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