Solved

Filter files with these extensions (File Dialog Box)

Posted on 2001-06-11
3
217 Views
Last Modified: 2010-05-02
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
      .ShowOpen
    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
      frmOptions.cd_Options.ShowOpen
    Loop
      App_InventoryPath = Trim(frmOptions.cd_Options.FileName)
      frmOptions.txtInventoryPath = App_InventoryPath
    'Save the Database location
    SaveSetting App.Title, "App_Elements", "InventoryPath_DB", App_InventoryPath


-ADawn
0
Comment
Question by:ADawn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6179208
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
Else
  'Write to registry
End If
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6179211
er, change the ...<> "HTML" Or Right$... to ...<> "HTML" And Right$...
0
 
LVL 2

Accepted Solution

by:
TravisHall earned 20 total points
ID: 6179341
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
Else
 '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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question