Results 1 to 3 of 3

Thread: Selecting a file Excel/Access

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    30

    Selecting a file Excel/Access

    i'm trying to automate the import of an excel spreadhseet into Access 2000. The following code does so and imports this into a table called 'New'. However as you can see i have put the full path of where the spreadsheet is to be located everytime a user wants to import it.

    What i need is this to be a dialog box that appears (on push of a button) where you can browse and choose the file wherever it may be located. I have tried adding a MS common dialog control but it says i do not have the activeX license, is there any other way around this?? Many thanks

    '''''''''''''''''''code
    DoCmd.TransferSpreadsheet acImport, , "New", _
    "C:\Documents and Settings\Nick Williams\My Documents\My Databases\SupplierParts.xls", _
    True, "A:C"
    '''''''''''''''''''''

  2. #2
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Selecting a file Excel/Access

    Sure:

    VB Code:
    1. Option Explicit
    2. '^*^*^*^*^*^*^*^*^*^*^*^*^*
    3. '> Folder/File browsing code module
    4. '^*^*^*^*^*^*^*^*^*^*^*^*^*
    5.  
    6. 'Retrieves the window handle to the active window associated with the thread that calls the function
    7. Private Declare Function apiGetActiveWindow& Lib "user32" Alias "GetActiveWindow" ()
    8.  
    9. 'Retrieves a handle to the specified child window's parent window
    10. Private Declare Function apiGetParent& Lib "user32" Alias "GetParent" _
    11.                         (ByVal hWnd As Long)
    12.  
    13. 'Retrieves the dimensions of the bounding rectangle of the specified window
    14. Private Declare Function GetWindowRect& Lib "user32" _
    15.                         (ByVal hWnd As Long, _
    16.                          lpRect As RECT)
    17.  
    18. 'Retrieves various system metrics (in pixels)
    19. Private Declare Function GetSystemMetrics& Lib "user32" _
    20.                         (ByVal nIndex As Long)
    21.  
    22. 'Changes the position and dimensions of the specified window
    23. Private Declare Function MoveWindow& Lib "user32" _
    24.                         (ByVal hWnd As Long, _
    25.                          ByVal x As Long, _
    26.                          ByVal y As Long, _
    27.                          ByVal nWidth As Long, _
    28.                          ByVal nHeight As Long, _
    29.                          ByVal bRepaint As Long)
    30.  
    31. 'Appends one string to another
    32. Private Declare Function lstrcat& Lib "kernel32.dll" Alias "lstrcatA" _
    33.                         (ByVal lpString1 As String, _
    34.                          ByVal lpString2 As String)
    35.                          
    36. 'Displays a dialog box enabling the user to select a Shell folder
    37. Private Declare Function SHBrowseForFolder& Lib "shell32.dll" _
    38.                         (lpbi As BrowseInfo)
    39.                        
    40. 'Converts an item identifier list to a file system path
    41. Private Declare Function SHGetPathFromIDList& Lib "shell32.dll" _
    42.                         (ByVal pidList As Long, _
    43.                          ByVal lpBuffer As String)
    44.                          
    45. 'Retrieves a pointer to the ITEMIDLIST structure of a special folder
    46. Private Declare Function SHGetSpecialFolderLocation& Lib "shell32.dll" _
    47.                         (ByVal hWndOwner As Long, _
    48.                          ByVal nFolder As Long, _
    49.                          ppidl As Long)
    50.                          
    51. 'Retrieves a handle to a window whose class name and window name match the specified strings
    52. Private Declare Function FindWindowEx& Lib "user32" Alias "FindWindowExA" _
    53.                         (ByVal hWnd1 As Long, _
    54.                          ByVal hWnd2 As Long, _
    55.                          ByVal lpsz1 As String, _
    56.                          ByVal lpsz2 As String)
    57.                          
    58. 'Retrieves the identifier of the thread that created the specified window
    59. Private Declare Function GetWindowThreadProcessId& Lib "user32" _
    60.                         (ByVal hWnd As Long, _
    61.                          ByRef lpdwProcessId As Long)
    62.                          
    63. 'Retrieves the process identifier of the calling process
    64. Private Declare Function GetCurrentProcessId& Lib "kernel32" ()
    65.  
    66. 'Returns a handle to the desktop window
    67. Private Declare Function GetDesktopWindow& Lib "user32" ()
    68.  
    69. 'Frees a block of task memory
    70. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    71.  
    72. 'Parameters for the dialog centering
    73. Public Type RECT
    74.         Left As Long
    75.         Top As Long
    76.         Right As Long
    77.         Bottom As Long
    78. End Type
    79.  
    80. 'Parameters for the SHBrowseForFolder function
    81. Private Type BrowseInfo
    82.     hWndOwner As Long
    83.     pIDLRoot As Long
    84.     pszDisplayName As Long
    85.     lpszTitle As Long
    86.     ulFlags As Long
    87.     lpfnCallback As Long
    88.     lParam As Long
    89.     iImage As Long
    90. End Type
    91.  
    92. 'Special Folder Types
    93. Public Enum FolderType
    94.     AppData = &H1A
    95.     BitBucket = &HA
    96.     CommonDesktopDirectory = &H19
    97.     CommonDocuments = &H2E
    98.     CommonFavorites = &H1F
    99.     CommonPrograms = &H17
    100.     CommonStartMenu = &H16
    101.     CommonStartup = &H18
    102.     CommonTemplates = &H2D
    103.     CommonAltStartup = &H1E
    104.     CommonAppData = &H23
    105.     CommonDesktop = &H0
    106.     CommonMyMusic = &H35
    107.     CommonMyPictures = &H36
    108.     CommonMyVideos = &H37
    109.     CommonStartAdmin = &H2F
    110.     Connections = &H31
    111.     Controls = &H3
    112.     Drives = &H11
    113.     Favorites = &H6
    114.     Fonts = &H14
    115.     LocalAltStartup = &H1D
    116.     LocalAppData = &H1C
    117.     LocalAppMSCDBurning = &H3B
    118.     LocalCookies = &H21
    119.     LocalDesktop = &H10
    120.     LocalHistory = &H22
    121.     LocalInternetCache = &H20
    122.     LocalMyVideos = &HE
    123.     LocalStartAdmin = &H30
    124.     MSHome = &H3D
    125.     MyMusic = &HD
    126.     MyPictures = &H27
    127.     NetHood = &H13
    128.     Network = &H12
    129.     Personal = &H5
    130.     Printers = &H4
    131.     Printhood = &H1B
    132.     Profile = &H28
    133.     ProgramFiles = &H26
    134.     CommonProgramFiles = &H2B
    135.     CommonX86ProgramFiles = &H2C
    136.     X86ProgramFiles = &H2A
    137.     Programs = &H2
    138.     Recent = &H8
    139.     Resources = &H39
    140.     SendTo = &H9
    141.     StartMenu = &HB
    142.     Startup = &H7
    143.     System = &H25
    144.     SystemX86 = &H29
    145.     Templates = &H15
    146.     Windows = &H24
    147. End Enum
    148.  
    149. 'What to browse for
    150. Public Enum BrowseType
    151.     BrowseForComputers = &H1000
    152.     BrowseForPrinters = &H2000
    153.     BrowseForFiles = &H4000
    154.     BrowseForFilesCreateOption = &H4040
    155.     BrowseForFoldersWithEditBox = &H10
    156.     BrowseForFoldersCreateOption = &H40
    157.     BrowseForFolders = &H1
    158. End Enum
    159.  
    160. 'Screen metric constants
    161. Private Const SM_CXFULLSCREEN = &H10
    162. Private Const SM_CYFULLSCREEN = &H11
    163.  
    164. 'Dialog fully initiallized constant
    165. Private Const BFFM_INITIALIZED = &H1
    166.  
    167. 'Maximum character length of path
    168. Private Const MAX_PATH  As Long = 260
    169.  
    170. Private Function GetAccesshWnd()
    171.     Dim hWnd As Long
    172.     Dim hWndAccess As Long
    173.  
    174.     ' Get the handle to the currently active window.
    175.     hWnd = apiGetActiveWindow()
    176.     hWndAccess = hWnd
    177.  
    178.     ' Find the top window (which has no parent window).
    179.     While hWnd <> 0
    180.         hWndAccess = hWnd
    181.         hWnd = apiGetParent(hWnd)
    182.     Wend
    183.  
    184.     GetAccesshWnd = hWndAccess
    185. End Function
    186.  
    187. Private Function BrowseCallBackFunc(ByVal hWnd As Long, ByVal lMsg As Long, ByVal lParam As Long, ByVal pData As Long) As Long
    188. '   Windows calls this function when the dialog events occur
    189.     Select Case lMsg
    190.         Case BFFM_INITIALIZED
    191.             CenterDialog hWnd
    192.     End Select
    193.    
    194.     'Allow the dialog to close
    195.     BrowseCallBackFunc = 0
    196. End Function
    197.  
    198. Private Function BrowseCallBackFuncAddress() As Long
    199.     BrowseCallBackFuncAddress = Long2Long(AddressOf BrowseCallBackFunc)
    200. End Function
    201.  
    202. Private Function Long2Long(x As Long) As Long
    203.     'Explicitly convert a udt to a long
    204.     Long2Long = x
    205. End Function
    206.  
    207. Private Function fValidatePath(ByVal szFullPath As String) As String
    208.     'Check the folder path and add a seperator if necessary
    209.     Select Case Right$(szFullPath, 1)
    210.         Case ""
    211.             Exit Function
    212.         Case "\"
    213.             fValidatePath = szFullPath
    214.         Case Else
    215.             fValidatePath = szFullPath & "\"
    216.     End Select
    217. End Function
    218.  
    219. Private Sub CenterDialog(hWnd As Long)
    220.     'Used to center the dialog on the screen
    221.     Dim WinRect As RECT
    222.     Dim ScrWidth As Integer
    223.     Dim ScrHeight As Integer
    224.     Dim DlgWidth As Integer
    225.     Dim DlgHeight As Integer
    226.    
    227.     GetWindowRect hWnd, WinRect
    228.     DlgWidth = WinRect.Right - WinRect.Left
    229.     DlgHeight = WinRect.Bottom - WinRect.Top
    230.     ScrWidth = GetSystemMetrics(SM_CXFULLSCREEN)
    231.     ScrHeight = GetSystemMetrics(SM_CYFULLSCREEN)
    232.     MoveWindow hWnd, (ScrWidth - DlgWidth) / 2, (ScrHeight - DlgHeight) / 2, DlgWidth, DlgHeight, 1
    233. End Sub
    234.  
    235. Public Function fBrowseFor(ByVal BrowseFor As BrowseType, _
    236.                                ByVal RootFolder As FolderType, _
    237.                                Optional bCenter As Boolean) As String
    238.     Dim iNullpos As Integer
    239.     Dim lpIDList As Long
    240.     Dim lres As Long
    241.     Dim szPath As String
    242.     Dim BInfo As BrowseInfo
    243.     Dim lRootID As Long
    244.    
    245.    
    246.     'Foolproof way to find the main Access window handle
    247.     Dim AppWnd As Long
    248.   '  If Val(Application.Version) >= 10 Then
    249.    '     AppWnd = Application.hWndAccessApp
    250.    ' Else
    251.         AppWnd = GetAccesshWnd()
    252.    ' End If
    253.        
    254.     'Retrieve special folder locations
    255.     SHGetSpecialFolderLocation AppWnd, RootFolder, lRootID
    256.     With BInfo
    257.         .hWndOwner = AppWnd
    258.         If bCenter Then .lpfnCallback = BrowseCallBackFuncAddress
    259.         .ulFlags = BrowseFor
    260.        
    261.        
    262.         'Determine our browse title
    263.         Select Case BrowseFor
    264.             Case 1
    265.                 .lpszTitle = lstrcat("Select a Folder", "")
    266.             Case 64
    267.                 .lpszTitle = lstrcat("Select a Folder", "")
    268.             Case 4096
    269.                 .lpszTitle = lstrcat("Select a Computer", "")
    270.             Case 8192
    271.                 .lpszTitle = lstrcat("Select a Printer", "")
    272.             Case Else
    273.                 .lpszTitle = lstrcat("Select a File", "")
    274.         End Select
    275.     End With
    276.  
    277.     If lRootID <> 0 Then BInfo.pIDLRoot = lRootID
    278.         lpIDList = SHBrowseForFolder(BInfo)
    279.  
    280.     If lpIDList <> 0 Then
    281.         szPath = String(MAX_PATH, 0)
    282.         lres = SHGetPathFromIDList(lpIDList, szPath)
    283.         Call CoTaskMemFree(lpIDList)
    284.         iNullpos = InStr(szPath, vbNullChar)
    285.         If iNullpos <> 0 Then
    286.             szPath = Left$(szPath, iNullpos - 1)
    287.         End If
    288.     End If
    289.  
    290.     'Browsing for files allow folders to be selected also
    291.     'We check the selected item to see if it is a file or not
    292.     If Not Mid$(Right$(szPath, 4), 1, 1) = "." Then
    293.         fBrowseFor = fValidatePath(szPath)
    294.     Else
    295.         fBrowseFor = szPath
    296.     End If
    297. End Function


    Then incorporate like this:

    VB Code:
    1. Dim szFilePicked As String
    2.     szFilePicked = fBrowseFor(BrowseForFiles, Drives, True)
    3.    
    4.     DoCmd.TransferSpreadsheet acImport, , "New", szFilePicked, True, "A:C"
    Justin Labenne
    www.jlxl.net

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    30

    Re: Selecting a file Excel/Access

    That is genious my friend.. it works great, your a legend.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width