Solved

displaying file name in the textbox

Posted on 2004-09-23
8
161 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 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
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 12137703
text1.text = UCase(Left(f.Name, Len(f.Name) - 4))
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now