Solved

Add the last Directory File name from a path to a combobox

Posted on 2014-09-14
7
144 Views
Last Modified: 2014-09-15
Typical Paths
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Roofing Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Fencing Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\LanndScaping Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Gardner Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\New Gutter Bids
Add to Combo box
With cboBids
.AddItem Roofing Bids
.AddItem Fencing Bids
.AddItem LanndScaping Bids
.AddItem Gardner Bids
.AddItem New Gutter Bids
End With
How to do this ?
0
Comment
Question by:isnoend2001
  • 4
  • 2
7 Comments
 
LVL 12

Expert Comment

by:jkaios
Comment Utility
Isn't this the same as http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_28517766.html?

Or do you mean JUST the LAST directory, which is "New Gutter Bids" and discard the other?
0
 

Author Comment

by:isnoend2001
Comment Utility
Thanks for your reply I have decided not to use the Dir function
My app creates the directories so i have decided to store the files/paths in an array and save the array.
i have tried this:
Private Sub Command5_Click()
Dim Result As String
Result = LastDirctory("C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\New Gutter Bids")
Debug.Print Result
End Sub

Function LastDirctory(FileWithPath As String) As String
Dim BackSlash As String
Dim position As Integer
BackSlash = "\"
position = InStrRev(FileWithPath, BackSlash, , vbTextCompare)
LastDirctory = Right(FileWithPath, position)
End Function

Open in new window

But this returns this:
d Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\New Gutter Bids
I only want to return: New Gutter Bids
what is wrong ?
0
 
LVL 12

Assisted Solution

by:jkaios
jkaios earned 200 total points
Comment Utility
You can still use the technique that you've used in your previous post using the Dir() function before and inside a loop.  Just remember that the last call to the Dir() function will hold the last directory/filename.  What you just need to do is check if there is no more files/directories.

Just add a new variable - strLastDir.

So, based on your directory structure, here is what code to get the last directory from the given path:

   Dim MyFile, MyPath, MyName, strLastDir
   MyPath = "'C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\'"
   MyName = Dir(MyPath, vbDirectory)   'Retrieve the first entry.
   Do While MyName <> ""
      If (GetAttr(MyPath & MyName) And vbDirectory) Then
         If (MyName <> ".") And (MyName <> "..") Then
            Debug.Print MyName
            strLastDir = MyName            '...THIS IS THE NEW LINE to STORE LAST DIRECTORY
         End If
      End If
      MyName = Dir()
   Loop

   Debug.Print strLastDir
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:isnoend2001
Comment Utility
Thanks for adding that
I get Bad Filename or number on this line:
MyName = Dir(MyPath, vbDirectory)   'Retrieve the first entry.
0
 

Author Comment

by:isnoend2001
Comment Utility
I got this to work:
Function LastDirctory(FileWithPath As String) As String
    Dim FilewithPathLength As Integer
    Dim BackSlash As String
    Dim position As Integer
    FilewithPathLength = Len(FileWithPath)
    BackSlash = "\"
    position = InStrRev(FileWithPath, BackSlash, , vbTextCompare)
    position = FilewithPathLength - position
    LastDirctory = Right(FileWithPath, position)
End Function
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 300 total points
Comment Utility
You could just use Split
Function LastDirctory(FileWithPath As String) As String
    Dim strParts() As String
    strParts = Split(FileWithPath, "\")
    LastDirctory = strParts(UBound(strParts))
End Function

Open in new window

0
 

Author Closing Comment

by:isnoend2001
Comment Utility
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now