Changing Data Environment Connection at runtime

Changing Data Environment Connection at runtime

Post by David Kate » Sun, 21 Oct 2001 04:14:58



I'm runninb VB6, DE, with either a SQL2000 or an Access2000 back end. At
design time I can connect to either by just changing the Connection and
clicking the refresh button. But at run time I want to choose which database
to use. I can set the connection string at run-time, but I need to do a
refresh (just like clicking the refresh button in the DE).

Any ideas?

Thanks, David

 
 
 

Changing Data Environment Connection at runtime

Post by chri » Wed, 24 Oct 2001 11:55:10


Hi David,

At run time, you can just run the command of the dataenvironment to refresh
the contents. In fact, the command object is regarded as a function of the
dataenvironment and can be ran when needed. In the situation you described,
after changing the dataenvironment connectionstring and so the refered
database is changed, we need to run the command to apply the changes .
Please refer to the following example.

In this example, I have two buttons to change the back end database of an
dataenvironment between SQL server and Access database each, and then
populate an datagrid control with the data from one of the tables in each
database.

Below is the code corresponding to each button_click event:

Private Sub Command1_Click()
DataEnvironment1.Connection1.Close
DataEnvironment1.Connection1.ConnectionString =
"Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=pubs;Data Source=chris2000"
DataEnvironment1.Connection1.Open

DataEnvironment1.Commands(1).CommandText = "dbo.TABLE1"
DataEnvironment1.Command1 'This line is the key point.

DataGrid1.DataMember = "command1"
End Sub

Private Sub Command2_Click()
DataEnvironment1.Connection1.Close
DataEnvironment1.Connection1.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\My
Documents\TEMP\Db1.mdb;Persist Security Info=False"
DataEnvironment1.Connection1.Open

DataEnvironment1.Commands(1).CommandText = "table1"
DataEnvironment1.Command1 'This line is the key point.

DataGrid1.DataMember = "command1"
End Sub

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. Copy right, 2001 Microsoft Corporation.
All rights reserved.

Chris
Microsoft Support

 
 
 

1. Connection: changing connection string in data environment

Is it normally possible to change the connection string of a connection that
is part of a data environment?
The connection has a connection string already set in the development
environment but we wish to use an udl file in the production environment.
This file could then be changed without having to modify the program code.
When I try to connect using
    dataenvironment.Connection1.Open "File Name=" & "file.udl"
I get the error message  no 3001 saying that the parameters are not valid or
in conflict with other parameters or out of authorized limits.
This command works well with a connection object declared in code.
Is there a solution to this problem?  Or the connection cannot be changed?
Thank you very much,
Jean Trudel

2. VAX Rdb/VMS questions

3. Changing Data environment provider at runtime

4. Transferring of Database not working properly

5. Changing Database Connection In Data Environment

6. ODBC -> Oracle 16 bit on win2000pro

7. Problem while changing the connection string in Data Environment

8. A VB Newbie question about creating tables.

9. Changing Data Environment Connection String

10. Database changes in runtime environment

11. Data Environment / Data Report / Changing database file at run time

12. closing ADO data controls and data environment connections

13. Setting Data Environment provider at runtime