Filter files with these extensions (File Dialog Box)

Hi again,

VB6 (sp4) ADO

I have two concerns. I want to display all files with these extensions: *.htm and *html in my file dialog box, and I want to check to see if the proper file extension was selected before I save its location to the Windows registry. Thanks for your help (as always).

With frmOptions.cd_Options
      .DialogTitle = "Connect to application's back-end database."
      .InitDir = "C:\DS3VB6\DS3_DATA\"
      .FileName = "" 'App_BackEndDBName
      .Filter = "Access (*.mdb)| *.mdb"
      .CancelError = True 'Causes an error if user clicks on cancel
    End With
'Remember that the file names are case sensitive
    Do While UCase(Right(Trim(frmOptions.cd_Options.FileName), Len("DS3DATA.MDB"))) <> "DS3DATA.MDB"
      MsgBox "You are trying to link to the wrong file.", vbCritical, App.Title
      App_InventoryPath = Trim(frmOptions.cd_Options.FileName)
      frmOptions.txtInventoryPath = App_InventoryPath
    'Save the Database location
    SaveSetting App.Title, "App_Elements", "InventoryPath_DB", App_InventoryPath

Who is Participating?
TravisHallConnect With a Mentor Commented:
Jon's checking code is okay, but it would recognise a longer file extension ending in HTML or HTM as valid, when it isn't. For example, "somefile.chtml" would pass the test, and it shouldn't. To modify that code slightly:

If Right$(UCase(CommonDialog1.FileName), 5) <> ".HTML" Or Right$(UCase(CommonDialog1.FileName), 4) <> 
".HTM" Then
 MsgBox "Invalid Selection!", vbCritical, "Error"
 Exit Sub
 'Write to registry
End If

That would fix that case, and also the case when the full file name is "somethinghtml" (which has no extension).

However, it should also be noted that all file names selected when using the appropriate filter must match that filter. That's what the filter is for. I guess there are times when one should not assume that things will work the way they are supposed to, but that does seem a little too paranoid to me, for the general case.
To limit the users to HTML files:
.Filter = "HTML Files (*.HTML, *.HTM)|*.html;*.htm"

To check the type (place this right after the line of code to show the dialog:
If Right$(UCase(CommonDialog1.FileName), 4) <> "HTML" Or Right$(UCase(CommonDialog1.FileName), 3) <> "HTM" Then
  MsgBox "Invalid Selection!", vbCritical, "Error"
  Exit Sub
  'Write to registry
End If
er, change the ...<> "HTML" Or Right$... to ...<> "HTML" And Right$...
All Courses

From novice to tech pro — start learning today.