hi.. I have a problem on opening 2 file ... as below is my code... I want to open 1 file let's say .Dat file and it will trigger the other .Hdr file. the only different is the extension name , 11U06P0434FN.dat and 11U06P0434FN.hdr .... anyone can help me .... please .

CommonDialog1.Filter = ".Dat"
CommonDialog1.InitDir = App.Path & "\BatchFile"
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen

txtdata(0).Text = rtndirectory(Trim(CommonDialog1.FileTitle))
If Trim(CommonDialog1.FileName) = "" Then Exit Sub
DatFName = CommonDialog1.FileName

CommonDialog1.ShowOpen
txtdata(1).Text = rtndirectory(Trim(CommonDialog1.FileTitle))
HdrFName = CommonDialog1.FileName

End Sub

Dani AI

Generated

Short summary: the simplest, most reliable way is to let the user pick the .dat file once, then build the matching .hdr filename programmatically, check that file exists, and open both. pointed out the dialog filter, and rightly suggested deriving the header name from the data filename — below is a more robust, production-friendly pattern that avoids the pitfalls of naive string math.

A robust flow and example (VB6-style):

  • get the full path chosen by the CommonDialog
  • find the last dot in the filename and replace the extension (handles filenames with extra dots)
  • test for the .hdr file before trying to open it
  • if missing, prompt the user to locate it or abort
  • open the files with proper file numbers and close them when done

Example VB snippet implementing that flow:

' datPath holds the full path returned by the CommonDialog
Dim datPath As String
Dim hdrPath As String
Dim pos As Long

pos = InStrRev(datPath, ".")
If pos > 0 Then
    hdrPath = Left$(datPath, pos - 1) & ".hdr"
Else
    hdrPath = datPath & ".hdr"
End If

If Dir(hdrPath) = "" Then
    If MsgBox("Header file not found: " & hdrPath & vbCrLf & "Locate it manually?", vbYesNo + vbQuestion) = vbYes Then
        ' show a dialog to let user pick the .hdr
    Else
        Exit Sub
    End If
End If

Dim fDat As Integer, fHdr As Integer
fDat = FreeFile
Open datPath For Binary Access Read As #fDat
fHdr = FreeFile
Open hdrPath For Input As #fHdr
' ... process ...
Close #fDat
Close #fHdr

Extra tips: use Dir or the Scripting.FileSystemObject.FileExists to validate files; handle user cancel on the CommonDialog; choose For Binary vs For Input depending on file format; and set a clear CommonDialog filter such as a pattern that lists .dat and .hdr (and All Files) so users can see both types. This avoids asking the user to pick two files when one selection should determine the other.

Recommended Answers

All 7 Replies

So if you mean that you don't get the option to open multiple type files, then you need to change your code slightly.

CommonDialog1.Filter = "Dat File (*.Dat)|Hdr File (*.Hdr)"

This will give you option to open both type of files

thnx binoj_daniel..

Let me know if that worked?

you can also register on my site for more tech articles. click here to visit my site

Hi,

use ur code to open Dat file and then get the header file by just replacing last 3 chars:

DatFName = CommonDialog1.FileName
HdrFName = Replace(LCase(DatFName),".dat",".hdr")

Or

DatFName = CommonDialog1.FileName
HdrFName = Left(DatFName,Len(DatFName)-3) & "hdr"

Regards
Veena

thnx veena , it works .... i use the 2nd one u gave me.... thnx alot.....

thnx veena .... it works .... i use the 2nd one u gave me..... appreciate it....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.