A simple question (hopefully!)

davebhoy

Active member
Joined
Aug 22, 2006
Messages
26
Programming Experience
1-3
Hi there

I'm very new to VB.Net and only performed some simple coding in Access prior to this so hopefully this is a basic question.

If I am declaring objects or variables such as a connection string or Access Application, how do I write the code so that I only need to declare them once (say on page startup) and not everytime I need to connect to the Access Database. Example is as follows:

Protected Sub btnMacro_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMacro.Click
Dim objaccess As Object = CreateObject("access.application")
Dim strDB As String = "C:\LA Applications\DRep\V3008\DEBT_V3008.mdb"

Thanks in advance!

David
 
If you want to use those variables everywhere in your form, then
declare those variables on top (just inside your Form Class) like this:

Private objaccess As Object
Private strDB As String

Now you can use objaccess and strDB anywhere in your form.

For example in your [Form]_Load procedure you can initialize them

objaccess = CreateObject("access.application")
strDB = "C:\LA Applications\DRep\V3008\DEBT_V3008.mdb"
 
Thanks a million

It's a page in a web application so does than change things? The top of the page reads

Partial
Class admin
Inherits System.Web.UI.Page

Also how do I declare them to all pages rather than just this one?

Thanks again

David
 
it doesn't change anything, you can declare those variables after that "inherits" line there.

But now you want them to be declared throughout all pages.

If it was a vb.net project instead of a website, you could do it like this, you should add a Module to your project and declare your variables there like

Public objaccess As Object = CreateObject("access.application")
Public strDB As String = "C:\LA Applications\DRep\V3008\DEBT_V3008.mdb"

you can place methods in there too if you want.


but i don't know how to add a module to a web site project :-\
 
Back
Top