Solved

FILE NAMED W/ VARIABLE.VALUE

Posted on 1998-08-21
11
141 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now