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
Solved

FILE NAMED W/ VARIABLE.VALUE

Posted on 1998-08-21
11
144 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

828 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