Results 1 to 10 of 10

Thread: Change a MSAccess conection string using commandline arguments

  1. #1

    Thread Starter
    Registered User
    Join Date
    Nov 2016
    Posts
    51

    Change a MSAccess conection string using commandline arguments

    What I'm trying to achieve is to change the connection string to search for the database on different locations depending on an argument passed to the executable. So, if I run my app with the /offline argument, it would use a connection string with "Data Source=|DataDirectory|DataBase\database1.accdb", while, without arguments, it would use "Data Source=\\Network_Folder\Folder\database1.accdb".

    Is it possible to achieve this on vb.net+access?

    Important info: I've used VS2015 Wizard to create the current connection string.
    Last edited by Tucalipe; Dec 10th, 2016 at 10:57 AM.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: Change a MSAccess conection string using commandline arguments

    Is it possible to achieve this on vb.net+access?
    Yes

    First, you will need to store all of your connection strings somewhere that is accessible to your application. Then in the startup routine (either sub main, or in the startup form) you will read the command line arguments and select the correct connection string based on the argument you have passed using a case statement.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Registered User
    Join Date
    Nov 2016
    Posts
    51

    Re: Change a MSAccess conection string using commandline arguments

    Ah, I can't use the wizard then? It'll have to do, thanks!

  4. #4

    Thread Starter
    Registered User
    Join Date
    Nov 2016
    Posts
    51

    Re: Change a MSAccess conection string using commandline arguments

    Ah, I think I've reached a dead end... According to what I've seen in VS2015 I'd need to rewrite all of my queries and most of my programming for the software to use a different connection string. Is that right? Or is there an easier way to declare a new connection string allowing me to have it dinamycally set?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Change a MSAccess conection string using commandline arguments

    Quote Originally Posted by Tucalipe View Post
    Ah, I think I've reached a dead end... According to what I've seen in VS2015 I'd need to rewrite all of my queries and most of my programming for the software to use a different connection string. Is that right? Or is there an easier way to declare a new connection string allowing me to have it dinamycally set?
    No that is not right. It's as simple as setting the connection string each time you create a table adapter, e.g.
    vb.net Code:
    1. Using adapter As New SomeTableAdapter
    2.     adapter.Connection.ConnectionString = someConnectionString
    3.  
    4.     '...
    5. End Using
    Just make sure that you've set the connection to be public in the DataSet designer.

  6. #6

    Thread Starter
    Registered User
    Join Date
    Nov 2016
    Posts
    51

    Re: Change a MSAccess conection string using commandline arguments

    Can I use ConfigurationManager.AppSettings to change that setting if I were using Windows forms? Something along the lines of:
    vb.net Code:
    1. Private Sub setdblocation (e As Object, sender as EventArgs) Handles Form1.Load()
    2.         If CheckOffline() = True Then
    3.             value = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Local Folder\database.accdb"
    4.         ElseIf CheckOffline() = False Then
    5.             value = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Network Place\DataBase\database.accdb"
    6. configFile.AppSettings.Settings(0).value = value
    7. End Sub

    I only have one setting so far, so I'm sure Settings(0) is the Connection String.

  7. #7
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Change a MSAccess conection string using commandline arguments

    It might be easier to store both connection strings in the config file and use the correct one based on the app running online or offline.

    You can update the config file through code but not in the way you have there, if you really feel that is the approach you want then https://social.msdn.microsoft.com/Fo...orum=vbgeneral might be of use.

    However the main issue you might have with updating the config file is folder permissions; if your application is installed into Program Files (or equivalent) you aren't going to have permissions to make changes to the file by default- you would need to either modify the default permissions (probably a bad idea) or make sure the app is installed elsewhere.

  8. #8

    Thread Starter
    Registered User
    Join Date
    Nov 2016
    Posts
    51

    Re: Change a MSAccess conection string using commandline arguments

    Quote Originally Posted by jmcilhinney View Post
    No that is not right. It's as simple as setting the connection string each time you create a table adapter, e.g.
    vb.net Code:
    1. Using adapter As New SomeTableAdapter
    2.     adapter.Connection.ConnectionString = someConnectionString
    3.  
    4.     '...
    5. End Using
    Just make sure that you've set the connection to be public in the DataSet designer.
    So I'd need to have a copy of each query from the original online table adapter on the offline one?

  9. #9
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Change a MSAccess conection string using commandline arguments

    There isn't a need to duplicate all the TableAdapter / query code, just change the connection string for them based on the user being online or not.

    The code jmcilhinney posted is showing you how to change the connection string for a TableAdapter, there is no need to duplicate things.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Change a MSAccess conection string using commandline arguments

    Quote Originally Posted by Tucalipe View Post
    So I'd need to have a copy of each query from the original online table adapter on the offline one?
    Just read what I said. There's copying. There's no online or offline. You simply do what I said. I said that you set the connection string each time you create a table adapter so that is what you do. When I say "create a table adapter" I mean an object, not a class. If you want to retrieve data to display in Form1 then you create a table adapter and call its Fill method. Between creating the object and calling the method, you set the connection string. That's it, that's all.

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