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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create a new database automatically?

Status
Not open for further replies.

pmo

Technical User
Jan 15, 2001
76
AU
I friend has written a small bit of code I have listed below that creates a new database on a floppy disk. This works fine, however if this database already exists on the floppy it errors as it will not write over the top of it. Can anyone add to this code that will force the creation of the new database over the top of the existing one. I am using this as a means of backing up. This code only allows you to copy to blank disks each time. I would like to be able to use the same disk and write over the top each time.
PLEASE HELP I DON'T KNOW ANYTHING ABOUT CODE..

Function Create_Teacher_Disk()

Dim DefaultWorkspace As Workspace
Dim CurrentDatabase As Database, MyDatabase As Database
Set DefaultWorkspace = DBEngine.Workspaces(0)
Set CurrentDatabase = DefaultWorkspace.Databases(0)

On Error GoTo Err_TEACHER_Click

' Create new, encrypted database.
Set MyDatabase = DefaultWorkspace.CreateDatabase("a:\teacher.mdb", DB_LANG_GENERAL, DB_ENCRYPT)

End Function
 
VB 6
I have vb code here to create a copy of an exsisting DB, and put it on the A Drive. I hope this leads you in the right direction: You will like the sripting.filesystemobject, but be ethical when using it.

Public Sub backup()
On Error GoTo jason2

Dim date1 As Variant
Dim fil
Dim fso
Dim fill2
Dim fso2
date1 = Format(Date, "MM-DD-YYYY")
Set fso = CreateObject("scripting.filesystemobject")
Set fso2 = CreateObject("scripting.filesystemobject")
Set fill = fso.getfile("c:IVDB2000.mdb")
fill.Copy ("a:") 'you might need a / example ("a:/)
jason2:
Exit Sub
End Sub

Jason
 
Try This:

Function Create_Teacher_Disk()

Dim strFile as string
Dim DefaultWorkspace As Workspace
Dim CurrentDatabase As Database, MyDatabase As Database
Set DefaultWorkspace = DBEngine.Workspaces(0)
Set CurrentDatabase = DefaultWorkspace.Databases(0)

On Error GoTo Err_TEACHER_Click

' check if the file exists
strFile = Dir("A:\teacher.mdb")
If Len(strFile) > 0 then
' the file exists - so delete it
Kill "A:\teacher.mdb")
End If

' Create new, encrypted database.
Set MyDatabase = DefaultWorkspace.CreateDatabase("a:\teacher.mdb", DB_LANG_GENERAL, DB_ENCRYPT)

End Function

Let me know how this works.

John
 
I have a couple of comments here. Two of the above functions have an OnError line but no error handler. These will error out on compile. That line should not be there, or better, there should be an error handler.

Now for the more important comment. pmo, you said that you use this as a means of backing up. All the CreateDatabase method does is create a new, blank database! If you follow this up by exporting all your database objects you are OK, but this does not create a copy of your existing teacher.mdb. What will work is the filecopy() function as follows (make sure to read my following comments!)

Function BackupTeacher()
Dim strSource As String
Dim strDestination As String

strSource = "C:\Teacher.mdb"
strDestination = "A:\teacher.mdb"

'check if the file exists
strFile = Dir(strDestination)
If Len(strFile) > 0 Then
' the file exists - so delete it
Kill strDestination
End If

FileCopy strSource, strDestination

End Function

However, there is a gotcha. This code will fail if it is run from within teacher.mdb because teacher.mdb is open and you can't copy it. You have to run this code from outside; either from another mdb or maybe a little vb proggie.

After you go through all that work it might be easier to just drag your teacher.mdb icon to the A: drive icon and be done with it. Or if you like command prompts, at a DOS prompt, use

Copy C:\SourceDir\teacher.mdb A:\teacher.mdb

Either way you'll be prompted as to whether you want to overwrite the existing file.
 
Thanks for the assistance on the creation of the new database. It is now up and running.

Wayne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top