1.11M Members

Automatically detect a drive for Backup a database

 
0
 

i have here a code for my back up:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dbload As String = Application.StartupPath + "\Enrollment System.accdb"
    Dim backupdb As String = "G:{0:yyyyMMdd}" + ".accdb"
    System.IO.File.Copy(dbload, String.Format(backupdb, Date.Today), True)
    MsgBox("DB Backup Successful")
End Sub

but this code sets a certain drive where my database is save..what i wanted is that on how to automatically detect a drive then save it if their is available drive and if not then probably would prompt that their is no destination for saving..

please help me

 
1
 

Here's how you can get an ArrayList of available drives.

Imports System.IO

Public Function GetDrives() As ArrayList
    Dim drives As New ArrayList

    For Each drive As DriveInfo In My.Computer.FileSystem.Drives
        drives.Add(drive.Name)
    Next

    Return drives
End Function
 
0
 

do i still need to use my code with your code?

 
0
 

You can use both.
Just add a String variable to your code that will contain the selected drive from the returning ArrayList from my code.
Then use that variable and concatinate it with ":{0:yyyyMMdd}" + ".accdb"

    'This will get the very last available driveletter
    Dim driveletter As String = GetDrives().Item(GetDrives.Count -1)

    Dim backupdb As String = driveletter & ":{0:yyyyMMdd}" + ".accdb"
 
0
 

@Oxiegen this is my new code based on what you said

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dbload As String = Application.StartupPath + "\Enrollment System.accdb"
    Dim driveletter As String = GetDrives().Item(GetDrives.Count - 1)
    Dim backupdb As String = driveletter & ":\{0:yyyyMMdd}" + ".accdb"
    System.IO.File.Copy(dbload, String.Format(backupdb, Date.Today), True)
    MsgBox("DB Backup Successful")
End Sub
Public Function GetDrives() As ArrayList
    Dim drives As New ArrayList
    For Each drive As DriveInfo In My.Computer.FileSystem.Drives
        drives.Add(drive.Name)
    Next
    Return drives
End Function

but i get an error as seen in the attach image backup

Attachments
 
0
 

What does backupdb look like?
And also, have you tried using backupdb without using String.Format(backupdb, Date.Today)?

 
0
 

isn't it that backupdb is a declaration? sorry i don't know either...

 
0
 

when i remove String.Format(backupdb, Date.Today) it prompt the message that successful but nothing is save..

 
0
 

Good.
Now, reformat the backup variable so that it looks like this:

Dim backupdb As String = driveletter & "{0:yyyyMMdd}" + ".accdb"

Notice that I removed ":\".

 
0
 

now it's working...thanks a lot..but i have just one question...what if their is no available drive how do i prompt a message that insert a drive first???

 
0
 

oh i found out it already..thanks for everyone's help

Question Answered as of 2 Years Ago by Oxiegen
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article