 |
 |

07-29-2003, 08:40 AM
|
 |
Freshman
|
|
Join Date: Jul 2003
Location: Rivendell
Posts: 34
|
|
Automatic Creation of Access Database using VB
|
Is it possible to automatically create a new Access database (with all the fields already specified, etc.) through VB? For instance, clicking the command button "Create database" will automatically create and/or open an Access database with all the fields already specified. I would greatly appreciate any help you could give. Thank you.
|
|

07-29-2003, 03:18 PM
|
|
Centurion
|
|
Join Date: Apr 2003
Posts: 129
|
|
Quote: Originally Posted by justagirl Is it possible to automatically create a new Access database (with all the fields already specified, etc.) through VB? For instance, clicking the command button "Create database" will automatically create and/or open an Access database with all the fields already specified. I would greatly appreciate any help you could give. Thank you.
You can do it one of two ways:
Click here to see one way.
The other way, which would be much easier but might not be what you're looking for is to have an empty database that is setup with the fields that you want and make a copy of it whenever you need a new one (sort of like making a template).
Whenever you want to make a copy of it use this line:
Code:
FileSystem.FileCopy <source path>, <destination path>
|
|

07-31-2003, 12:16 PM
|
 |
Contributor
|
|
Join Date: Jul 2003
Location: Singapore
Posts: 686
|
|
May I suggest
May Be This helps...
Code:
Private Sub CreateNew(sDataNamePath As String)
'sDataNamePath is U'r new Database Name Full Path
Dim dbNew As DAO.Database
Dim tblNew As DAO.TableDef
Dim strTbName As String
On Error Goto Create_Err
Set dbNew = DAO.CreateDatabase(strDataNamePath, dbLangGeneral, dbVersion30)
strTbName = "NewTable"
Set tblNew = dbNew.CreateTableDef(strTbName)
tblNew.Fields.Append tblNew.CreateField("FieldID", dbLong, 4)
tblNew.Fields.Append tblNew.CreateField("Field1", dbText, 50)
tblNew.Fields.Append tblNew.CreateField("Field2", dbText, 50)
tblNew.Fields.Append tblNew.CreateField("Field3", dbText, 50)
tblNew.Fields.Append tblNew.CreateField("Field4", dbDate, 8)
tblNew.Fields(0).AllowZeroLength = False
tblNew.Fields(1).AllowZeroLength = True
tblNew.Fields(2).AllowZeroLength = True
tblNew.Fields(3).AllowZeroLength = True
tblNew.Fields(4).AllowZeroLength = True
dbDay.TableDefs.Append tblNew
dbNew.Close
Set dbNew = Nothing
Exit Sub
Create_Err:
MsgBox "Error Has Occured" & vbcr & _
Err.Number & vbcr & Err.Description, vbExclamation + vbOkonly,"Error"
Exit Sub
End Sub
|
__________________
I May Have Interpret It wrongly
Correct me..if I misunderstood U.
Hope This helps........and Enjoy Coding........//
zak2zak
|

08-04-2003, 06:53 AM
|
 |
Freshman
|
|
Join Date: Jul 2003
Location: Rivendell
Posts: 34
|
|
Follow up!
|
What if I wanted to create a database everytime I run the program? You see, I'm doing an archiving sort of program, i.e. what I wanted to do was to archive files from July 1 to July 10, this will be put into a database called DBjuly1to10.mdb; then when I run the program once more, I input July 11 to July 20, and the program should archive files of those dates to DBjuly11to20.mdb... Is it possible to auto-name files this way? How do I create a new database everytime (with the same fields and all)? Thanks heaps!
|
|

08-04-2003, 08:59 AM
|
 |
Contributor
|
|
Join Date: Jul 2003
Location: Singapore
Posts: 686
|
|
New Database Everytime
May I have more info,,
You specify that U need to create new database base on the date that you input. The ideal way I suggest is to have a database that is based on year
Example:
Database Name : DB2003.mdb
The new DBjuly1to10.mdb is good but I suggest that U create new Table instead
Example:
Table Name : tbljuly1to10
This will be archive in a Database of DB2003.mdb and is easier to differeciate between Database and searching of records will be base on Table Name instead of lots of Database.
Code:
'Create New Database
Dim CreateDatabase(strDataName As String)
Dim dbNew As DAO.Database
If Dir(strDataName)<>"" then
MsgBox "Database Exist"
Else
Set dbNew = DAO.CreateDatabase(strDataName, dbLangGeneral, dbVersion30)
dbNew.Close
Set dbNew = Nothing
End If
End Sub
'Use the CreateNew Sub Routine from my first reply to add to the newly created Database such as
Private Sub CreateNew(sDataNamePath As String, strTableName As String)
'sDataNamePath is U'r new Database Name Full Path
Dim dbNew As DAO.Database
Dim tblNew As DAO.TableDef
Dim tblCheck As DAO.TableDef
Dim strTbName As String
On Error Goto Create_Err
Set dbNew = DAO.CreateDatabase(strDataNamePath, dbLangGeneral, dbVersion30)
For Each tblCheck in dbNew.TableDefs 'check if tablename exist
If tblCheck.Name = strTableName Then
MsgBox "Table Exist"
dbNew.Close
Set dbNew =Nothing
Exit Sub
Else
Set tblNew = dbNew.CreateTableDef(strTableName) ' create new table
End If
tblNew.Fields.Append tblNew.CreateField("FieldID", dbLong, 4)
tblNew.Fields.Append tblNew.CreateField("Field1", dbText, 50)
tblNew.Fields.Append tblNew.CreateField("Field2", dbText, 50)
tblNew.Fields.Append tblNew.CreateField("Field3", dbText, 50)
tblNew.Fields.Append tblNew.CreateField("Field4", dbDate, 8)
tblNew.Fields(0).AllowZeroLength = False
tblNew.Fields(1).AllowZeroLength = True
tblNew.Fields(2).AllowZeroLength = True
tblNew.Fields(3).AllowZeroLength = True
tblNew.Fields(4).AllowZeroLength = True
dbDay.TableDefs.Append tblNew
dbNew.Close
Set dbNew = Nothing
Exit Sub
Create_Err:
MsgBox "Error Has Occured" & vbcr & _
Err.Number & vbcr & Err.Description, vbExclamation + vbOkonly,"Error"
Exit Sub
End Sub
|
__________________
I May Have Interpret It wrongly
Correct me..if I misunderstood U.
Hope This helps........and Enjoy Coding........//
zak2zak
|

08-04-2003, 09:33 AM
|
|
Centurion
|
|
Join Date: Apr 2003
Posts: 129
|
|
Yes, I agree. Having a whole year in one database is probably a better idea.
It might be easier also to use a make table query instead of making the table from scratch then filling it with your data.
Code:
'make table query to add archived table into database
sqlStr = "SELECT * INTO <new tableName> IN '<path to new database>' FROM <Table with Data>"
docmd.setwarnings(False)
docmd.runsql(sqlstr)
docmd.setwarnings(True)
|
|

08-04-2003, 10:21 AM
|
 |
Freshman
|
|
Join Date: Jul 2003
Location: Rivendell
Posts: 34
|
|
thanks!
|
To zak2zak and yakule...
Thank you both! I will try doing just as you have suggested. A set of tables in a database for a single whole year is a much better approach. Thanks again!
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|