Solved

displaying file name in the textbox

Posted on 2004-09-23
8
169 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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.
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 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…

739 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