Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Automatic Creation of Access Database using VB


Reply
 
Thread Tools Display Modes
  #1  
Old 07-29-2003, 08:40 AM
justagirl's Avatar
justagirl justagirl is offline
Freshman
 
Join Date: Jul 2003
Location: Rivendell
Posts: 34
Question 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.
Reply With Quote
  #2  
Old 07-29-2003, 03:18 PM
yakule yakule is offline
Centurion
 
Join Date: Apr 2003
Posts: 129
Default

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>
Reply With Quote
  #3  
Old 07-31-2003, 12:16 PM
zak2zak's Avatar
zak2zak zak2zak is offline
Contributor
 
Join Date: Jul 2003
Location: Singapore
Posts: 686
Lightbulb 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
Reply With Quote
  #4  
Old 08-04-2003, 06:53 AM
justagirl's Avatar
justagirl justagirl is offline
Freshman
 
Join Date: Jul 2003
Location: Rivendell
Posts: 34
Default 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!
Reply With Quote
  #5  
Old 08-04-2003, 08:59 AM
zak2zak's Avatar
zak2zak zak2zak is offline
Contributor
 
Join Date: Jul 2003
Location: Singapore
Posts: 686
Question 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
Reply With Quote
  #6  
Old 08-04-2003, 09:33 AM
yakule yakule is offline
Centurion
 
Join Date: Apr 2003
Posts: 129
Default

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)
Reply With Quote
  #7  
Old 08-04-2003, 10:21 AM
justagirl's Avatar
justagirl justagirl is offline
Freshman
 
Join Date: Jul 2003
Location: Rivendell
Posts: 34
Smile 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!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word - MailMerge with VB, Word 2000 and an Access 97 database Agamemnon1 Word, PowerPoint, Outlook, and Other Office Products 0 08-27-2002 07:33 AM
QB to VB Conversion - By AIO BillSoo Tutors' Corner 0 08-06-2002 11:37 AM
User-defined type not defined (im new) skript_kiddie General 8 06-16-2002 02:06 PM
Using VB Prog to access Database Online phat32 Database and Reporting 3 05-18-2002 11:42 AM
VB application using Access database on WIndows NT VBBeginner Database and Reporting 1 10-10-2000 12:52 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->