By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

Command Button to Browse for File, Browser Window Opens Twice

P: 12
Hi,

On a form I have a command button that allows a user to browse for a file (PDF, Word, etc…). This command button then stores a hyperlink to the file within an associated txtfield in the table.

The VB to browse for the file is as follows

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub CmdBuildingAdd_Click()
  3. Me!txtSelectedFile = BrowseFiles()
  4.    variable = BrowseFiles()
  5. End Sub
  6.  
  7.  
This VB code calls the following module:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Compare Database
  3. Private Const VER_PLATFORM_WIN32_NT = 2
  4. Private Type OSVERSIONINFO
  5.    dwOSVersionInfoSize As Long
  6.    dwMajorVersion As Long
  7.    dwMinorVersion As Long
  8.    dwBuildNumber As Long
  9.    dwPlatformId As Long
  10.    szCSDVersion As String * 128
  11. End Type
  12.  
  13. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
  14.                         (ByRef lpVersionInformation As OSVERSIONINFO) As Long
  15. Private Declare Function GetFileNameFromBrowseW Lib "shell32" Alias "#63" _
  16.                         (ByVal hwndOwner As Long, _
  17.                          ByVal lpstrFile As Long, _
  18.                          ByVal nMaxFile As Long, _
  19.                          ByVal lpstrInitialDir As Long, _
  20.                          ByVal lpstrDefExt As Long, _
  21.                          ByVal lpstrFilter As Long, _
  22.                          ByVal lpstrTitle As Long) As Long
  23. Private Declare Function GetFileNameFromBrowseA Lib "shell32" Alias "#63" _
  24.                         (ByVal hwndOwner As Long, _
  25.                          ByVal lpstrFile As String, _
  26.                          ByVal nMaxFile As Long, _
  27.                          ByVal lpstrInitialDir As String, _
  28.                          ByVal lpstrDefExt As String, _
  29.                          ByVal lpstrFilter As String, _
  30.                          ByVal lpstrTitle As String) As Long
  31.  
  32. Public Function BrowseFiles()
  33.    Dim sSave As String
  34.  
  35.    sSave = Space(255)
  36.    'If we're on WinNT, call the unicode version of the function
  37.    If IsWinNT Then
  38.       GetFileNameFromBrowseW Screen.ActiveForm.hwnd, _
  39.                              StrPtr(sSave), _
  40.                              255, _
  41.                              StrPtr("t:\"), _
  42.                              StrPtr("doc"), _
  43.                              StrPtr("Word files (*.doc)" + Chr$(0) + "*.doc" + Chr$(0) + _
  44.                                     "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), _
  45.                               StrPtr("The Title")
  46.    'If we're not on WinNT, call the ANSI version of the function
  47.    Else
  48.       GetFileNameFromBrowseA Screen.ActiveForm.hwnd, _
  49.                              sSave, _
  50.                              255, _
  51.                              "t:\", _
  52.                              "doc", _
  53.                              "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0) + _
  54.                              "Word files (*.doc)" + Chr$(0) + "*.doc" + Chr$(0), _
  55.                              "The Title"
  56.    End If
  57.  
  58.    BrowseFiles = Trim(Replace(sSave, Chr$(0), " "))
  59.  
  60. End Function
  61.  
  62. Public Function IsWinNT() As Boolean
  63.    Dim myOS As OSVERSIONINFO
  64.  
  65.    myOS.dwOSVersionInfoSize = Len(myOS)
  66.    GetVersionEx myOS
  67.    IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
  68.  
  69. End Function
  70.  
  71.  
It all works, but three are a few odd issues:

#1: When the user clicks the command button, a file browser window opens to allow the user to select a file. Once they navigate to it and click “Open”, the name of the file dose not appear in then “Open” field of the browser window, BUT the hyperlink is truly added to the database table.

#2: After the user selects the file in the browser window and clicks open, ANOTHER browser window appears and wants the user to navigate to a file again. It is redundant since they have completed this procedure in #1. At this point, they have to click “cancel”.

Is there a way to code this so the user can see the name of the file in the browser window file name box… and.. is there a way to stop the module(?) to open the browser window twice?

Thank you!!
Jan 13 '10 #1
Share this Question
Share on Google+
1 Reply


Expert Mod 2.5K+
P: 2,545
Hi. I can't comment on why your browser window is not showing the file selected (issue 1) as you are using Windows API calls to do so. There are other, simpler ways to do this that do not require API function calls (for example, using the built-in Office open/save file dialog).

For issue 2, the code in your sub calls the function twice in succession:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdBuildingAdd_Click() 
  2. Me!txtSelectedFile = BrowseFiles()  << first call
  3.    variable = BrowseFiles()  << second call
  4. End Sub
This should really just be
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdBuildingAdd_Click() 
  2. Me!txtSelectedFile = BrowseFiles() 
  3.    variable = Me!txtSelectedFile 
  4. End Sub
-Stewart
Jan 13 '10 #2

Post your reply

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