Improve company productivity with a Business Account.Sign Up

x
?
Solved

displaying file name in the textbox

Posted on 2004-09-23
8
Medium Priority
?
180 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
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.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

606 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