[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

displaying file name in the textbox

Posted on 2004-09-23
8
Medium Priority
?
176 Views
Last Modified: 2010-05-02
Onload VB form, I want to search for a file name in c:\temp, read only the name of the file (without extention) and display in the textbox of the form.

for example, file name = john.txt in c:\temp
In the textbox "JOHN" is displayed (in upper case) as soon as user opens the VB form.

Please help me code.
0
Comment
Question by:Vasi04
8 Comments
 
LVL 6

Accepted Solution

by:
bkthompson2112 earned 1000 total points
ID: 12136843
Hi Vasi04,

Try something like this.  It puts the first filename found in the textbox.

Private Sub Form_Load()
  Dim FileName As String
  FileName = Dir("C:\temp\*.*")
  If InStr(FileName, ".") > 0 Then
    Text1.Text = Left$(FileName, InStr(FileName, ".") - 1)
  Else
    Text1.Text = FileName
  End If
End Sub

bkt
0
 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 400 total points
ID: 12136973
Try this

Option Explicit

Private Sub Form_Load()
    FileAttribute "c:\temp"
End Sub
Public Sub FileAttribute(FileName)
    Dim fso
    Dim folder
    Dim f
    Dim Files
    Dim File
   
    On Error Resume Next
    If (Dir$(FileName & "\", vbDirectory) = "") Then Exit Sub
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(FileName)
   
    Set Files = folder.Files
    If Files.Count <> 0 Then
        For Each File In Files
            Set f = fso.GetFile(File)
            If (UCase(Right(f.Name, 4)) = ".TXT") Then
                List1.AddItem UCase(Left(f.Name, Len(f.Name) - 4))
            End If
            Set f = Nothing
        Next
    End If
End Sub
0
 

Author Comment

by:Vasi04
ID: 12137298
Thanks both of you.
EDDYKT, are we assigning filename to text1.text
can I have this way:
text1.text = List1.AddItem UCase(Left(f.Name, Len(f.Name) - 4))

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:TheClickMaster
ID: 12137703
text1.text = UCase(Left(f.Name, Len(f.Name) - 4))
0
 
LVL 5

Expert Comment

by:abith
ID: 12137752
hi

that should not be like this

text1.text=text1.text & vbcrlf & UCase(Left(f.Name, Len(f.Name) - 4))

and set the multiline property of text box to true then the file name will be added with the enter key.

Regards
Abith. K
0
 
LVL 4

Expert Comment

by:avi247
ID: 12137967

Like the above experts mentioned, Use

text1.text = text1.text & vbcrlf & UCase(Left(f.Name, Len(f.Name) - 4))

Set
Text1 Multiline property to True
Text1 Scrollbars property to Vertical or Both or Horizontal
0
 
LVL 5

Assisted Solution

by:KarcOrigin
KarcOrigin earned 400 total points
ID: 12139621
Try this also:

Option Explicit

Private Sub Form_Load()
    Text1.Text = SetFile("c:\temp", "john.txt")
End Sub

Private Function SetFile(ByVal DirName As String, ByVal SearchFilename As String) As String
    Dim fso As Object
    Dim folder As Object
    Dim files As Object
    Dim file As Object
    Dim f As Object
   
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(DirName)
    Set files = folder.files
    If files.Count <> 0 Then
        For Each file In files
            Set f = fso.GetFile(file)
            Debug.Print f.Name
            If (Trim(UCase(f.Name)) = UCase(SearchFilename)) Then
                SetFile = UCase(Mid(f.Name, 1, InStr(1, f.Name, ".") - 1))
                GoTo CleanExit
            End If
            Set f = Nothing
        Next
    End If
   
CleanExit:
   
    ' Deallocate the memory occupied by different objects
    If Not f Is Nothing Then Set f = Nothing
    If Not file Is Nothing Then Set file = Nothing
    If Not files Is Nothing Then Set files = Nothing
    If Not folder Is Nothing Then Set folder = Nothing
    If Not fso Is Nothing Then Set fso = Nothing

End Function

I have considered that you are searching for a file as you have mentioned in your question.
Thanks.
0
 
LVL 3

Assisted Solution

by:sgayatri
sgayatri earned 200 total points
ID: 12140400

add project >> references >> microsoft scripting runtime

str1 = "Yourfilewithpath"

dim fso as new scripting.filesystemobject
text1.text = fso.getbasename(str1)

prints the filename without extension.

Simple as that.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
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…
Suggested Courses

834 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