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

Recommended Answers

All 10 Replies

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

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

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"

@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

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

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

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

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

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

Notice that I removed ":\".

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???

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.