Inline Code/Class Question [Resolved]
Hi,
I'm creating a database application, which connects to a database and performs different queries on different forms. I've designed it two different ways:- Using code in each form to make a new connection and then doing the query with that
- Using a class for the connection, and creating a global instance of it in a module, so that the connection is made when the application loads and the recordset is accessed by various Property Get statements.
Which do you think is better programming practice? Generally, we're encouraged not to use global declarations, but this is an object, so I'm not sure if that's okay. Also, with the class, if I need to retrieve some information from a separate table (e.g. I have an ID number from one table, and I need to match that to a name from another), then I either have to store the orginal query and query again at the end of retrieving the new information, or create a new instance of the class.
If this is unclear, and you want some code, just ask.
Re: Inline Code/Class Question
Had another thought on this since earlier:
What if I declared my connection object globally when the application starts and then created separate recordsets each time I needed them?
There must be some standard way of doing this...
Re: Inline Code/Class Question
Quote:
Originally Posted by olamm2k
Hi,
I'm creating a database application, which connects to a database and performs different queries on different forms. I've designed it two different ways:
- Using code in each form to make a new connection and then doing the query with that
- Using a class for the connection, and creating a global instance of it in a module, so that the connection is made when the application loads and the recordset is accessed by various Property Get statements.
Which do you think is better programming practice? Generally, we're encouraged not to use global declarations, but this is an object, so I'm not sure if that's okay. Also, with the class, if I need to retrieve some information from a separate table (e.g. I have an ID number from one table, and I need to match that to a name from another), then I either have to store the orginal query and query again at the end of retrieving the new information, or create a new instance of the class.
If this is unclear, and you want some code, just ask.
#2 is definately more Object-Oriented than #1 so I would strongly recommend #2.
However there is a third option, which is similar to #2; make #2 as an ActiveX EXE (out of process Object). This object would be stand-alone, running before and/or after your application loads. This gives you the advantages of encapsulation and the option to run the Object on your machine, or even a centralized server (a different machine).
Re: Inline Code/Class Question
Thanks for your reply.
I've not created an ActiveX EXE before, so I've no idea how to go about this (not the creation, but how to use it once made).
Could you give me an example, or point me to a site containing one?
Re: Inline Code/Class Question
Either technic will work, it does however totally depends on your application architecture. If you don't need to keep connection open then why would you ever need to declare ti globally and visa versa ... You may get tons of recommendation but it's all your call - you're the designer so you must know all aspects ... There is however one thing to keep in mind: simplicity is what really counts after all - don't overcomplicate you program(s) if there is no need for it ...
Cheers
Re: Inline Code/Class Question
The connection will be kept open the whole time, which is why I think it makes sense to just create it once, then make the various recordsets I need from it.
Do you think a class approach would be better, or just declaring it in a module as a global object (Global objConn as New ADODB.Connection etc.)?
Re: Inline Code/Class Question
Personally I don't think that class is necessary: it will create some overhead - object within object.
Declaring ONE object variable as adodb.connection globally in the module should be more than enough.
Re: Inline Code/Class Question
Fair enough - I'll go with the global object declaration.
Just out of interest, though, how do you use an ActiveX EXE as suggested?
Re: Inline Code/Class Question
How would you use it as suggested ? That would be for creating DCOM type applications.
You may want to visit MSDN ON LINE for more information.
Good luck.
Re: Inline Code/Class Question
Thanks, I'll take a look.
Re: Inline Code/Class Question
Quote:
Originally Posted by olamm2k
Fair enough - I'll go with the global object declaration.
Just out of interest, though, how do you use an ActiveX EXE as suggested?
This may be a good place to start:
http://www.vbforums.com/showthread.php?t=306032