Solved

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

Posted on 2014-09-14
7
147 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
ID: 40322406
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
ID: 40322419
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
ID: 40322454
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:isnoend2001
ID: 40322486
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
ID: 40322488
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
ID: 40322604
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
ID: 40323422
Thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…

813 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

15 Experts available now in Live!

Get 1:1 Help Now