Results 1 to 2 of 2

Thread: Error Routine

  1. #1
    rlculver
    Guest

    Error Routine

    Hi There all,

    I have been setting my database to read by having the actual database located within the same directly as my VB app, using the following code :-

    Code:
    Set High_Score = OpenDatabase("maths.mdb")
    I will like to add a error routine where by the if the "maths.mdb" is not located within the same directory, a browse dialogue is displayed enabling the user to find the specfic file.

    Please help me

    Thanks

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    You'll need a global varibale to store the path to the db. Put this into a module
    Code:
    Public strDBPath As String
    Public Function FileExists(strPath As String) As Boolean
    
    On Error Resume Next
    
    Dim lngRetVal As Long
    lngRetVal = Len(Dir$(strPath))
    
    If Err Or lngRetVal = 0 Then
        FileExists = False
    Else
        FileExists = True
    End If
    
    End Function
    You need some way to check if the file exists before trying to read from it, so use this code when your app first starts. Put a common dialog control on a form and paste this code
    Code:
    'Set to default path
    If Right(App.Path, 1) <> "\" Then
        strDBPath = App.Path & "\maths.mdb"
    Else
        strDBPath = App.Path & "maths.mdb"
    End If
    
    'check to see if exists
    If FileExists(strDBPath) <> True Then
       'file does not exists, show Open file dialog. Put a commonddialog control on a form
      ' Set CancelError is True
      CommonDialog1.CancelError = False
      ' Set flags
      CommonDialog1.flags = cdlOFNHideReadOnly
      ' Set filters
      CommonDialog1.Filter = "MDB File (*.mdb)|*.mdb"
      ' Specify default filter
      CommonDialog1.FilterIndex = 1
      ' Display the Open dialog box
      CommonDialog1.ShowOpen
      ' Display name of selected file
      strDBPath = CommonDialog1.FileName
    End If
    strDBPath now contains the path to the db, whether it is where it is supposed to be, or somewhere they have selected. You will need to change this
    Code:
    Set High_Score = OpenDatabase("maths.mdb")
    To this
    Code:
    Set High_Score = OpenDatabase(strDBPath)
    Last edited by chrisjk; May 26th, 2001 at 10:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width