1,105,263 Community Members

Automatically detect a drive for Backup a database

Member Avatar
jhedonghae
Junior Poster
117 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Oxiegen
Master Poster
765 posts since Jun 2006
Reputation Points: 88 [?]
Q&As Helped to Solve: 149 [?]
Skill Endorsements: 7 [?]
Featured
 
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
Member Avatar
jhedonghae
Junior Poster
117 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Oxiegen
Master Poster
765 posts since Jun 2006
Reputation Points: 88 [?]
Q&As Helped to Solve: 149 [?]
Skill Endorsements: 7 [?]
Featured
 
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"
Member Avatar
jhedonghae
Junior Poster
117 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
Oxiegen
Master Poster
765 posts since Jun 2006
Reputation Points: 88 [?]
Q&As Helped to Solve: 149 [?]
Skill Endorsements: 7 [?]
Featured
 
0
 

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

Member Avatar
jhedonghae
Junior Poster
117 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
jhedonghae
Junior Poster
117 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Oxiegen
Master Poster
765 posts since Jun 2006
Reputation Points: 88 [?]
Q&As Helped to Solve: 149 [?]
Skill Endorsements: 7 [?]
Featured
 
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 ":\".

Member Avatar
jhedonghae
Junior Poster
117 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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???

Member Avatar
jhedonghae
Junior Poster
117 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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