Cesar Francisco
The solution for Access/Jet databases.
'Do not forget to add reference to
Microsoft ActiveX Data Object Recordset 2.8 Library,
Microsoft ADO Ext.2.8 for DDL and Security and
Microsoft Jet and Replication Objects 2.6 Library under the COM Tab.
Imports System.IO
Imports System.IO.File
Public Class Form3
Public Shared Sub Restart()
End Sub
'Watch out for the DialogBoxes: BackupInfo,RestoreRestart,NewDBRestart.DataBaseEraseWarning.
You have to make them,
'BACKUP A DATABASE
'This backup will add a timestamp Month/Day/Year-HoursMinutes to the original database (DB.mdb) file's name and moves that file into a BackUps folder. Do not forget to add this BackUps folder before publish your app.
Private Sub BackUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackUp.Click
Dim x As String
Dim f As String
Dim z As String
Dim g As String
Dim BCK As New BackupInfo
x = Format(Now(), "_MMddyy_HHmm")
z = "C:\Program Files\YourApplication\DB.mdb"
g = x + ".mdb"
f = "C:\Program Files\YourApplicationNameHere\BackUps\DB" & g & ""
File.Copy(z, f)
ToolStripStatusLabel1.Text = "Backup completed succesfully."
If BCK.ShowDialog() = DialogResult.OK Then
End If
End Sub
'RESTORE A DATABASE
'Find you Backup file with TimeStamp in the BackUps folder (or anywhere else you backed up your database).
'This will replace (overwrite the database) in the YourApplication folder.
Private Sub RestoreDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestoreDB.Click
Dim Filename As String
Dim R As New RestoreRestart
Dim overwrite As Boolean
overwrite = True
Dim xi As String
With OpenFileDialog1
'.Filter = "Database files (*.mdb)|*.mdb|" & "All files|*.*"
If .ShowDialog() = DialogResult.OK Then
Filename = .FileName
xi = "C:\Program Files\YourApplicationNameHere\DB.mdb"
File.Copy(Filename, xi, overwrite)
End If
End With
'Notify user in the ToolStrip (or MessageBox.Show("Data restored....")
ToolStripStatusLabel1.Text = "Data restored successfully"
'Restart application
If R.ShowDialog() = DialogResult.OK Then
Application.Restart()
End If
End Sub
'CREATE A NEW EMPTY DATABASE.
Private Sub CrNewDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrNewDB.Click
Dim L As New DatabaseEraseWarning
Dim Cat As ADOX.Catalog
Cat = New ADOX.Catalog
Dim R2 As New NewDBRestart
If File.Exists("C:\Program Files\YourApplicationNameHere\DB.mdb") Then
If L.ShowDialog() = DialogResult.Cancel Then
Exit Sub
Else
File.Delete("C:\Program Files\YourApplicationNameHere\DB.mdb")
End If
End If
Cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\ YourApplicationNameHere \
DB.mdb; Jet OLEDB:Engine Type=5")
Dim Cn As ADODB.Connection
'Dim Cat As ADOX.Catalog
Dim Tablename As ADOX.Table
'Taylor these according to your need - add so many column as you need.
Dim col As ADOX.Column = New ADOX.Column
Dim col1 As ADOX.Column = New ADOX.Column
Dim col2 As ADOX.Column = New ADOX.Column
Dim col3 As ADOX.Column = New ADOX.Column
Dim col4 As ADOX.Column = New ADOX.Column
Dim col5 As ADOX.Column = New ADOX.Column
Dim col6 As ADOX.Column = New ADOX.Column
Cn = New ADODB.Connection
Cat = New ADOX.Catalog
Tablename = New ADOX.Table
'Open the connection
Cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\YourApplicationNameHere\DB.mdb;Jet OLEDB:Engine Type=5")
'Open the Catalog
Cat.ActiveConnection = Cn
'Create the table (you can name it anyway you want)
Tablename.Name = "Table1"
'Taylor these according to your need - add so many column as you need. Watch for the DataType!
col.Name = "ID"
col.Type = ADOX.DataTypeEnum.adInteger
col1.Name = "Serial"
col1.Type = ADOX.DataTypeEnum.adInteger
col1.Attributes = ADOX.ColumnAttributesEnum.adColNullable
col2.Name = "FName"
col2.Type = ADOX.DataTypeEnum.adVarWChar
col2.Attributes = ADOX.ColumnAttributesEnum.adColNullable
col3.Name = "LName"
col3.Type = ADOX.DataTypeEnum.adVarWChar
col3.Attributes = ADOX.ColumnAttributesEnum.adColNullable
col4.Name = "DOB"
col4.Type = ADOX.DataTypeEnum.adDate
col4.Attributes = ADOX.ColumnAttributesEnum.adColNullable
col5.Name = "Sex"
col5.Type = ADOX.DataTypeEnum.adVarWChar
col5.Attributes = ADOX.ColumnAttributesEnum.adColNullable
col6.Name = "Ph1"
col6.Type = ADOX.DataTypeEnum.adVarWChar
col6.Attributes = ADOX.ColumnAttributesEnum.adColNullable
Tablename.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "ID")
'You have to append all your columns you have created above
Tablename.Columns.Append(col)
Tablename.Columns.Append(col1)
Tablename.Columns.Append(col2)
Tablename.Columns.Append(col3)
Tablename.Columns.Append(col4)
Tablename.Columns.Append(col5)
Tablename.Columns.Append(col6)
'Append the newly created table to the Tables Collection
Cat.Tables.Append(Tablename)
'User notification (again you can use MessageBox.Show() if you want)
ToolStripStatusLabel1.Text = "A new empty database was created successfully"
'clean up objects
Tablename = Nothing
Cat = Nothing
Cn.Close()
Cn = Nothing
'This restart the application
If R2.ShowDialog() = DialogResult.OK Then
Application.Restart()
End If
End Sub
'COMPACT A DATABASE
'Have a Button with "CompactDB" design name
Private Sub CompactDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompactDB.Click
Dim JRO As JRO.JetEngine
JRO = New JRO.JetEngine
'The first source is the original, the second is the compacted database under an other
name.
JRO.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program
Files\VSoft\AppMiss\AmDB.mdb; Jet OLEDB:Engine Type=5",
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program
Files\VSoft\AppMiss\AmDBComp.mdb; JetOLEDB:Engine Type=5")
'Here the original (not compacted database is deleted)
File.Delete("C:\Program Files\YourApplicationNameHere\DB.mdb")
'Here the compacted database is renamed to the original database.
Rename(" C:\Program Files\YourApplicationNameHere\DBComp.mdb", "C:\Program
Files\ YourApplicationNameHere \DB.mdb")
'User notification
ToolStripStatusLabel1.Text = "The database was compacted successfully"
End Sub
End Class