Solved

displaying file name in the textbox

Posted on 2004-09-23
8
171 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
[X]
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
8 Comments
 
LVL 6

Accepted Solution

by:
bkthompson2112 earned 250 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 100 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 100 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 50 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

689 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