 |
 |

05-21-2003, 08:47 AM
|
|
Freshman
|
|
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
|
|
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
|
|

05-21-2003, 08:52 AM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
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. 
|
|

05-21-2003, 12:04 PM
|
|
Freshman
|
|
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
|
|
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   
|
|

05-21-2003, 12:34 PM
|
 |
Regular
|
|
Join Date: Feb 2003
Location: Cleveland
Posts: 83
|
|
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!!"
|

05-21-2003, 03:19 PM
|
|
Freshman
|
|
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
|
|
|
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....
|
|

05-21-2003, 03:28 PM
|
 |
Regular
|
|
Join Date: Feb 2003
Location: Cleveland
Posts: 83
|
|
|
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!!"
|

05-21-2003, 06:18 PM
|
|
Freshman
|
|
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
|
|
|
How can i know if my module is connect to a form (I dont think it is, but just in case...)
|
|

05-27-2003, 07:00 AM
|
 |
Regular
|
|
Join Date: Feb 2003
Location: Cleveland
Posts: 83
|
|
|
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!!"
|

05-27-2003, 09:46 PM
|
|
Freshman
|
|
Join Date: Sep 2002
Location: Montreal, QC
Posts: 49
|
|
|
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
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|