473,326 Members | 2,081 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,326 software developers and data experts.

Using FileDialog property to open and display FileName in a textbox

Hi pals,

I would like to know how to display the FileName of the selected file in the textbox and open it using FileDialog property. I have imported the necessary reference from the library - Microsoft Office Object Library 10.0. Tks

Control:
TextBox - Display FileName
Button - Open

Code:

VBA:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command_Onclick() 
  2.  
  3.     Dim fd As FileDialog 
  4.     Dim varSelected As Variant 
  5.  
  6.     Set fd = Application.FileDialog(msopen....) 
  7.  
  8.     With fd 
  9.         .MultiSelect = True 
  10.         .Show 
  11.  
  12.         If . show = -1 Then 
  13.             For Each varSelected .SelectedItem 
  14.  
  15.             Next 
  16.         Else 
  17.             MsgBox "Cancel" 
  18.         End If 
  19.  
  20.     End With 
  21.  
  22. End Sub
Sep 22 '07 #1
16 17737
ADezii
8,834 Expert 8TB
Hi pals,

I would like to know how to display the FileName of the selected file in the textbox and open it using FileDialog property. I have imported the necessary reference from the library - Microsoft Office Object Library 10.0. Tks

Control:
TextBox - Display FileName
Button - Open

Code:

VBA:
Private Sub Command_Onclick()

Dim fd As FileDialog
Dim varSelected As Variant

Set fd = Application.FileDialog(msopen....)

With fd
.MultiSelect = True
.Show

If . show = -1 Then
For Each varSelected .SelectedItem

Next
Else
MsgBox "Cancel"
End If

End With

End Sub
The following code will display a listing of multiple Files selected in a Text Box named txtDisplayFileNames using the FileDialog Object:
Expand|Select|Wrap|Line Numbers
  1. Const conVerticalOnly As Integer = 2
  2.  
  3. 'Declare a variable as a FileDialog object.
  4. Dim fd As FileDialog, strFilesSelected As String
  5.  
  6. Set fd = Application.FileDialog(msoFileDialogFilePicker)
  7.  
  8. 'Declare a variable to contain the path, it must be a Variant because
  9. 'of the For..Each construct requires it
  10. Dim vrtSelectedItem As Variant
  11.  
  12. With fd
  13.   .AllowMultiSelect = True
  14.   .Title = "Browse Files"
  15.      If .Show = -1 Then
  16.         For Each vrtSelectedItem In .SelectedItems
  17.           strFilesSelected = strFilesSelected & vrtSelectedItem & vbCrLf
  18.         Next vrtSelectedItem
  19.       Else
  20.       'The user pressed Cancel
  21.       End If
  22. End With
  23.  
  24. Set fd = Nothing
  25.  
  26. Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
  27. Me![txtDisplayFileNames] = strFilesSelected
Sep 22 '07 #2
tks for the prompt reply. However, the selected file cannot be opened when the prog is executed. It just display the name of the selected file in the textbox. What I need further is to open the selected file from the DirList. May I know how can that be achieved?

Logic of the prog:-
The prog should run in such a way, whereby the name of the selected file is displayed in the textbox as well as open upon execution. Tks


The following code will display a listing of multiple Files selected in a Text Box named txtDisplayFileNames using the FileDialog Object:
Expand|Select|Wrap|Line Numbers
  1. Const conVerticalOnly As Integer = 2
  2.  
  3. 'Declare a variable as a FileDialog object.
  4. Dim fd As FileDialog, strFilesSelected As String
  5.  
  6. Set fd = Application.FileDialog(msoFileDialogFilePicker)
  7.  
  8. 'Declare a variable to contain the path, it must be a Variant because
  9. 'of the For..Each construct requires it
  10. Dim vrtSelectedItem As Variant
  11.  
  12. With fd
  13.   .AllowMultiSelect = True
  14.   .Title = "Browse Files"
  15.      If .Show = -1 Then
  16.         For Each vrtSelectedItem In .SelectedItems
  17.           strFilesSelected = strFilesSelected & vrtSelectedItem & vbCrLf
  18.         Next vrtSelectedItem
  19.       Else
  20.       'The user pressed Cancel
  21.       End If
  22. End With
  23.  
  24. Set fd = Nothing
  25.  
  26. Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
  27. Me![txtDisplayFileNames] = strFilesSelected
Sep 22 '07 #3
ADezii
8,834 Expert 8TB
tks for the prompt reply. However, the selected file cannot be opened when the prog is executed. It just display the name of the selected file in the textbox. What I need further is to open the selected file from the DirList. May I know how can that be achieved?

Logic of the prog:-
The prog should run in such a way, whereby the name of the selected file is displayed in the textbox as well as open upon execution. Tks
  1. Are you trying to Open Single or Multiple Files?
  2. What File Type Extensions do you wish to be limited to? (What File Types are to opened)?
  3. Are you sure you want to set MultiSelect = True.
  4. What exactly are you trying to accomplish?
Sep 22 '07 #4
ADezii
8,834 Expert 8TB
tks for the prompt reply. However, the selected file cannot be opened when the prog is executed. It just display the name of the selected file in the textbox. What I need further is to open the selected file from the DirList. May I know how can that be achieved?

Logic of the prog:-
The prog should run in such a way, whereby the name of the selected file is displayed in the textbox as well as open upon execution. Tks
  1. Are you trying to Open Single or Multiple Files?
  2. What File Type Extensions do you wish to be limited to? (What File Types are to opened)?
  3. Are you sure you want to set MultiSelect = True?
  4. What exactly are you trying to accomplish?
Sep 22 '07 #5
Hi,

What I want to achieve are:

Upon the button click event, the "Browse file" window will be loaded. When I clicked on the "Open" button, the chosen file should be appearing on the "FileName" Listbox, and the file should open. Eventually, the filename should display on the textbox. In addition, the prog should also open any files in the network drive.

What has been accomplished so far is only displaying the selected fileName on the TextBox. tks
Sep 23 '07 #6
ADezii
8,834 Expert 8TB
Hi,

What I want to achieve are:

Upon the button click event, the "Browse file" window will be loaded. When I clicked on the "Open" button, the chosen file should be appearing on the "FileName" Listbox, and the file should open. Eventually, the filename should display on the textbox. In addition, the prog should also open any files in the network drive.

What has been accomplished so far is only displaying the selected fileName on the TextBox. tks
It seems as though you need the capability to select and Open Files with any File Type Extension (no restrictions on Files). Is this assumption correct? I'm specifically asking you this, because this can easily be done, but involves accessing the API, and I wanted to make sure that you are not in over your head. If this is what you are requesting, just let me know, and I'll gladly post the code with some explanation. Do you need the capability to Open multiple Files at a time (MultiSelect = True) or only allow one File to be Opened at a time (MultiSelect = False). Opening a half dozen or so Files along with the Applications associated with them can get to be real ugly real fast!
Sep 23 '07 #7
Tks for your prompt reply once again.

Yes, u are right!

May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".


It seems as though you need the capability to select and Open Files with any File Type Extension (no restrictions on Files). Is this assumption correct? I'm specifically asking you this, because this can easily be done, but involves accessing the API, and I wanted to make sure that you are not in over your head. If this is what you are requesting, just let me know, and I'll gladly post the code with some explanation. Do you need the capability to Open multiple Files at a time (MultiSelect = True) or only allow one File to be Opened at a time (MultiSelect = False). Opening a half dozen or so Files along with the Applications associated with them can get to be real ugly real fast!
Sep 24 '07 #8
ADezii
8,834 Expert 8TB
Tks for your prompt reply once again.

Yes, u are right!

May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".
May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".
  1. You will be opening Files with no restrictions whatsoever on their Types (.???). To my way of thinking, there is no way to accomplish this but to Open/Execute the Files by nature of their File Extensions. The Server Application associated with the Extension needs to be executed and the File loaded within it. This can be done exclusively within the confines of VBA, but a single API Call wrapped in a Sub-Procedure is still needed.
  2. Going the single File route is an excellent idea.
  3. There is no such Function or Property called DirList. What you may be referring to is either the Dir() Function in VB/VBA, or the DirListBox Control available in Visual Basic.
  4. Give me a little time to get the applicable code together and tie it all in to the FileDialog code that has already been posted.
Sep 24 '07 #9
ADezii
8,834 Expert 8TB
Tks for your prompt reply once again.

Yes, u are right!

May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".
tecnocraze, here is the solution to your problem. I narrowed it down to 3 steps - follow them exactly, and you'll be OK. The code has been thoroughly debugged and is fully operational as is. I would strongly suggest you not try to modify the Function Procedure or actual call, since it does execute an API Function. Well, here goes:
  1. Declare the ShellExecute() API Function in a Standard Code Module. This is what does the 'dirty work' and actually Opens the File based solely on its Extension.
    Expand|Select|Wrap|Line Numbers
    1. Declare Function ShellExecute Lib "shell32.dll" _
    2. Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
    3. ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    4.  
  2. Copy and Paste the Public Execute_Program() Function into a Standard Code Module. This Function is a 'Wrapper' for the actual ShellExecute() Function. It essentially makes the API Function transparent and shields the User from its intricacies. As you can see, it accepts only 3 Arguments, 2 of which you probably will never use. I would strongly suggest you keep the Error Evaluation code in place and not delete it, since it may prove invaluable should you experience any problems Opening Files.
    Expand|Select|Wrap|Line Numbers
    1. Public Function Execute_Program(ByVal strFilePath As String, _
    2.     ByVal strParms As String, ByVal strDir As String) _
    3.     As Boolean
    4.  
    5. 'run program ' <R6>
    6. Dim hwndProgram As Integer
    7. hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, 3)     '3 ==> Show Maximized
    8.  
    9. 'evaluate errors (if any)
    10. Select Case (hwndProgram)
    11.   Case 0
    12.     MsgBox "Insufficient system memory or corrupt program file.", 0, "Error running " & strFilePath
    13.       Execute_Program = False
    14.         Exit Function
    15.   Case 2
    16.     MsgBox "File not found.", 0, "Error running " & strFilePath
    17.       Execute_Program = False
    18.         Exit Function
    19.   Case 3
    20.     MsgBox "Invalid path.", 0, "Error running " & strFilePath
    21.       Execute_Program = False
    22.         Exit Function
    23.   Case 5
    24.     MsgBox "Sharing or Protection Error.", 0, "Error running " & strFilePath
    25.       Execute_Program = False
    26.         Exit Function
    27.   Case 6
    28.     MsgBox "Separate data segments are required for each task.", 0, "Error running " & strFilePath
    29.       Execute_Program = False
    30.         Exit Function
    31.   Case 8
    32.     MsgBox "Insufficient memory to run the program.", 0, "Error running " & strFilePath
    33.       Execute_Program = False
    34.         Exit Function
    35.   Case 10
    36.     MsgBox "Incorrect Windows version.", 0, "Error running " & strFilePath
    37.       Execute_Program = False
    38.         Exit Function
    39.   Case 11
    40.     MsgBox "Invalid program file.", 0, "Error running " & strFilePath
    41.       Execute_Program = False
    42.         Exit Function
    43.   Case 12
    44.     MsgBox "Program file requires a different operating system.", 0, "Error running " & strFilePath
    45.       Execute_Program = False
    46.         Exit Function
    47.   Case 13
    48.     MsgBox "Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath
    49.       Execute_Program = False
    50.         Exit Function
    51.   Case 14
    52.     MsgBox "Unknown program file type.", 0, "Error running " & strFilePath
    53.       Execute_Program = False
    54.         Exit Function
    55.   Case 15
    56.     MsgBox "Windows program does not support protected memory mode.", 0, "Error running " & strFilePath
    57.       Execute_Program = False
    58.         Exit Function
    59.   Case 16
    60.     MsgBox "Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath
    61.       Execute_Program = False
    62.         Exit Function
    63.   Case 19
    64.     MsgBox "Attempt to run a compressed program file.", 0, "Error running " & strFilePath
    65.       Execute_Program = False
    66.         Exit Function
    67.   Case 20
    68.     MsgBox "Invalid dynamic link library.", 0, "Error running " & strFilePath
    69.       Execute_Program = False
    70.         Exit Function
    71.   Case 21
    72.     MsgBox "Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath
    73.       Execute_Program = False
    74.         Exit Function
    75.   Case Else
    76. End Select
    77.   'All is well if we get to this point
    78.   Execute_Program = True
    79. End Function
  3. Last but not least, I've made several modifications to the actual code which Opens the File Dialog, allows you to select a File, then Opens/Executes it. Rather than pinpoint the changes, eliminate the original code and substitute this modified version. This code will Open the Office File Dialog Box, and once a selection has been made and OK-ed by the User, the File will be Opened in the 'Registered' Application that is associated with the File's Extension.
    Expand|Select|Wrap|Line Numbers
    1. Const conVerticalOnly As Integer = 2
    2.  
    3. 'Declare a variable as a FileDialog object.
    4. Dim fd As FileDialog, strFilesSelected As String, blnRetVal As Boolean
    5.  
    6. Set fd = Application.FileDialog(msoFileDialogFilePicker)
    7.  
    8. 'Declare a variable to contain the path, it must be a Variant because
    9. 'of the For..Each construct requires it
    10. Dim vrtSelectedItem As Variant
    11.  
    12. With fd
    13.   .AllowMultiSelect = False
    14.   .Title = "Browse Files"
    15.      If .Show = -1 Then
    16.         For Each vrtSelectedItem In .SelectedItems
    17.           'Now, will only be 1 since MultiSelect = False
    18.           strFilesSelected = vrtSelectedItem
    19.           blnRetVal = Execute_Program(strFilesSelected, "", "")
    20.         Next vrtSelectedItem
    21.       Else
    22.       'The user pressed Cancel
    23.       End If
    24. End With
    25.  
    26. Set fd = Nothing
    27.  
    28. Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
    29. Me![txtDisplayFileNames] = strFilesSelected
  4. Good Luck and let me know how you make out.
Sep 24 '07 #10
tks for your prompt reply once again. Very much appreciated
Sep 25 '07 #11
tks for your prompt reply. Very much apppreciated
Sep 25 '07 #12
ADezii
8,834 Expert 8TB
tks for your prompt reply. Very much apppreciated
Not a problem. It can easily be adapted for multiple file openings also, but I do not think that this would be advisable.
Sep 25 '07 #13
It works perfectly well. Great! Tks
Sep 26 '07 #14
ADezii
8,834 Expert 8TB
It works perfectly well. Great! Tks
Glad it worked out for you.
Sep 28 '07 #15
Hi,
could you tell me how to include all these codes (these 3 steps) into Access 2003 database?
Thanks in advance

@technocraze
Jul 6 '09 #16
ADezii
8,834 Expert 8TB
@sajtovi007
What you are attempting is called 'Hijacking' a Thread and is not permitted in this Forum. All the code you need has already been posted, if you are still confused, please create another Thread explaining your needs, and we will be happy to Reply.
Jul 6 '09 #17

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

Similar topics

112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
2
by: seberino | last post by:
Why can't I import gtk (pygtk) module? It appears gtk module want X perms? >>> import gtk Traceback (most recent call last): File "<stdin>", line 1, in ? File...
2
by: theWizard1 | last post by:
Using Visual Studio.Net 2005, Asp.Net 2.0, and trying to use technique shown in the Programming Asp.Net 3rd Edition by O'Reily, page 257. On the first page, this would become the previous page, I...
0
by: Petr Jakes | last post by:
Hi my script is working well when I am running it from the terminal window. While I am trying to start it as a Cron job, I am getting an Error described bellow: My configuration: Pentium III,...
3
by: technocraze | last post by:
Hi community experts, May I knw whether is it possible to display the corresponding value of the second textbox based on the user input in the first textbox? This means that once the user enters...
3
by: gomzi | last post by:
hi, I am trying to set the maxlength property of a multiline textbox without much success. Could anyone tell me as to how it could be done ? thanks, gomzi.
3
by: sunilrkp | last post by:
javascript code to save the contents of a textarea to a file using FIleDialog.
10
by: dandyliondancer | last post by:
hola i figured out how to make an image appear next to a link when hovering by displaying it as a block and then using background:url div.menu a{ display:block; } div.menu a:hover{
2
by: sajtovi007 | last post by:
Hi, I've found code in thread: Using FileDialog property to open File and display FileName in a textbox but: 1. I do not know how to add it in to ACCESS 2003 database. 2. I have fields in...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.