Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Global procedure?


Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2003, 08:47 AM
kalia kalia is offline
Freshman
 
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
Question Global procedure?


Hi,

I have a project in vb6 using mysql for database. I decalre my variable in a module like this :


Option explicit
Dim conn As ADODB.Connection
Dim rs As New ADODB.Recordset


And I want to open my database only one time when the project start.... But I don't know how?? Do I have to make a global procedure?? If it's yes, how can I do it?

This is the code I want to use only one time:


Set conn = New ADODB.Connection

conn.ConnectionString = "Provider=MySQLProv;" & _
"Server=localhost;" & _
"Data source=babysit;" & _
"User Id=root;" & _
"Password="

conn.Open

If conn.State <> adStateOpen Then
MsgBox "Couldn't open connection"
Exit Sub
End If


Thanks
Reply With Quote
  #2  
Old 05-21-2003, 08:52 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

You can use the Form_Load or Form_Initialize event (initialize runs once, so you can try that)... or you could create a Sub Main() in a module and set the project to start up from there.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 05-21-2003, 12:04 PM
kalia kalia is offline
Freshman
 
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
Unhappy

If I use the form_load() ou initialize, it work only in the form where the code is.... when I open another form I lost it.... I try the sub main(), but it give me the same error on the Open "SELECT * FROM..." --> Object required

I don't have this error if i declare my variables and open my connection in the same form. But I want to declare and open my connection only one time not in all the forms i have!!

I dont know if you understand what i try to do, but if you dont tell me and i try to explain it in another way.... (im really not good in english )

thank for the help, but it dont work
Reply With Quote
  #4  
Old 05-21-2003, 12:34 PM
Hoplite's Avatar
Hoplite Hoplite is offline
Regular
 
Join Date: Feb 2003
Location: Cleveland
Posts: 83
Default

You need to make sure the connection is declared with proper scope. Declare it as a Public in the Gen. Declarations of a Module. That way the object can be called from all forms and modules. You need to make it and initialize it only once, then keep it open.

Say StartUpForm is the first form of the app. I would make the Sub Main initialize the connection then load the form.

In a module:
Code:
'General Declarations Public Conn as ADODB.Connection Sub Main() Set Conn = New ADODB.Connection Conn.ConnectionString = [insert your favorite connection string here] Conn.Open StartUpForm.Show End Sub

You need to place it where it will run only once at startup, and find a place so you can close it. If forms are loaded and unloaded multiple time, avoid the Form_Load event.
__________________
"May the forces of evil become confused on the way to your house!!"
Reply With Quote
  #5  
Old 05-21-2003, 03:19 PM
kalia kalia is offline
Freshman
 
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
Unhappy

I did exactely what you told me to do *HOPLITE* but now i have an error on the same line (rs.open) : Operation is not allowed on an object refering a closed or invalid connection.

When i execute the program, my variable conn = nothing....
Reply With Quote
  #6  
Old 05-21-2003, 03:28 PM
Hoplite's Avatar
Hoplite Hoplite is offline
Regular
 
Join Date: Feb 2003
Location: Cleveland
Posts: 83
Default

You need to make sure that you are feeding it the open connection object, you also need to make sure there are NO other declarations of variables or objects named the same thing in any form or module, and NO other set statements associated with it.

I would also make sure it's in an actual module, not one associated with a form. If it declared on a form level and the form is unloaded, it will destroy the connection.
__________________
"May the forces of evil become confused on the way to your house!!"
Reply With Quote
  #7  
Old 05-21-2003, 06:18 PM
kalia kalia is offline
Freshman
 
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
Unhappy

How can i know if my module is connect to a form (I dont think it is, but just in case...)
Reply With Quote
  #8  
Old 05-27-2003, 07:00 AM
Hoplite's Avatar
Hoplite Hoplite is offline
Regular
 
Join Date: Feb 2003
Location: Cleveland
Posts: 83
Default

If you attached a module to the project, that'll do it. The code window brought up when you double click a control on a form or select "View Code" on a form are modules attached to a form.
__________________
"May the forces of evil become confused on the way to your house!!"
Reply With Quote
  #9  
Old 05-27-2003, 09:46 PM
kalia kalia is offline
Freshman
 
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
Smile

Ok, I knew that, i didn't understand the way you told me.... sorry, my english is not that good!

Thank you very much for your help! All works great (for now!!!)

kalia
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clean up after global ADO Connection???? ChubbyArse Database and Reporting 0 03-10-2003 06:56 AM
How to execute store procedure from VB Robertkoltun Database and Reporting 1 03-03-2003 01:50 PM
Class Modules vs Basic Module nighthawk General 3 12-12-2001 01:53 PM
MS Winsocket gibson General 3 07-20-2001 10:03 AM
VB MIDI PROGRAMMING Billc General 0 04-19-2001 06:47 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->