How to Declare Const in Access VBA

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, how do I Declare Const in Access VBA...
Specially the Connection to different database...so that i can refer to this const from any Module?
Thanks a lot in advance. :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Just as in Excel. In a Standard Module:
Code:
Public Const DB1_PATH = \\Server1\Folder1\DB1.mdb"
 
Upvote 0
That works perfect...like in excel
Is it possible to add connection,sstring,id,pass etc in Const?
so that everytime i want to connect to other database i just refer to that const connection from my module?


Xen, thanks again for all the help...
I think i'm learning fast:)
 
Upvote 0
Pedie

Of course it works, Access VBA and Excel VBA are basically the same.

The main difference is the object model.

In Access you have databases, tables, queries, forms etc.

I Excel you have workbooks, worksheets, ranges, charts etc.
 
Upvote 0
Thanks Norie for clarifying for me...

'm trying to declare it this way and make it work...please advice now do i refer to this kind of const from my module?

Thanks again

Code:
[/FONT]
[FONT=Courier New]Public Const mydb1 = cnn.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Personal_Files\Access\MAINDB.accdb;Jet OLEDB:Database Password=TsPw;")[/FONT]
[FONT=Courier New]
 
Upvote 0
pedie

You can't refer to a constant like that in VBA, whether it's Access/Word/Excel/Whatever.

cnn.Open... is not a constant.
 
Upvote 0
The closest you can get with a constant is:

Code:
Public Const DB1_CONNECTION_STRING As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Personal_Files\Access\MAINDB.accdb;Jet OLEDB:Database Password=TsPw;"

Then in your code:
Code:
Sub Foo()
Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    Call rs.Open(DB1_CONNECTION_STRING)
    '//More code
    rs.Close
    Set rs = Nothing
End Sub
 
Upvote 0
Pedie

What are you using this code for?

It seems a bit strange to be using ADO in Access to work with another Access database.

Have you checked if there's anything in Access that would allow you to do whatever you are doing without code?
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top