Solved

FILE NAMED W/ VARIABLE.VALUE

Posted on 1998-08-21
11
146 Views
Last Modified: 2010-04-30
We have a VB created sequential file named by the value of a variable.  The exact value is unknown.  We need to do a dir (varname) to check for the file's existence.  But we keep getting "specified path/filename is invalid".

halp.
tanx.
0
Comment
Question by:RUSTY
[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
  • 6
  • 4
11 Comments
 
LVL 4

Expert Comment

by:mcix
ID: 1430336
Post your source
0
 

Author Comment

by:RUSTY
ID: 1430337
Private Sub Command1_Click()         'FORM1
Dim DAY As String
DAY = Form1.Calendar1.Value
DAY = Format(DAY, "######")
Open DAY For Output As #1
Print #1, RichTextBox1.Text
Close #1
End Sub


Private Sub Command1_Click()      'FORM2
Dim DAY As String
DAY = Form1.Calendar1.Value
DAY = Format(DAY, "######")
 Form3.Visible = True
 Form3.RichTextBox1.LoadFile (DAY)
End Sub


The file gets created perfect.  But we need to do an If dir(var) before loading file into rtbox in order to avoid errors if that "day" has no entries/created files.  When the file exists, no problem, it loads right in.  It seems only the dir function won't recognize (DAY).
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1430338
Leading spaces on the name is the problem ?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:RUSTY
ID: 1430339
Nope.  Filenames are all tight, example "36013" and "36028".
0
 
LVL 4

Expert Comment

by:mcix
ID: 1430340
By default, RTF textboxes assume an RTF extension.

You could try rtb.Load(mstrfilename,rtfText)
0
 

Author Comment

by:RUSTY
ID: 1430341
Sorry, you seem to misunderstand the problem.  The files do load nicely into the rich text box.  The app at this point is the calendar control.  One clicks a date and enters stuff, which is sent to a sequential file.  Then, to view the data, another button is pushed, which pulls the file, who's name is created from the variable created by the calendar.value (the date which presently has focus on the calendar control.  If there has been no data entered for the chosen day, there is no corresponding file.  If there is no file for that day, there is nothing to load into the rtb, and we get an error.  We want to test the directory first, before attempting to load a file into the rtb, with an if statement.  Problem is, when we do a dir(filename), because the filename is the unknow variable, we enter the variable name for the filename, which works when creating the file, and when reading the file;  just seems the dir function won't recognize it as a filename, like we need to know first what the date with focus ends up looking like when stringized and formatted, then to type that somehow w/ code into the dir(pathname/filename).  HMMMMM.
Darn.  We'll get it yet.  Keep 'em coming.
0
 
LVL 4

Expert Comment

by:mcix
ID: 1430342
Your code looks something like this?

    Dim DAY As String
    DAY = Form1.Calendar1.Value
    DAY = Format(DAY, "######")
    Dim mstrDayFile As String
    mstrDayFile = Dir(DAY)
   
    If DayFile <> "" Then
        RichTextBox1.LoadFile mstrDayFile
    End If

0
 
LVL 4

Expert Comment

by:mcix
ID: 1430343
Ooops...
 
    Dim DAY As String
    DAY = Form1.Calendar1.Value
    DAY = Format(DAY, "######")
    Dim mstrDayFile As String
    mstrDayFile = Dir(DAY)
   
    If mstrDayFile <> "" Then
        RichTextBox1.LoadFile mstrDayFile
    Else
        ' Too bad no file there
    End If

0
 
LVL 4

Expert Comment

by:mcix
ID: 1430344
Here is a generic way to do it:

Private Sub Command1_Click()
   
    Dim DAY As String
    DAY = Form1.Calendar1.Value
    DAY = Format(DAY, "######")
    Dim mstrDayFile As String
    mstrDayFile = Dir(DAY)

    If Not LoadRTF(DAY, RichTextBox1) Then
        ' Failed
        MsgBox "It does not work"
    Else
        ' Succeed
        MsgBox "It works"
    End If

End Sub

Public Function LoadRTF(FileName As String, RtfBox As RichTextBox) As Boolean

    On Error GoTo err_LoadRTF
   
    RtfBox.LoadFile FileName

    LoadRTF = True
   
exit_LoadRTF:
    Exit Function
   
err_LoadRTF:
   
    Select Case Err.Number
   
        Case 75
            ' File or Path Not Found
            MsgBox "The requested file: " & FileName & "could not be loaded", vbInformation, "Check file name"
            LoadRTF = False
           
            Resume exit_LoadRTF
        Case Else
            ' Some other unknown error
            MsgBox "Error: " & Err.Number & " - " & Err.Description, vbInformation, "Generic Error Trap"
            Resume exit_LoadRTF
    End Select
   
End Function

0
 

Author Comment

by:RUSTY
ID: 1430345
mcix, I owe you points!  Please post "answer" so I may award your most successful assitance.
0
 
LVL 4

Accepted Solution

by:
mcix earned 20 total points
ID: 1430346
Glad it worked... Sorry I didn't get it right the first time...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

690 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