Solved

FILE NAMED W/ VARIABLE.VALUE

Posted on 1998-08-21
11
140 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
Comment Utility
Post your source
0
 

Author Comment

by:RUSTY
Comment Utility
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
Comment Utility
Leading spaces on the name is the problem ?
0
 

Author Comment

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

Expert Comment

by:mcix
Comment Utility
By default, RTF textboxes assume an RTF extension.

You could try rtb.Load(mstrfilename,rtfText)
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:RUSTY
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad it worked... Sorry I didn't get it right the first time...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

771 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

10 Experts available now in Live!

Get 1:1 Help Now