Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql server : how can I keep my database open !

Status
Not open for further replies.

peterve

IS-IT--Management
Mar 19, 2000
1,348
NL
I am starting to write an application in VB6 that connects to SQL Server .

I open the database with this code:

Code:
Dim objconn as new ADODB.Connection
"Driver=SQL Server;Server=SQLServer1;Database=Name_of_Database"


My question is : how can I open the database in my first form, and keep it open ??

(If I define the object 'objconn' in a .bas file, then it is not recognized in my first form, it only works when I define this object in my first form itself)

Any ideas ??


---------------------------------------------------------------------
I have not failed, I've just found 10,000 ways that don't work
---------------------------------------------------------------------
Peter Van Eeckhoutte
peter.ve@pandora.be
*:->* Did this post help? Click below to let me know !
 
I believe the problem is that you are Dim-ming the variable. If you have a form and some modules (and even some classes and additional forms), the scope of variables between/in these code-containers is very important.

If you Dim a variable, as in:
Private Sub Whatever()
Dim objConn as ADODB.Connection
...
End Sub

Then it is only visible from within that Sub. If at the top of the code within that form (in the General Declarations area and not inside any sub) you declare the variable like this:
Private objConn as ADODB.Connection

Then the variable is available to all subs within that form/module/class.

If you want to create a variable that is available to *all* the code-containers in a project, then you have to declare it within a .bas module as a public variable in the General Declarations section of that module:
Public objConn as ADODB.Connection

Hope this helps.

~Jason

 
Thanks... I found out what I did wrong : I declared the variable as 'private' in the .bas module... no wonder it didn't work ...

---------------------------------------------------------------------
I have not failed, I've just found 10,000 ways that don't work
---------------------------------------------------------------------
Peter Van Eeckhoutte
peter.ve@pandora.be
*:->* Did this post help? Click below to let me know !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top