Using a Variable within a Connection String

  • Thread starter Thread starter pooba53
  • Start date Start date
P

pooba53

Take a gander:

Me.OleDbConnection1.ConnectionString = "Jet OLEDB:Global
Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=1;Data Source=""AppBase"";Jet OLEDB:Engine
Type=5;Provider=""Mic" & _
"rosoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet
OLEDB:SFP=False;persist sec" & _
"urity info=False;Extended Properties=;Mode=Share Deny
None;Jet OLEDB:Encrypt Dat" & _
"abase=False;Jet OLEDB:Create System Database=False;Jet
OLEDB:Don't Copy Locale o" & _
"n Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;User ID=Admin;Jet" & _
" OLEDB:Global Bulk Transactions=1"

I would like to use a string contained in a variable for the Data
Source in the above example. In the above example the variable is
called "AppBase". No matter what I do with regard to quotes, no
quotes, single quotes, etc. the value of the string is never
substituted in. It remains "AppBase".

Is there some syntax that will allow me to use the contents of a
variable in my connection string?

Thanks.
 
"...Data Source=" & AppBase & ";Jet OLEDB:Engine..."

Assume AppBase is declared as String and holds a valid content.
 
Hey, that worked like a charm! Thanks a million ;-)

One thing I noticed, however, is that the portion of the code where
I'm editing is in the "Windows Form Designer generated code" portion.
This is where the connection string is. For some reason my code
disappears after a few runs.

I'm wondering how I can get around this problem.

-Dan
 
Hey, that worked like a charm! Thanks a million ;-)

One thing I noticed, however, is that the portion of the code where
I'm editing is in the "Windows Form Designer generated code" portion.
This is where the connection string is. For some reason my code
disappears after a few runs.

I'm wondering how I can get around this problem.

-Dan

Never make changes to the Application generated code. Make your changes after
the InitializeComponent() call in the Form's constructor. Doing so will
override any settings created or set in the InitiailzeComponent method.

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
 
Never make changes to the Application generated code. Make your changes after
the InitializeComponent() call in the Form's constructor. Doing so will
override any settings created or set in the InitiailzeComponent method.

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.comhttp://www.arltex.comhttp://www.tomchilders.comhttp://www.n5ge.com

My connection string is set up when InitializeComponents() completes.
I understand this, but won't I screw something up if I declare an
additional connection string somewhere else in the code?

Thanks.
-Dan
 
Pooba:

Have you considered using a ConnectionStringBuilder? Probably your best bet

Can you provide an example of how this would be incorporated and where
in my code? I read the information at the URL above, but that does not
really paint a complete picture.

Thanks.
-Dan
 
Back
Top