Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import Tables VBA

Status
Not open for further replies.

ryan1

Programmer
May 15, 2002
106
US
I'm trying to import tables, queries, rpts, mac, and mods from another database. I want to write over top of all previous tbls, qry, rpts, mac and mods. But do not delete anything that is not being written over. I would also like to be able to use a browse window to go and select the database i want to import from. I know this is a lot any help on any part of this would be very helpful.Thanks
 

OK. I’ll get you started. The following is a browse function that really works very nicely. Just copy and paste it into a new module. For an example of how it works, after you have compiled it, go into debug mode, <CNTL-G> and execute TestIt which will let you browse every text file on your system.

If you pay attention to what testit is doing you will be able to see how it should work in your application.

I’m sure someone else will help you with another step.

Here is the code:



OK. I’ll get you started. The following is a browse function that really works very nicely. Just copy and paste it into a new module. For an example of how it works, after you have compiled it, go into debug mode, <CNTL-G> and execute TestIt which will let you browse every text file on your system.

If you pay attention to what testit is doing you will be able to see how it should work in your application.

I’m sure someone else will help you with another step.

Here is the code:


Option Compare Database
Option Explicit

'***************** Code Start **************
'This code was originally written by Ken Getz.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
' Code courtesy of:
' Microsoft Access 95 How-To
' Ken Getz and Paul Litwin
' Waite Group Press, 1996

Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Declare Function aht_apiGetOpenFileName Lib &quot;comdlg32.dll&quot; _
Alias &quot;GetOpenFileNameA&quot; (OFN As tagOPENFILENAME) As Boolean

Declare Function aht_apiGetSaveFileName Lib &quot;comdlg32.dll&quot; _
Alias &quot;GetSaveFileNameA&quot; (OFN As tagOPENFILENAME) As Boolean
Declare Function CommDlgExtendedError Lib &quot;comdlg32.dll&quot; () As Long

Global Const ahtOFN_READONLY = &H1
Global Const ahtOFN_OVERWRITEPROMPT = &H2
Global Const ahtOFN_HIDEREADONLY = &H4
Global Const ahtOFN_NOCHANGEDIR = &H8
Global Const ahtOFN_SHOWHELP = &H10
' You won't use these.
'Global Const ahtOFN_ENABLEHOOK = &H20
'Global Const ahtOFN_ENABLETEMPLATE = &H40
'Global Const ahtOFN_ENABLETEMPLATEHANDLE = &H80
Global Const ahtOFN_NOVALIDATE = &H100
Global Const ahtOFN_ALLOWMULTISELECT = &H200
Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
Global Const ahtOFN_PATHMUSTEXIST = &H800
Global Const ahtOFN_FILEMUSTEXIST = &H1000
Global Const ahtOFN_CREATEPROMPT = &H2000
Global Const ahtOFN_SHAREAWARE = &H4000
Global Const ahtOFN_NOREADONLYRETURN = &H8000
Global Const ahtOFN_NOTESTFILECREATE = &H10000
Global Const ahtOFN_NONETWORKBUTTON = &H20000
Global Const ahtOFN_NOLONGNAMES = &H40000
' New for Windows 95
Global Const ahtOFN_EXPLORER = &H80000
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
Global Const ahtOFN_LONGNAMES = &H200000

Function TestIt()
Dim strFilter As String
Dim lngFlags As Long
' strFilter = ahtAddFilterItem(strFilter, &quot;Access Files (*.mda, *.mdb)&quot;, _
' &quot;*.MDA;*.MDB&quot;)
' strFilter = ahtAddFilterItem(strFilter, &quot;dBASE Files (*.dbf)&quot;, &quot;*.DBF&quot;)
strFilter = ahtAddFilterItem(strFilter, &quot;Text Files (*.txt)&quot;, &quot;*.TXT&quot;)
' strFilter = ahtAddFilterItem(strFilter, &quot;All Files (*.*)&quot;, &quot;*.*&quot;)
MsgBox &quot;You selected: &quot; & ahtCommonFileOpenSave(InitialDir:=&quot;C:\&quot;, _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:=&quot;Hello! Open Me!&quot;)
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Debug.Print Hex(lngFlags)
End Function

Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant
' Here's an example that gets an Access database name.
Dim strFilter As String
Dim lngFlags As Long
Dim varFileName As Variant
' Specify that the chosen file must already exist,
' don't change directories when you're done
' Also, don't bother displaying
' the read-only box. It'll only confuse people.
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = &quot;&quot;
End If
If IsMissing(varTitleForDialog) Then
varTitleForDialog = &quot;&quot;
End If

' Define the filter string and allocate space in the &quot;c&quot;
' string Duplicate this line with changes as necessary for
' more file templates.
strFilter = ahtAddFilterItem(strFilter, _
&quot;Access (*.mdb)&quot;, &quot;*.MDB;*.MDA&quot;)
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:=varTitleForDialog)
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
End If
GetOpenFile = varFileName
End Function

Function ahtCommonFileOpenSave( _
Optional ByRef Flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal Filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal FileName As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant
' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
'
' In:
' Flags: one or more of the ahtOFN_* constants, OR'd together.
' InitialDir: the directory in which to first look
' Filter: a set of file filters, set up by calling
' AddFilterItem. See examples.
' FilterIndex: 1-based integer indicating which filter
' set to use, by default (1 if unspecified)
' DefaultExt: Extension to use if the user doesn't enter one.
' Only useful on file saves.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Out:
' Return Value: Either Null or the selected filename
Dim OFN As tagOPENFILENAME
Dim strFileName As String
Dim strFileTitle As String
Dim fResult As Boolean
' Give the dialog a caption title.
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = &quot;&quot;
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(Flags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = &quot;&quot;
If IsMissing(FileName) Then FileName = &quot;&quot;
If IsMissing(DialogTitle) Then DialogTitle = &quot;&quot;
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
If IsMissing(OpenFile) Then OpenFile = True
' Allocate string space for the returned strings.
strFileName = Left(FileName & String(256, 0), 256)
strFileTitle = String(256, 0)
' Set up the data structure before you call the function
With OFN
.lStructSize = Len(OFN)
.hwndOwner = hwnd
.strFilter = Filter
.nFilterIndex = FilterIndex
.strFile = strFileName
.nMaxFile = Len(strFileName)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with
' these options.
.hInstance = 0
'.strCustomFilter = &quot;&quot;
'.nMaxCustFilter = 0
.lpfnHook = 0
'New for NT 4.0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
End With
' This will pass the desired data structure to the
' Windows API, which will in turn it uses to display
' the Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(OFN)
Else
fResult = aht_apiGetSaveFileName(OFN)
End If

' The function call filled in the strFileTitle member
' of the structure. You'll have to write special code
' to retrieve that if you're interested.
If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = vbNullString
End If
End Function

Function ahtAddFilterItem(strFilter As String, _
strDescription As String, Optional varItem As Variant) As String
' Tack a new chunk onto the file filter.
' That is, take the old value, stick onto it the description,
' (like &quot;Databases&quot;), a null character, the skeleton
' (like &quot;*.mdb;*.mda&quot;) and a final null character.

If IsMissing(varItem) Then varItem = &quot;*.*&quot;
ahtAddFilterItem = strFilter & _
strDescription & vbNullChar & _
varItem & vbNullChar
End Function

Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
intPos = InStr(strItem, vbNullChar)
If intPos > 0 Then
TrimNull = Left(strItem, intPos - 1)
Else
TrimNull = strItem
End If
End Function
'************** Code End *****************


Although originally written for A95, it works very well with A2K2.
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
You can't import things from other databases and have them overwrite what's already there. You will have to remove them from the current database first.

This code uses a modified version of the &quot;DoTheyExist&quot; routine from mvps.org/access.

Use the code Robert gave you to get the path to the file you are importing then pass it to my code.
It's not been tested, but I can't see any obvious errors.
Let me know how it works.

Ben

Option Compare Database
Option Explicit

Sub OverwriteImport(DatabasePath As String)
Dim dbImport As DAO.Database, dbCurrent As DAO.Database
Dim tdf As DAO.TableDef, qdf As DAO.QueryDef
Dim i As Integer

Set dbCurrent = CurrentDb
Set dbImport = OpenDatabase(DatabasePath)

'ImportTables
For Each tdf In dbImport.TableDefs
If ObjectExists(&quot;Table&quot;, tdf.Name, dbCurrent) = True Then
DoCmd.SetWarnings False
DoCmd.DeleteObject acTable, tdf.Name
DoCmd.SetWarnings True
End If
DoCmd.TransferDatabase acImport, &quot;Microsoft Access&quot;, DatabasePath, _
acTable, tdf.Name, tdf.Name
Next tdf

'ImportQueries
For Each qdf In dbImport.QueryDefs
If ObjectExists(&quot;Query&quot;, qdf.Name, dbCurrent) = True Then
DoCmd.SetWarnings False
DoCmd.DeleteObject acQuery, qdf.Name
DoCmd.SetWarnings True
End If
DoCmd.TransferDatabase acImport, &quot;Microsoft Access&quot;, DatabasePath, _
acQuery, qdf.Name, qdf.Name
Next qdf

'ImportForms
For i = 0 To dbImport.Containers(&quot;Forms&quot;).Documents.Count - 1
If ObjectExists(&quot;Form&quot;, dbImport.Containers(&quot;Forms&quot;).Documents(i).Name, dbCurrent) = True Then
DoCmd.SetWarnings False
DoCmd.DeleteObject acForm, dbImport.Containers(&quot;Forms&quot;).Documents(i).Name
DoCmd.SetWarnings True
End If
DoCmd.TransferDatabase acImport, &quot;Microsoft Access&quot;, DatabasePath, _
acMacro, dbImport.Containers(&quot;Forms&quot;).Documents(i).Name, dbImport.Containers(&quot;Forms&quot;).Documents(i).Name
Next i

'ImportReports
For i = 0 To dbImport.Containers(&quot;Reports&quot;).Documents.Count - 1
If ObjectExists(&quot;Form&quot;, dbImport.Containers(&quot;Reports&quot;).Documents(i).Name, dbCurrent) = True Then
DoCmd.SetWarnings False
DoCmd.DeleteObject acForm, dbImport.Containers(&quot;Reports&quot;).Documents(i).Name
DoCmd.SetWarnings True
End If
DoCmd.TransferDatabase acImport, &quot;Microsoft Access&quot;, DatabasePath, _
acMacro, dbImport.Containers(&quot;Reports&quot;).Documents(i).Name, dbImport.Containers(&quot;Reports&quot;).Documents(i).Name
Next i

'ImportModules
For i = 0 To dbImport.Containers(&quot;Modules&quot;).Documents.Count - 1
If ObjectExists(&quot;Form&quot;, dbImport.Containers(&quot;Modules&quot;).Documents(i).Name, dbCurrent) = True Then
DoCmd.SetWarnings False
DoCmd.DeleteObject acForm, dbImport.Containers(&quot;Modules&quot;).Documents(i).Name
DoCmd.SetWarnings True
End If
DoCmd.TransferDatabase acImport, &quot;Microsoft Access&quot;, DatabasePath, _
acMacro, dbImport.Containers(&quot;Modules&quot;).Documents(i).Name, dbImport.Containers(&quot;Modules&quot;).Documents(i).Name
Next i

'ImportMacros
For i = 0 To dbImport.Containers(&quot;Scripts&quot;).Documents.Count - 1
If ObjectExists(&quot;Macro&quot;, dbImport.Containers(&quot;Scripts&quot;).Documents(i).Name, dbCurrent) = True Then
DoCmd.SetWarnings False
DoCmd.DeleteObject acMacro, dbImport.Containers(&quot;Scripts&quot;).Documents(i).Name
DoCmd.SetWarnings True
End If
DoCmd.TransferDatabase acImport, &quot;Microsoft Access&quot;, DatabasePath, _
acMacro, dbImport.Containers(&quot;Scripts&quot;).Documents(i).Name, dbImport.Containers(&quot;Scripts&quot;).Documents(i).Name
Next i

End Sub

Function ObjectExists(ObjectType As String, strObjectName As String, db As DAO.Database) As Boolean

Dim tbl As TableDef
Dim qry As QueryDef
Dim i As Integer


ObjectExists = False

If ObjectType = &quot;Table&quot; Then
For Each tbl In db.TableDefs
If tbl.Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next tbl
ElseIf ObjectType = &quot;Query&quot; Then
For Each qry In db.QueryDefs
If qry.Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next qry
ElseIf ObjectType = &quot;Form&quot; Or ObjectType = &quot;Report&quot; Or ObjectType = &quot;Module&quot; Then
For i = 0 To db.Containers(ObjectType & &quot;s&quot;).Documents.Count - 1
If db.Containers(ObjectType & &quot;s&quot;).Documents(i).Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
ElseIf ObjectType = &quot;Macro&quot; Then
For i = 0 To db.Containers(&quot;Scripts&quot;).Documents.Count - 1
If db.Containers(&quot;Scripts&quot;).Documents(i).Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
Else
MsgBox &quot;Invalid Object Type passed, must be Table, Query, Form, Report, Macro, or Module&quot;
End If

End Function
----------------------------------------
Ben O'Hara
----------------------------------------
 
Thanks Robert the browse function works awesome. I'm getting ready to try Ben's now. Thanks Guy's
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top