Solved

Filter files with these extensions (File Dialog Box)

Posted on 2001-06-11
3
214 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

948 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now