Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CommonDialog.Open Problem in VB5

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
0
0
US
I'm writing a routine to open a file using VB5's common dialog box.&nbsp;&nbsp;Only two types of files are allowed:&nbsp;&nbsp;txt and dat and I want the routine (btnOpen.click) to loop until a valid file is selected.<br><br>I've written something like the following for the click event:<br><br>Private Sub Command1_Click()<br><br>Dim LineIn as String, GoodFile As String, FileExt as String<br><br>GoodFile = &quot;N&quot;<br><br>Do Until GoodFile = &quot;Y&quot;<br>&nbsp;&nbsp;frmOpenFile.Show&nbsp;&nbsp;&nbsp;&nbsp;' this is the cdl form<br>&nbsp;&nbsp;FileName = frmOpenFile!CommonDialog1.FileName<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;FileExt = Ucase$(Right$(FileName, 3))<br><br>&nbsp;&nbsp;If FileExt = &quot;EXE Or ... (other excluded types) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Invalid File Type&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;frmOpenFile.Hide<br>&nbsp;&nbsp;&nbsp;&nbsp;FileName = &quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;GoodFile = &quot;N&quot;<br>&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;GoodFile = &quot;Y&quot;<br>&nbsp;&nbsp;End If<br>Loop<br><br>Open FileName For Input As 1<br>&nbsp;...<br><br>Close 1<br><br>End Sub<br><br>Routine seems to work the first time for a bad file (always works for a 'good' file) but the second time around for a bad one, it jumps out of the loop and tries to open something I don't want it to open OR, if I clear the file name in the common dialog box, it gives me a run-time error 'File or path not found'.<br><br>I KNOW it's right in front of me ... but I'm just not seeing what's wrong.<br><br>Any ideas/suggestions/fixes GRATEFULLY accepted.<br><br>Thanks!<br><br>Tom<br>&nbsp;<br>
 
Have you looked at the <FONT FACE=monospace><b>Filter</font></b> property?<br><br><FONT FACE=monospace><b><br>CommonDialog1.Filter = &quot;All Files (*.*)¦*.*¦Text _<br>&nbsp;&nbsp;&nbsp;Files (*.txt)¦*.txt¦Batch Files (*.bat)¦*.bat&quot;<br></font></b><br><br>This would allow you to only select files with the extensions you want.<br><br>(Your code looks fine - I was going to check it but comdlg32.ocx seems to be b(*&%$$%£d on my machine at the moment :-( I'm going to have to fix that one day)<br><br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them as well. Post the question and send me a note saying "Have a look at so-and-so would you?" - that's fine.
 
Yes - the filter is set to display .dat, .txt and all (*.*) files.<br><br>I can't see that resetting it between attempts by the user to get a 'good' file would have any effect ... ????<br><br>So you're having the ole comdlg32.ocx problems too?&nbsp;&nbsp;Glad I'm not the only one out here with that issue to face!<br><br>Thanks for your note!<br><br>Tom
 
Use this function if you won't give an ocx with your program.Many people don't like have many *.ocx on their PC<br><br>Public Type OPENFILENAME<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lStructSize As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hwndOwner As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hInstance As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrFilter As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrCustomFilter As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nMaxCustFilter As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFilterIndex As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrFile As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nMaxFile As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrFileTitle As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nMaxFileTitle As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrInitialDir As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrTitle As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flags As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFileOffset As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFileExtension As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrDefExt As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lCustData As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpfnHook As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpTemplateName As String<br>End Type<br>'<br>Public Declare Function GetOpenFileName Lib &quot;comdlg32.dll&quot; Alias &quot;GetOpenFileNameA&quot; (pOpenfilename As OPENFILENAME) As Long<br>Public Declare Function GetSaveFileName Lib &quot;comdlg32.dll&quot; Alias &quot;GetSaveFileNameA&quot; (pOpenfilename As OPENFILENAME) As Long<br><br>'<br>' Constant flags<br>Public Const OFN_ALLOWMULTISELECT = &H200<br>Public Const OFN_CREATEPROMPT = &H2000<br>Public Const OFN_ENABLEHOOK = &H20<br>Public Const OFN_ENABLETEMPLATE = &H40<br>Public Const OFN_ENABLETEMPLATEHANDLE = &H80<br>Public Const OFN_EXPLORER = &H80000<br>Public Const OFN_EXTENSIONDIFFERENT = &H400<br>Public Const OFN_FILEMUSTEXIST = &H1000<br>Public Const OFN_HIDEREADONLY = &H4<br>Public Const OFN_LONGNAMES = &H200000<br>Public Const OFN_NOCHANGEDIR = &H8<br>Public Const OFN_NODEREFERENCELINKS = &H100000<br>Public Const OFN_NOLONGNAMES = &H40000<br>Public Const OFN_NONETWORKBUTTON = &H20000<br>Public Const OFN_NOREADONLYRETURN = &H8000<br>Public Const OFN_NOTESTFILECREATE = &H10000<br>Public Const OFN_NOVALIDATE = &H100<br>Public Const OFN_OVERWRITEPROMPT = &H2<br>Public Const OFN_PATHMUSTEXIST = &H800<br>Public Const OFN_READONLY = &H1<br>Public Const OFN_SHAREAWARE = &H4000<br>Public Const OFN_SHAREFALLTHROUGH = 2<br>Public Const OFN_SHARENOWARN = 1<br>Public Const OFN_SHAREWARN = 0<br>Public Const OFN_SHOWHELP = &H10<br>'<br>Global Dialogue As OPENFILENAME<br><br>Public Function Dlg(Title As String, Filter As String, Hwnd As Long) As String<br><br><br>Dim Fichier As String<br>Dim Filtre As String<br>Dim RetVal As Long<br>Dialogue.lStructSize = Len(Dialogue)<br>Dialogue.hwndOwner = Hwnd<br>Dialogue.hInstance = App.hInstance<br>Dialogue.lpstrFilter = Filter<br>Dialogue.lpstrFile = Space(254)<br>Dialogue.nMaxFile = 255<br>Dialogue.lpstrFileTitle = Space(254)<br>Dialogue.nMaxFileTitle = 255<br>Dialogue.lpstrInitialDir = App.Path<br>Dialogue.lpstrTitle = Title<br>Dialogue.flags = 6148<br>'<br>RetVal = GetOpenFileName(Dialogue)<br>If RetVal = 0 Then<br>Dlg = &quot;&quot;<br>Else<br>Dlg = Dialogue.lpstrFile<br>End If<br>End Function<br><br>Send me remark at <A HREF="mailto:drone38@mageos.com">drone38@mageos.com</A>
 
CmDlgSnd.DialogTitle = &quot;Please Select the Text File&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CmDlgSnd.Filter = &quot;Text File (*.TXT) ¦ *.TXT&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CmDlgSnd.InitDir = App.Path<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CmDlgSnd.ShowOpen
 
You don't need to see all files (*.*), just those you<br>want. Like this:<br>Filter = &quot;Files (*.txt;*.dat)¦*.txt;*.dat&quot;<br><br>Also, If you make GoodFile a Boolean, then you can<br>loop until GoodFile is TRUE. This will save your<br>program from evaluating for &quot;Y&quot; each time.<br>Like so:<br><br>Dim GoodFile As Boolean<br><br>Do Until GoodFile<br>'<br>If FileExt =&quot;txt&quot; or FileExt=&quot;dat&quot; Then GoodFile=True<br>'<br>'<br>'<br>Loop<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top