hello guys... im having trouble to open up the 2 files , i tried so many times , but i can't think what's the right directory should i put at the '??????' .... the purpose is to pass the value of directory so it can read and open up 2 of the file that i want to open .... i do hope you could understand me and hopefully you can help me.... pls .....

Option Explicit

Private Sub CmdChkFile_Click()

Dim FileName As String
Dim a As String, b As String
Dim fs As FileSystemObject
Dim ts As TextStream, ts1 As TextStream
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile("???????", ForReading)
Set ts1 = fs.OpenTextFile("??????", ForReading)
txtdata(0).Text = rtndirectory(Trim(CommonDialog1.FileName))
txtdata(1).Text = rtndirectory(Trim(CommonDialog1.FileName))

If txtdata(0).Text = "" Then Exit Sub
If txtdata(1).Text = "" Then Exit Sub


Do While Not ts.AtEndOfStream
a = ts.ReadLine
b = ts1.ReadLine
Loop

Debug.Print


End Sub

Private Sub cmdFileFmt_Click()

On Error GoTo err

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

If Trim(CommonDialog1.FileName) = "" Then Exit Sub

txtdata(1).Text = rtndirectory(Trim(CommonDialog1.FileName))

Exit Sub
err:
MsgBox err.Description, vbCritical, "Error lines " + err.Number


End Sub


Private Sub Cmdsource_Click()
On Error GoTo err

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

If Trim(CommonDialog1.FileName) = "" Then Exit Sub

txtdata(0).Text = rtndirectory(Trim(CommonDialog1.FileName))

Exit Sub
err:
MsgBox err.Description, vbCritical, "Error lines " + err.Number

End Sub

Public Function rtndirectory(vdirectory As String) As String
Dim vtxt As Integer
vtxt = InStr(vdirectory, "\\")
Do While Not vtxt = 0
vdirectory = Mid(vdirectory, 1, vtxt) & Right(vdirectory, Len(vdirectory) - (vtxt + 1))
vtxt = InStr(vdirectory, "\\")
Loop
rtndirectory = vdirectory
End Function

Dani AI

Generated

Short summary: the OpenTextFile calls need the actual full paths that the CommonDialog returned. Storing the dialog results in variables (as did) makes those paths available later when the FileSystemObject opens the two streams. That simple change fixes the immediate "????" problem.

Practical hardening not in the posts: always check that the file exists before opening it (FileExists or Dir). When using late binding with CreateObject("Scripting.FileSystemObject"), symbolic constants like ForReading may not be defined — either add the scripting reference or define the numeric constant. Close each TextStream and release objects after use. Add error handling that reports which filename failed so the source of any I/O error is obvious.

Reading two files together needs care: don’t rely on a single stream’s AtEndOfStream if the files can have different lengths. Either loop while both streams have data (if processing line pairs) or read them independently and handle missing lines. That prevents runtime errors and ensures data remains aligned when the files are meant to be read in parallel.

A note on the path-normalizing routine shown in the thread: altering leading double backslashes can break UNC paths (\server\share...). Prefer leaving the dialog-returned path intact or use FileSystemObject helpers for safe path building and validation. Also make InitDir sensible so dialogs start where expected.

In short: ’s approach is the correct fix. For production use add file-existence checks, clear error messages, proper stream cleanup, and careful handling of UNC or unequal-length files to make the solution robust. Good to see confirmed the fix.

Recommended Answers

All 2 Replies

Hi,

I have modified ur code. Check this:

Option Explicit
Dim HdrFName as String
Dim DatFName As String

Private Sub CmdChkFile_Click()

If Trim(DatFName) ="" Then Exit Sub
If Trim(HdrFName) ="" Then Exit Sub

Dim FileName As String
Dim a As String, b As String
Dim fs As FileSystemObject
Dim ts As TextStream, ts1 As TextStream
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(HdrFName, ForReading)
Set ts1 = fs.OpenTextFile(DatFName, ForReading)
txtdata(0).Text = rtndirectory(Trim(CommonDialog1.FileName))
txtdata(1).Text = rtndirectory(Trim(CommonDialog1.FileName))

If txtdata(0).Text = "" Then Exit Sub
If txtdata(1).Text = "" Then Exit Sub


Do While Not ts.AtEndOfStream
a = ts.ReadLine
b = ts1.ReadLine
Loop

Debug.Print


End Sub

Private Sub cmdFileFmt_Click()

On Error GoTo err

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

If Trim(CommonDialog1.FileName) = "" Then Exit Sub

HdrFName = CommonDialog1.FileName

Exit Sub
err:
MsgBox err.Description, vbCritical, "Error lines " + err.Number


End Sub


Private Sub Cmdsource_Click()
On Error GoTo err

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

If Trim(CommonDialog1.FileName) = "" Then Exit Sub

DatFName = CommonDialog1.FileName

Exit Sub
err:
MsgBox err.Description, vbCritical, "Error lines " + err.Number

End Sub

Public Function rtndirectory(vdirectory As String) As String
Dim vtxt As Integer
vtxt = InStr(vdirectory, "\\")
Do While Not vtxt = 0
vdirectory = Mid(vdirectory, 1, vtxt) & Right(vdirectory, Len(vdirectory) - (vtxt + 1))
vtxt = InStr(vdirectory, "\\")
Loop
rtndirectory = vdirectory
End Function

Regards
Veena

veena thank you ...... millions of thank you

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.