?
Solved

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

Posted on 2014-09-14
7
Medium Priority
?
151 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Suggested Courses

752 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