Solved

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

Posted on 2014-09-14
7
150 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

690 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