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

Programatically Populate New Database With Tables

Using code in MyDateBase I create a new database named MyNewDatabase. Now I
need to import (or export) using code in MyDatabase tables in another
external database named MyData to MyNewDatabase. Is there a way to do it?
What is the code?

Thanks,

Steve
Nov 13 '05 #1
3 2184
I work from a table created mainly from the documenter. The table and fields
are
ztblTempStructure
TableName
FieldName
FieldType
FieldSize
Indexed
PrimaryKey

And the code is:

Function BldTempTables() As Boolean
'================================================= ===========
' Programmer: Duane Hookom
' Revision #:
' Called From:
' Date: 7/5/01
' Parameters:
'================================================= ===========
On Error GoTo BldTempTables_Err
Dim strErrMsg As String 'For Error Handling

'Dim the objects
Dim dbThis As DAO.Database
Dim dbTemp As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim ndx As DAO.Index
Dim rsStruct As DAO.Recordset 'the struct table

'Dim the variables
Dim strFolder As String 'the folder this application is located
in
Dim strThisDBName As String 'the name of this MDB
Dim strTempDBName As String 'The name of the temp mdb
Dim strTableName As String 'the table name

Set dbThis = CurrentDb
strThisDBName = dbThis.Name
strFolder = Left(strThisDBName, Len(strThisDBName) -
Len(Dir(strThisDBName)))
strTempDBName = strFolder & "PrdRptTemp.MDB"
On Error Resume Next
Kill strTempDBName 'if the old one exists, delete it
On Error GoTo BldTempTables_Err
'Create the new empty database
Set dbTemp = CreateDatabase(strTempDBName, dbLangGeneral)
Set rsStruct = dbThis.OpenRecordset("Select TableName, FieldName,
FieldType, FieldSize, Indexed " & _
"FROM ztblTempStructure ORDER BY TableName")
With rsStruct
If Not .EOF Then
.MoveFirst
Do Until .EOF
strTableName = !TableName
Set tdf = dbTemp.CreateTableDef(strTableName)
Do Until !TableName <> strTableName
Select Case !FieldType
Case dbText
Set fld = tdf.CreateField(!FieldName,
!FieldType, !FieldSize)
fld.AllowZeroLength = True
Case Else
Set fld = tdf.CreateField(!FieldName,
!FieldType)
End Select

tdf.Fields.Append fld
tdf.Fields.Refresh
.MoveNext
If .EOF Then
Exit Do
End If
Loop
dbTemp.TableDefs.Append tdf
dbTemp.TableDefs.Refresh

Loop
End If
.Close
End With

'Create the indexes
Set rsStruct = dbThis.OpenRecordset("Select TableName, FieldName,
FieldType, Indexed, PrimaryKey " & _
"FROM ztblTempStructure WHERE Indexed = -1 OR PrimaryKey = -1
ORDER BY TableName")
With rsStruct
.MoveFirst
If Not .EOF Then
.MoveFirst
Do Until .EOF
Set tdf = dbTemp.TableDefs(!TableName)
'Debug.Print tdf.Name
strTableName = !TableName
Do Until !TableName <> strTableName
'Debug.Print "-" & !FieldName
Set ndx = tdf.CreateIndex(!FieldName)
Set fld = ndx.CreateField(!FieldName, !FieldType)
ndx.Fields.Append fld
'set up the primary key field.
If !PrimaryKey = True Then
ndx.Primary = True
End If
tdf.Indexes.Append ndx
tdf.Indexes.Refresh
.MoveNext
If .EOF Then
Exit Do
End If
Loop
Loop
End If
.Close
End With
Set rsStruct = dbThis.OpenRecordset("Select Distinct TableName From
ztblTempStructure")
'relink the tables
With rsStruct
.MoveFirst
Do Until .EOF
DoCmd.DeleteObject acTable, !TableName
DoCmd.TransferDatabase acLink, "Microsoft Access",
strTempDBName, acTable, !TableName, !TableName
dbThis.TableDefs.Refresh
.MoveNext
Loop
.Close
End With
Set rsStruct = Nothing
Set dbThis = Nothing
Set dbTemp = Nothing
BldTempTables = True

BldTempTables_Exit:
Exit Function

BldTempTables_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) &
vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description
MsgBox strErrMsg, vbInformation, "BldTempTables"
BldTempTables = False
Resume BldTempTables_Exit
End Select
End Function
--
Duane Hookom
MS Access MVP
"PC Datasheet" <no****@nospam.spam> wrote in message
news:_O*****************@newsread1.news.atl.earthl ink.net...
Using code in MyDateBase I create a new database named MyNewDatabase. Now
I need to import (or export) using code in MyDatabase tables in another
external database named MyData to MyNewDatabase. Is there a way to do it?
What is the code?

Thanks,

Steve

Nov 13 '05 #2
Duane,

Thanks for your response!

I'm hoping to find code using TransferDatabase that can be run from
MyDateBase to import tables from MyData into MyNewDatabase.

Steve
"Duane Hookom" <duanehookom@NO_SPAMhotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP14.phx.gbl...
I work from a table created mainly from the documenter. The table and
fields are
ztblTempStructure
TableName
FieldName
FieldType
FieldSize
Indexed
PrimaryKey

And the code is:

Function BldTempTables() As Boolean
'================================================= ===========
' Programmer: Duane Hookom
' Revision #:
' Called From:
' Date: 7/5/01
' Parameters:
'================================================= ===========
On Error GoTo BldTempTables_Err
Dim strErrMsg As String 'For Error Handling

'Dim the objects
Dim dbThis As DAO.Database
Dim dbTemp As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim ndx As DAO.Index
Dim rsStruct As DAO.Recordset 'the struct table

'Dim the variables
Dim strFolder As String 'the folder this application is located
in
Dim strThisDBName As String 'the name of this MDB
Dim strTempDBName As String 'The name of the temp mdb
Dim strTableName As String 'the table name

Set dbThis = CurrentDb
strThisDBName = dbThis.Name
strFolder = Left(strThisDBName, Len(strThisDBName) -
Len(Dir(strThisDBName)))
strTempDBName = strFolder & "PrdRptTemp.MDB"
On Error Resume Next
Kill strTempDBName 'if the old one exists, delete it
On Error GoTo BldTempTables_Err
'Create the new empty database
Set dbTemp = CreateDatabase(strTempDBName, dbLangGeneral)
Set rsStruct = dbThis.OpenRecordset("Select TableName, FieldName,
FieldType, FieldSize, Indexed " & _
"FROM ztblTempStructure ORDER BY TableName")
With rsStruct
If Not .EOF Then
.MoveFirst
Do Until .EOF
strTableName = !TableName
Set tdf = dbTemp.CreateTableDef(strTableName)
Do Until !TableName <> strTableName
Select Case !FieldType
Case dbText
Set fld = tdf.CreateField(!FieldName,
!FieldType, !FieldSize)
fld.AllowZeroLength = True
Case Else
Set fld = tdf.CreateField(!FieldName,
!FieldType)
End Select

tdf.Fields.Append fld
tdf.Fields.Refresh
.MoveNext
If .EOF Then
Exit Do
End If
Loop
dbTemp.TableDefs.Append tdf
dbTemp.TableDefs.Refresh

Loop
End If
.Close
End With

'Create the indexes
Set rsStruct = dbThis.OpenRecordset("Select TableName, FieldName,
FieldType, Indexed, PrimaryKey " & _
"FROM ztblTempStructure WHERE Indexed = -1 OR PrimaryKey = -1
ORDER BY TableName")
With rsStruct
.MoveFirst
If Not .EOF Then
.MoveFirst
Do Until .EOF
Set tdf = dbTemp.TableDefs(!TableName)
'Debug.Print tdf.Name
strTableName = !TableName
Do Until !TableName <> strTableName
'Debug.Print "-" & !FieldName
Set ndx = tdf.CreateIndex(!FieldName)
Set fld = ndx.CreateField(!FieldName, !FieldType)
ndx.Fields.Append fld
'set up the primary key field.
If !PrimaryKey = True Then
ndx.Primary = True
End If
tdf.Indexes.Append ndx
tdf.Indexes.Refresh
.MoveNext
If .EOF Then
Exit Do
End If
Loop
Loop
End If
.Close
End With
Set rsStruct = dbThis.OpenRecordset("Select Distinct TableName From
ztblTempStructure")
'relink the tables
With rsStruct
.MoveFirst
Do Until .EOF
DoCmd.DeleteObject acTable, !TableName
DoCmd.TransferDatabase acLink, "Microsoft Access",
strTempDBName, acTable, !TableName, !TableName
dbThis.TableDefs.Refresh
.MoveNext
Loop
.Close
End With
Set rsStruct = Nothing
Set dbThis = Nothing
Set dbTemp = Nothing
BldTempTables = True

BldTempTables_Exit:
Exit Function

BldTempTables_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) &
vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description
MsgBox strErrMsg, vbInformation, "BldTempTables"
BldTempTables = False
Resume BldTempTables_Exit
End Select
End Function
--
Duane Hookom
MS Access MVP
"PC Datasheet" <no****@nospam.spam> wrote in message
news:_O*****************@newsread1.news.atl.earthl ink.net...
Using code in MyDateBase I create a new database named MyNewDatabase. Now
I need to import (or export) using code in MyDatabase tables in another
external database named MyData to MyNewDatabase. Is there a way to do it?
What is the code?

Thanks,

Steve


Nov 13 '05 #3
rkc
PC Datasheet wrote:
Duane,

Thanks for your response!

I'm hoping to find code using TransferDatabase that can be run from
MyDateBase to import tables from MyData into MyNewDatabase.


If you take a minute to read the helpfile on TransferDatabase
you will find that:

<quote>
You can use the TransferDatabase action to import or export data between
the current Microsoft Access database (.mdb) or Microsoft Access project
(.adp) and another database.
</quote>

So if you want to use TransferDatabase to move a table from one .mdb
file to another .mdb file, neither of which are the current database,
you will need to do it in two steps using the current database as a
middle man.

It shouldn't be too difficult to actually write that little bit of code
yourself.


Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: CoreyMas | last post by:
Hello Everyone, I have been successful in creating a template column programatically using the examples provided in VS 2003. However I have not been able to programatically set the width of a...
3
by: Damian | last post by:
Hi. Is there a way to programatically populate a continuous form? I have an array of descriptions that I want to display in the continuous form and I have a textbox on the form called...
3
by: Tym | last post by:
OK - daft question of the day time... If I have database A which contains all the live data, and Database B which contains linked tables to those is A (i.e. a front end) is there a way of seeing...
8
by: Lumpierbritches | last post by:
Thank you in advance for any and all assistance, it is GREATLY appreciated. I was wondering if there is a way, with code to create a query for every table? Or can you use a naming convention in...
3
by: Yul | last post by:
Hi, We are in the process of designing an ASP.NET app, where a user will enter some 'Customer ID' to be queried in the database. If the ID is valid, several stored procedures will be called to...
3
by: crjunk | last post by:
I have 4 different databases that I'm having to pull data from in order to populate a datagrid. I am able to do this, but my problem is that because I'm pulling the data from 4 different...
5
by: Rich | last post by:
Hello, I have a search application to search data in tables in a database (3 sql server tables). I populate 2 comboboxes with with data from each table. One combobox will contain unique...
5
by: Ron | last post by:
Hi All, Development stage of setting up a new solution here. I CONSTANTLY have to unhook the relationships I've built via the Relationships tab on Tools, then change the Required element of a...
12
by: Andy B | last post by:
I need to custom build and use a dataset in c# to use with xml. Does anybody know where I can find out how to do something like this? I was going to create a class that generated the dataset and...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.