Path

I have the following code:

Option Strict Off
Option Explicit On 
Friend Class clsMain
    Public Function OpenConnect() As ADODB.Connection
        Dim com As Object
        com = New ADODB.Command
        Dim dbPath As String
        'dbPath = VB6.GetPath & "\" & "ss.mdb"
        dbPath = "My Documents" & "\" & "DotNet" & "\" & "ss" & "\" & "bin" & "\" & "ss.mdb"
        cnConnection = New ADODB.Connection
        cnConnection = New ADODB.Connection
        With cnConnection
            .Open("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "'; Jet OLEDB:database password=123")
        End With
    End Function
End Class

________________________________________________

Now when I run this program it gives me the following error:

It looks if my path is correct


An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ss.exe

Additional information: 'C:\My Documents\DotNet\ss\bin\My Documents\DotNet\ss\bin\ss.mdb' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.


Answer this question

Path

  • Vidottom

    I found spaces cause the problem.. try "\\" instead.

    plus in vb.net try this

    dbpath = Application.StartupPath & "\ss.mdb"


  • Andres_Ghelarducci

    Thank you the  dbpath = Application.StartupPath & "\ss.mdb" did work 


  • Joanne A.

    Actually, that's not true.  If you specify a file name without a path, the value of Environment.CurrentDirectory will be used.  When the app starts, it will be the same as Application.StartupPath, but if something changes the current directory, such as an OpenFileDialog, your code will break.
  • Guy Mahieu

    if your db is always in Application.StartupPath, you can just omit the app path and specify "ss.mdb". vb.net always look in the app startup path if a file name is not qualified with a path.

    -andy moyer

  • Youssef Abou-Kewik

    The path is not rooted, so it's being appended to the current working path, which is already the path you're looking for.  That's why "My Documents\DotNet\ss\bin\" is repeated in the error message.  Either root the path (C:\) or make it relative from the current working directory.

    I'm not sure why you're doing all the concatenation.  What you're doing always evaluates to "My Documents\DotNet\ss\bin\ss.mdb", so why not just put "My Documents\DotNet\ss\bin\ss.mdb"   Also, using the System.IO.Path class to manipuate path strings can be helpful.

    One last thing, which is admittedly only an opinion.  Option Strict off is roughly equivilent to AllowSubtleBugs = True

  • Path