• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

FILE NAMED W/ VARIABLE.VALUE

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
RUSTY
Asked:
RUSTY
  • 6
  • 4
1 Solution
 
mcixCommented:
Post your source
0
 
RUSTYAuthor Commented:
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
 
Answers2000Commented:
Leading spaces on the name is the problem ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RUSTYAuthor Commented:
Nope.  Filenames are all tight, example "36013" and "36028".
0
 
mcixCommented:
By default, RTF textboxes assume an RTF extension.

You could try rtb.Load(mstrfilename,rtfText)
0
 
RUSTYAuthor Commented:
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
 
mcixCommented:
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
 
mcixCommented:
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
 
mcixCommented:
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
 
RUSTYAuthor Commented:
mcix, I owe you points!  Please post "answer" so I may award your most successful assitance.
0
 
mcixCommented:
Glad it worked... Sorry I didn't get it right the first time...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now