473,414 Members | 1,618 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,414 software developers and data experts.

Linking tables; add filter extension (*.mdb) in a custom open command dialog box

Dear experts,

I got this below code to link to database. I use it as an on click command button to prompt for database to link.

Since it is a code which I didn't create from the beginning (its a modified copy I got from a friend), I would like to know how to add filter extension "*.mdb" (not just *.accdb) in the dialog box.

I've noticed that it must have something to do with the :
  • Public Function funOpenCommDlg (...)
  • Public Function LinkTableMain()
  • sInputFile = funOpenCommDlg("Access Database (*.accdb)|*.accdb", "Select Database to Link ", "", "*.accdb", True)

But I have no idea of how to modify the code. Could you please help me on this?

Many thanks in advance.

This is the code:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Type OpenFilename
  5.     lStructSize As Long
  6.     hwndOwner As Long
  7.     hInstance As Long
  8.     lpstrFilter As String
  9.     lpstrCustomFilter As Long
  10.     nMaxCustFilter As Long
  11.     iFilterIndex As Long
  12.     lpstrFile As String
  13.     nMaxFile As Long
  14.     lpstrFileTitle As String
  15.     nMaxFileTitle As Long
  16.     lpstrInitialDir As String
  17.     lpstrTitle As String
  18.     Flags As Long
  19.     nFileOffset As Integer
  20.     nFileExtension As Integer
  21.     lpstrDefExt As String
  22.     lCustData As Long
  23.     lpfnHook As Long
  24.     lpTemplateName As String
  25. End Type
  26.  
  27. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OpenFilename) As Long
  28. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  29. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOPENFILENAME As OpenFilename) As Long
  30.  
  31. Private Const OFN_READONLY = &H1
  32. Private Const OFN_OVERWRITEPROMPT = &H2
  33. Private Const OFN_HIDEREADONLY = &H4
  34. Private Const OFN_NOCHANGEDIR = &H8
  35. Private Const OFN_SHOWHELP = &H10
  36. Private Const OFN_ENABLEHOOK = &H20
  37. Private Const OFN_ENABLETEMPLATE = &H40
  38. Private Const OFN_ENABLETEMPLATEHANDLE = &H80
  39. Private Const OFN_NOVALIDATE = &H100
  40. Private Const OFN_ALLOWMULTISELECT = &H200
  41. Private Const OFN_EXTENSIONDIFFERENT = &H400
  42. Private Const OFN_PATHMUSTEXIST = &H800
  43. Private Const OFN_FILEMUSTEXIST = &H1000
  44. Private Const OFN_CREATEPROMPT = &H2000
  45. Private Const OFN_SHAREAWARE = &H4000
  46. Private Const OFN_NOREADONLYRETURN = &H8000
  47. Private Const OFN_NOTESTFILECREATE = &H10000
  48. Private Const OFN_NONETWORKBUTTON = &H20000
  49. Private Const OFN_NOLONGNAMES = &H40000                      '  force no long names for 4.x modules
  50. Private Const OFN_EXPLORER = &H80000                         '  new look commdlg
  51. Private Const OFN_NODEREFERENCELINKS = &H100000
  52. Private Const OFN_LONGNAMES = &H200000                       '  force long names for 3.x modules
  53. Private Const OFN_SHAREFALLTHROUGH = 2
  54. Private Const OFN_SHARENOWARN = 1
  55. Private Const OFN_SHAREWARN = 0
  56.  
  57.  
  58. Public Function funOpenCommDlg(ByVal sFilter As String, ByVal sDlgTitle As String, ByVal sDir As String, ByVal sDefExt As String, ByVal bMustExist As Boolean, Optional bMulti As Boolean = False) As String
  59.  
  60. Dim sFullName As String, sFileName As String
  61. Dim lResult As Long, lFlags As Long, i As Integer
  62. Dim uFileDlgData As OpenFilename
  63.  
  64. ' Define the filter string, converting all "|" to nulls
  65. sFilter = funSubstitute(sFilter, "|", Chr$(0))
  66.  
  67. ' Allocate string space for the returned strings.
  68. sFullName = Space$(25400)
  69. sFileName = Space$(25400)
  70.  
  71. lFlags = OFN_HIDEREADONLY Or OFN_EXPLORER
  72. 'Or OFN_NOCHANGEDIR
  73.  
  74. If bMustExist Then lFlags = lFlags Or OFN_FILEMUSTEXIST
  75. If bMulti Then lFlags = lFlags Or OFN_ALLOWMULTISELECT
  76. ' Set up the data structure before you call the GetOpenFilename
  77. With uFileDlgData
  78.     .hwndOwner = Application.hWndAccessApp
  79.     .lpstrFilter = sFilter
  80.     .iFilterIndex = 1
  81.     .lpstrFile = sFullName & Chr$(0)
  82.     .nMaxFile = Len(sFullName) + 1
  83.     .lpstrFileTitle = sFileName & Chr$(0)
  84.     .nMaxFileTitle = Len(sFileName) + 1
  85.     .lpstrTitle = sDlgTitle
  86.     .Flags = lFlags
  87.     .lpstrDefExt = sDefExt
  88.     .hInstance = 0
  89.     .lpstrCustomFilter = 0&
  90.     .nMaxCustFilter = 0
  91.     .lpstrInitialDir = sDir
  92.     .nFileOffset = 0
  93.     .nFileExtension = 0
  94.     .lCustData = 0
  95.     .lpfnHook = 0
  96.     .lpTemplateName = ""
  97.     .lStructSize = Len(uFileDlgData)
  98. End With
  99.  
  100. ' This will pass the desired data structure to the Windows API,
  101. ' which will in turn use it to display the Open Dialog form.
  102. lResult = GetOpenFileName(uFileDlgData)
  103.  
  104. ' Return the file selected
  105. If lResult = 0 Then
  106.     funOpenCommDlg = ""
  107. Else
  108.     If bMulti Then
  109.         funOpenCommDlg = uFileDlgData.lpstrFile
  110.     Else
  111.  
  112.         funOpenCommDlg = Left(uFileDlgData.lpstrFile, InStr(uFileDlgData.lpstrFile, vbNullChar) - 1)
  113.     End If
  114. End If
  115.  
  116. End Function
  117.  
  118. Private Function funSubstitute(ByVal sString As String, ByVal sFind As String, ByVal sReplace As String)
  119.  
  120. Dim i As Integer, sTmp As String
  121.  
  122. For i = 1 To Len(sString)
  123.     If Mid(sString, i, 1) = "|" Then
  124.         sTmp = sTmp & Chr$(0)
  125.     Else
  126.         sTmp = sTmp & Mid(sString, i, 1)
  127.     End If
  128. Next
  129.  
  130. funSubstitute = sTmp
  131.  
  132. End Function
  133.  
  134. Function SaveAsCommDlg(ByVal sFilter As String, ByVal sDlgTitle As String, ByVal sDir As String, ByVal sDefExt As String, Optional ByVal sDefName As String = "") As String
  135.  
  136. Dim sFullName As String, sFileName As String
  137. Dim lResult As Long, lFlags As Long, i As Integer
  138. Dim uFileDlgData As OpenFilename
  139.  
  140. sFilter = funSubstitute(sFilter, "|", Chr$(0))
  141. sFullName = sDefName & Space$(254 - Len(sDefName))
  142. sFileName = Space$(254)
  143.  
  144. lFlags = OFN_PATHMUSTEXIST Or OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY
  145.  
  146. With uFileDlgData
  147.    ' .hwndOwner = FindWindow("XLMAIN", Application.Caption)
  148.     .lpstrFilter = sFilter
  149.     .iFilterIndex = 1
  150.     .lpstrFile = sFullName & Chr$(0)
  151.     .nMaxFile = Len(sFullName) + 1
  152.     .lpstrFileTitle = sFileName & Chr$(0)
  153.     .nMaxFileTitle = Len(sFileName) + 1
  154.     .lpstrTitle = sDlgTitle
  155.     .Flags = lFlags
  156.     .lpstrDefExt = sDefExt
  157.     .hInstance = 0
  158.     .lpstrCustomFilter = 0&
  159.     .nMaxCustFilter = 0
  160.     .lpstrInitialDir = sDir
  161.     .nFileOffset = 0
  162.     .nFileExtension = 0
  163.     .lCustData = 0
  164.     .lpfnHook = 0
  165.     .lpTemplateName = ""
  166.     .lStructSize = Len(uFileDlgData)
  167. End With
  168.  
  169. lResult = GetSaveFileName(uFileDlgData)
  170.  
  171. ' Return the file selected
  172. If lResult = 0 Then
  173.     SaveAsCommDlg = ""
  174. Else
  175.     SaveAsCommDlg = Left(uFileDlgData.lpstrFile, InStr(uFileDlgData.lpstrFile, vbNullChar) - 1)
  176. End If
  177.  
  178. End Function
  179.  
  180. Public Function LinkTableMain()
  181. Dim sInputFile As String
  182. Dim tblObj As TableDef, sTableName As String
  183. Dim wsp As Workspace, dbsInput As Database, tdf As TableDef
  184. Dim iReturn As Integer
  185. sInputFile = funOpenCommDlg("Access Database (*.accdb)|*.accdb", "Select Database to Link ", "", "*.accdb", True)
  186.  
  187. If sInputFile <> "" Then
  188.     Set wsp = DBEngine.Workspaces(0)
  189.     ' Return reference to Another.mdb.
  190.     Set dbsInput = wsp.OpenDatabase(sInputFile)
  191.  
  192.     For Each tblObj In dbsInput.TableDefs
  193.         If (tblObj.Attributes And dbSystemObject) = 0 And tblObj.Name <> "Var" And tblObj.Name <> "Repetitive" _
  194.         And Left((tblObj.Name), 4) <> "MSys" _
  195.         Then
  196.             sTableName = tblObj.Name
  197.  
  198.             iReturn = SysCmd(acSysCmdSetStatus, "Linking Table " & sTableName & ", please wait...")
  199.             'Remove existng link
  200.             On Error Resume Next
  201.             CurrentDb.TableDefs.Delete sTableName
  202.             'On Error GoTo 0
  203.                 'Remove data for the same date
  204.             Set tdf = CurrentDb.CreateTableDef(sTableName)
  205.  
  206.             tdf.Connect = ";Database=" & sInputFile
  207.             tdf.SourceTableName = sTableName
  208.             CurrentDb.TableDefs.Append tdf
  209.  
  210.         End If
  211.     Next
  212.  
  213.     dbsInput.Close
  214.     Set dbsInput = Nothing
  215.     Set wsp = Nothing
  216.     Set tdf = Nothing
  217.     iReturn = SysCmd(acSysCmdClearStatus)
  218.  
  219. ElseIf sInputFile = "" Then Exit Function
  220. End If
  221.  On Error GoTo 0
  222.  
  223. Set tblObj = Nothing
  224.  
  225. End Function
  226.  
Jun 25 '13 #1
5 1538
zmbd
5,501 Expert Mod 4TB
Go to the bottom of this page
Click on the Microsoft Access / VBA Insights Sitemap
Read article 29
Should put you on the "right track"
Jun 25 '13 #2
@zmbd
Nope it doesn't. Sorry...
The title of article 29 is:
"recover deleted records in MS access database" which has nothing to do with linked database...
Jun 26 '13 #3
zmbd
5,501 Expert Mod 4TB
You did not click on the "Microsoft Access / VBA Insights Sitemap" link at the bottom of this page.

Instead it appears that you may have have clicked on:
" Microsoft Access / VBA Answers Sitemap"

If you will do so again, you'll find that the "Recover Deleted..." is (as of 07:04CST) Entry #30 and more than likely has dropped down the listing a tad.

However if you had clicked on the Microsoft Access / VBA Insights Sitemap link at the bottom of this thread page you would have been taken the list where would have seen:




I'll provide the link here: 29. Select a File or Folder using the FileDialog Object
Attached Images
File Type: jpg Insight_29.JPG (30.2 KB, 328 views)
Jun 26 '13 #4
Got it. I modified some lines and found it working well. Thanks.

@zmbd
Jun 28 '13 #5
zmbd
5,501 Expert Mod 4TB
I'm glad that worked!
NeoPa did a bang-up-job (IMHO) with one of the clearest, cleanest codes and explantion that I've seen for dialogs.
I've seen other codes that just appear to go on and on and on and no real explanation about what is going on and on and on.

TTFN
Jun 28 '13 #6

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

Similar topics

1
by: Venkat | last post by:
Hi, I have an file1.exe file stored at some physical location say C:\Test\file1.exe. I created a virtual folder say MyFolder pointing to my physical folder(C:\Test\). I wrote this piece of...
18
by: Mark P | last post by:
I have a bit of vb code that uses Tables.Append to programatically link tables from Oracle and DB2 datasources. The problem I am having on some client machines is that the link will take anywhere...
9
by: mooseshoes | last post by:
All: I've set up a database using two separate files so that I can keep the data stable while playing around with the structure. The tables in the data file are linked into the structure file...
1
by: N. Graves | last post by:
Help me please? I'm have users import tables from other Access Database in one of the process needed to use my ADB. To do this the user opens a form with a field for the file directory and...
2
by: Matthew Wells | last post by:
Good morning... I have an Access front end that uses SQL Server linked tables. SQL Server uses Windows authentication. I have one Windows group that all Access users are a member of. I added...
0
by: Sam | last post by:
Sorry for this newbie question. I have an existing c++ project where I want to add a second extension to an open file dialog box. How can I do it In the resource file, in a String Table I have...
0
by: Dune | last post by:
Hi there, I have an aspx page that allows users to enter several parameters using drop downs and text boxes. The users then press a button that produces an extract based on the parameters they...
0
by: Me | last post by:
I have an application that has a text box. At the end on the text box is the standard elypsis (command button) for launching the Open File Dialog box. I want the user to select a certain executable...
2
by: Jeroen | last post by:
The group and search engines revealed many hints on how to use the 'Open With...' dialog, for example at: http://www.codeproject.com/csharp/openwith.asp Now in addition to a solution as...
2
by: =?Utf-8?B?U29tZXNo?= | last post by:
Hi Friends, I would like to show the Open With dialog of windows (the one which is shown when windows couldn't find the application to open the file) to the user on some specific event. Can...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.