473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

I would like to be able to retrieve the file name when using the File Dialog Property

30
Currently I only get the file path. Is there way to retrieve the file name or do I have to just use the split function.

Expand|Select|Wrap|Line Numbers
  1. Dim fDialog As Office.FileDialog
  2.  
  3.    Dim varFile As Variant
  4.  
  5.    'Clear listbox contents.
  6.    'Me.FileList.RowSource = ""
  7.  
  8.    'Set up the File Dialog.
  9.    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
  10.  
  11.    With fDialog
  12.  
  13.       'Allow user to make multiple selections in dialog box.
  14.       .AllowMultiSelect = True
  15.  
  16.       'Set the title of the dialog box.
  17.       .Title = "Please select one or more files"
  18.  
  19.       'Clear out the current filters, and add our own.
  20.       .Filters.Clear
  21.       .Filters.Add "Access Databases", "*.MDB; *.ACCDB"
  22.       .Filters.Add "Access Projects", "*.ADP"
  23.       .Filters.Add "All Files", "*.*"
  24.  
  25.       'Show the dialog box. If the .Show method returns True, the
  26.       'user picked at least one file. If the .Show method returns
  27.       'False, the user clicked Cancel.
  28.       If .Show = True Then
  29.          'Loop through each file selected and add it to the list box.
  30.          For Each varFile In .SelectedItems
  31.             If IsNull(Me.addAttachmenttbox) Then
  32.             Me.addAttachmenttbox = varFile
  33.             Else
  34.             Me.addAttachmenttbox = Me.addAttachmenttbox & ";" & varFile
  35.             End If
  36.         Next
  37.       Else
  38.          MsgBox "You clicked Cancel in the file dialog box."
  39.       End If
  40.    End With
  41.  
Feb 14 '10 #1
7 2465
ADezii
8,834 Expert 8TB
I posted only the relevant code, Lines 3 and 27 will do the trick:
Expand|Select|Wrap|Line Numbers
  1. Dim fDialog As Office.FileDialog
  2. Dim varFile As Variant
  3. Dim strFileName As String
  4.  
  5. 'Set up the File Dialog.
  6. Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
  7.  
  8. With fDialog
  9.   'Allow user to make multiple selections in dialog box.
  10.   .AllowMultiSelect = True
  11.  
  12.   'Set the title of the dialog box.
  13.   .Title = "Please select one or more files"
  14.  
  15.   'Clear out the current filters, and add our own.
  16.   .filters.Clear
  17.   .filters.Add "Access Databases", "*.MDB; *.ACCDB"
  18.   .filters.Add "Access Projects", "*.ADP"
  19.   .filters.Add "All Files", "*.*"
  20.  
  21.   'Show the dialog box. If the .Show method returns True, the
  22.   'user picked at least one file. If the .Show method returns
  23.   'False, the user clicked Cancel.
  24.   If .Show = True Then
  25.     'Loop through each file selected and add it to the list box.
  26.     For Each varFile In .SelectedItems
  27.       strFileName = Mid$(varFile, InStrRev(varFile, "\") + 1)
  28.         Debug.Print strFileName        'Testamundo!
  29.     Next
  30.   Else
  31.          MsgBox "You clicked Cancel in the file dialog box."
  32.   End If
  33. End With
Feb 14 '10 #2
xraive
30
Thank you so much Adezii. I had used the split function and passed into an array but your way is much simpler.

I don't know if i have to open another question. Talking about the same File Dialog property, once a user selects multiple files I have added the file names to a list box (value list). I would like to set something up where the user can select multiple items to be deleted from the list box. Currently whith my code it only deletes one item instead of the selected items. The multiselect property is set to extended. See below I tried two ways.

Expand|Select|Wrap|Line Numbers
  1. Public Function removeAttachements(ctlList As ListBox)
  2.  
  3.     iCount = 0
  4.  
  5.  
  6.     If ctlList.ItemsSelected.Count <> 0 Then
  7.         For i = ctlList.ListCount - 1 To 0 Step -1
  8.  
  9.             If ctlList.Selected(i) Then
  10.             MsgBox i
  11.                 ctlList.RemoveItem (i)
  12.  
  13.             End If
  14.         Next i
  15.     Else
  16.         MsgBox "Nothing was selected from the list", vbInformation
  17.         Exit Function  'Nothing was selected
  18.     End If
  19.  
  20. End Function
or
Expand|Select|Wrap|Line Numbers
  1. Public Function removeAttachements(ctlList As ListBox)
  2.     Dim oItem As Variant
  3.     Dim sTemp As String
  4.     Dim iCount As Integer
  5.  
  6.     iCount = 0
  7.     oItem = varTemp
  8.  
  9.     If ctlList.ItemsSelected.Count <> 0 Then
  10.     For Each oItem In ctlList.ItemsSelected
  11.             ctlList.RemoveItem Index:=oItem
  12.         Next oItem
  13.     Else
  14.         MsgBox "Nothing was selected from the list", vbInformation
  15.         Exit Function  'Nothing was selected
  16.     End If
  17.  
  18. End Function
Both codes are only removing one item. Is there something I am doing wrong
Feb 14 '10 #3
ADezii
8,834 Expert 8TB
Dynamically removing Multiple Items from a List Box at Runtime can be tricky because of the Re-indexing of the contents. If the RowSourceType of the List Box is Value List, why not simply rebuild the Row Source of the List Box, eliminating those Items that are currently selected?
Expand|Select|Wrap|Line Numbers
  1. Dim lst As ListBox
  2. Dim i As Integer
  3. Dim strBuild As String
  4.  
  5. Set lst = Me![ctlList]
  6.  
  7. With lst
  8.   If .ItemsSelected.Count <> 0 Then
  9.     For i = 0 To .ListCount - 1
  10.       If .Selected(i) = True Then
  11.         'do nothing, don't want it included in strBuild
  12.       Else
  13.         strBuild = strBuild & .ItemData(i) & ";"
  14.       End If
  15.     Next i
  16.   Else
  17.     MsgBox "Nothing was selected from the list", vbInformation
  18.       Exit Sub
  19.   End If
  20. End With
  21.  
  22. Me![ctlList].RowSource = Left$(strBuild, Len(strBuild) - 1)
Feb 15 '10 #4
xraive
30
Hi Adezii,

My current list box has multiple columns. How would i be able to make this work.

Thank you,
Feb 15 '10 #5
ADezii
8,834 Expert 8TB
You're really gonna make me work on this one, aren't you? (LOL). The comparable logic for a 3-Column List Box is:
Expand|Select|Wrap|Line Numbers
  1. Dim lst As ListBox
  2. Dim i As Integer
  3. Dim strBuild As String
  4.  
  5. Set lst = Me![ctlList]
  6.  
  7. With lst
  8.   If .ItemsSelected.Count <> 0 Then
  9.     For i = 0 To .ListCount - 1
  10.       If .Selected(i) = True Then
  11.       Else
  12.         strBuild = strBuild & .Column(0, i) & ";" & .Column(1, i) & _
  13.                                ";" & .Column(2, i) & ";"
  14.       End If
  15.     Next i
  16.   Else
  17.     MsgBox "Nothing was selected from the list", vbInformation
  18.       Exit Sub
  19.   End If
  20. End With
  21.  
  22. Me![ctlList].RowSource = Left$(strBuild, Len(strBuild) - 1)
P.S. - Make the necessary adjustments for a Column Count <> 3
Feb 15 '10 #6
xraive
30
Hi Adezii

What can I say but just Damn you're good!

I had tried this
Expand|Select|Wrap|Line Numbers
  1. strBuild = strBuild & lst.Column(1, i) & ";" & lst.Column(2, i)
and I was getting an error looking at your code I see what I did wrong.

I wish there was a way I can thank you, you have saved me some time.

Time and time again you always come through.

Thank you very much. Untill next time (LOL), and don't worry you can sleep easy tonight. No more questions from me (LOL).

Thank you.
Feb 15 '10 #7
ADezii
8,834 Expert 8TB
The pleasure is all mine, take care.
Feb 15 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jim Witte | last post by:
Hi, How do I set a file input element? If it's a text input, I can set the value property. But this doesn't work with file inputs. And no, I can't change it - I'm writing a script to...
0
by: tkailler | last post by:
Hello, I've created a dialog by creating a class in which Instead of calling wx.Dialog.__init__ I precreate the dialog and then I create the GUI dialog using the Create method. When I create...
3
by: MLH | last post by:
I was running the following code while logged in as a user belonging only to the Users group. Set usrNew = .CreateUser(Me!UserID) 'The user ID is in a control on the form usrNew.PID =...
4
by: marco.nl | last post by:
it fails to retrieve newly intruduced data by the user. i tried this ... AfxGetApp()->WriteProfileString("Settings", "email", EDIT1); UpdateData(FALSE); and this.....
3
by: simon | last post by:
I created web application on file system, which is default by Visual studio 20005 beta2. This gives me some strange port numbers. How can I change, that my application will work on IIS? I...
3
by: tigrrgrr42 | last post by:
I am working(vb.net03and05) with word documents stored in a sql db and I am currently bringing them from a byte array into a temp file to pop into word and make word do its thing as a com object. ...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
2
by: tomromanul | last post by:
Hi all, I built a Windows Froms user control and I use it in IE 6+. It functions properly (it displays properly in IE and executes code, such as displaying a message box), except that in the web...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.