Results 1 to 12 of 12

Thread: [Resolved] MS Access Database and (dynamic) Connection String

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    [Resolved] MS Access Database and (dynamic) Connection String

    Hey all,
    I'm working on creating an installation package for my program. It includes an MS Access database, and I've just been using an ODBC connection to connect to it. I was building the connection string on the basis that I had a defineable location for the database. Now I need the connection string to just look for the file in the same folder that the installation was made to (and also the same folder that the .exe and other files will be in).
    Here is the connection string I'm using now:

    Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="DSN=MS Access Database;DBQ=C:\Documents and Settings\JP\My Documents\Visual Studio Projects\BrickBlaster\levels.mdb;DefaultDir=C:\Documents and Settings\JP\My Documents\Visual Studio Projects\BrickBlaster;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;"

    I guess what I'm asking is what do I change the DBQ= and DefaultDir= to?
    Last edited by Sgt Spike; Oct 4th, 2006 at 12:17 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Access Database and (dynamic) Connection String

    Use the Application.StartupPath instead of hard coding the db path, assumes the db is located in the exe's local folder.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    Re: MS Access Database and (dynamic) Connection String

    That's what I was looking for. I'm not getting it to work though... Here's the new connection string:

    Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="DSN=MS Access Database;DBQ=Application.StartupPath\levels.mdb;DefaultDir=Application.StartupPath;DriverId=25;FIL=M S Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;"

    When I click on "Test Connection" it gives me this error: "Test connection failed because of an error in initializing provider. [Microsoft][ODBC Driver Manager] Invalid connection string attribute."

    BTW, I'm making this connection by right-clicking on Data Connections in Server Explorer and clicking "Add Connection..." Then I select the Provider tab and select "Microsoft OLE DB Provider for ODBC Drivers". And then I click next, and put in my connection string. Am I missing any steps, or should I be doing this completely differently?

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: MS Access Database and (dynamic) Connection String

    you should be using the "Microsoft Jet 4.0 OLE DB Provider" to make your connection. using ODBC requires you setup a datasource on the machine, you don't really want that. OLEDB is newer and more flexible than ODBC.

    Change 4.0 to 3.1 if you happen to be using a real old Access DB like 97 or earlier.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Access Database and (dynamic) Connection String

    You need to place the double quotes outside of the vb code so vb knows to evaluate the function as a function and not a string text.
    VB Code:
    1. .Connectionstring = "Provider=MSDASQL.1;Persist Security Info=False;DBQ=" Application.Startuppath & "...blah blah blah...."
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    Re: MS Access Database and (dynamic) Connection String

    Here's the code I'm using for the connection string now. I'm setting it in code rather than in the properties window, as it didn't seem to like that. It's still coming up with an error though.

    VB Code:
    1. OdbcConnection.ConnectionString = "PageTimeout=5;DSN=MS Access Database;DefaultDir=" & Application.StartupPath & ";DriverId=25;DBQ=" & Application.StartupPath & "levels.mdb;FIL=MS Access;MaxBufferSize=2048;UID=admin"
    2.         Dim myCommand As New Odbc.OdbcCommand(getLineQuery, OdbcConnection)
    3.         OdbcConnection.Open()

    The error (which is caught and written to the console) is:

    Error in buildBricks(): System.Data.Odbc.OdbcException: ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.
    ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
    ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.
    at System.Data.Odbc.OdbcConnection.Open()
    at BrickBlaster.Form1.buildBricks() in C:\Documents and Settings\JP\My Documents\Visual Studio Projects\BrickBlaster\Form1.vb:line 443The program '[2900] BrickBlaster.exe' has exited with code 0 (0x0).
    RobDog, Is there anything wrong with the connection string as I am writing it?

    klienma, or anyone else, could you tell me a bit more about Jet 4.0 OLE DB Provider?

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: MS Access Database and (dynamic) Connection String

    well for starters, let me know what version of .NET you are using.

    It doesn't have much effect on the connection string you need, but I can probably provide you a sample DB connection.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    Re: MS Access Database and (dynamic) Connection String

    I'm using .NET 2003 Academic version.

    A sample DB connection with an OLE DB connection would be great. I was having trouble with it when I was trying to do it myself.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: MS Access Database and (dynamic) Connection String

    Here you go.
    Attached Files Attached Files

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    Re: MS Access Database and (dynamic) Connection String

    Just figured it out and got the ODBC connection working... in the connection string, instead of putting DBQ=" & Application.StartupPath & "levels.mdb, I just put DBQ=levels.mdb. That should fix my problem. Thanks for the help all.

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [Resolved] MS Access Database and (dynamic) Connection String

    I still suggest you use OLEDB

  12. #12
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: [Resolved] MS Access Database and (dynamic) Connection String

    It works because the database is in the same path as your application. Is each one of your users getting a seperate database? I second Klienma's suggestion...I'd drop access and use SQL Server if I had the money .

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