Results 1 to 10 of 10
  1. #1
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51

    How to Declare Const in Access VBA

    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.

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    public constants = visible from any module
    private constants = visible in the constant's module only

    for public:

    Code:
    public const variableName as datatype = value

  3. #3
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    ajetrumpet, that was cool...the public thing make it available in all the module...thanks a lot!!!

    I just need one more help...

    I want one const connection be there to another database...
    how can i declare that...

    I tried this way and wont work..

    Thanks again.
    Pedie
    Code:
    Const MyConnection = Connection.Open = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & db & ";Uid=;Pwd=123;"


    Quote Originally Posted by ajetrumpet View Post
    public constants = visible from any module
    Quote Originally Posted by ajetrumpet View Post
    private constants = visible in the constant's module only

    for public:

    Code:
    public const variableName as datatype = value


  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    you want to use ADO to connect to another mdb database? you can use either that or DAO. it doesn't matter.

    constants are only used for values. a connection is not a value, so no you can't use constants to do that. however, I believe you can always keep a connection open...for the entire length of the period that you have an mdb open. There might be a default timeout on established connections with ADO, but I don't know of it if there is.

    and as far as I know, you can't simply use connection.open arbitrarily in vb without declaring an ADO object first. for instance, you need to use:

    Code:
    dim conn as adodb.connection
    etc, etc...
    and by the way, simply using "const" instead of "public const" is the same thing in vb. the default specification for constants is public, just like the default spec for routines.

  5. #5
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    ajetrumpet, thank you very much!!!
    So to confirm, to open a different database besides the one i'm using i can use any type of connection like
    ADO
    ADODB
    DAO etc? and it does not matter right?

    Suppose i'm using connection like this to open my other db...
    How do i refer to that db and recordset?

    dim mydb as adodb.database?
    I tried and dont know what to do...
    'm new to access.

    Thanks again for helping...
    Regards
    Pedie

    Code:
    Dim cnn As New adodb.Connection
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Personal_Files\Access\MAINDB.accdb;Jet OLEDB:Database Password=123;"
    





    Quote Originally Posted by ajetrumpet View Post
    you want to use ADO to connect to another mdb database? you can use either that or DAO. it doesn't matter.
    Quote Originally Posted by ajetrumpet View Post

    constants are only used for values. a connection is not a value, so no you can't use constants to do that. however, I believe you can always keep a connection open...for the entire length of the period that you have an mdb open. There might be a default timeout on established connections with ADO, but I don't know of it if there is.

    and as far as I know, you can't simply use connection.open arbitrarily in vb without declaring an ADO object first. for instance, you need to use:

    Code:
    dim conn as adodb.connection
    etc, etc...


    and by the way, simply using "const" instead of "public const" is the same thing in vb. the default specification for constants is public, just like the default spec for routines.


  6. #6
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    ADODB is the syntax for ADO. I would use DAO to open another db, personally.

    It would look like so:

    Code:
    Sub whatever()
    
    Dim db As dao.Database
    Dim rs As dao.Recordset
    Set db = OpenDatabase("path")
    Set rs = db.OpenRecordset("table")
    
    'do whatever to the table
    
    rs.Close
    db.Close
    
    End Sub
    The only reason you would be concerned about the technical differences between ADO and DAO is if you were on a large network or you were offering remote access/ODBC to users. Other people will say differently, but in reality it doesn't matter all that much too often. For instance, I work for a government contractor and government will complicate anything out of anything, so of course I have to watch out for this stuff. But I know enough to know that to someone that's not managing a corporate network of machines, the difference between ADO and DAO is marginal, at best.

  7. #7
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    ajetrumpet, I dont know how to open the databse with password...
    Please help out here again...
    And so to confirm, it does not matter which connection string i use it can be ADO/DAO/JET etc I can still perform task in my other databse?

    Thanks again ...appriciate your help


    Code:
    Sub test333_1()
    Dim cnn As New adodb.Connection
    Dim db As dao.Database
    Dim rs As dao.Recordset
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Personal_Files\Access\MAINDB.accdb;Jet OLEDB:Database Password=123;"
    Set db = OpenDatabase("E:\Personal_Files\Access\MAINDB.accdb")
    Set rs = db.OpenRecordset("BTLog")
     rs.MoveFirst
     While Not rs.EOF
     Debug.Print rs.Fields(1)
     rs.MoveNext
     Wend
    rs.Close
    db.Close
    cnn.Close
    Set cnn = Nothing
    End Sub

  8. #8
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    Just calrification...

    Code:
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Personal_Files\Access\MAINDB.accdb;Jet OLEDB:Database Password=123;"


    If i use OLEDB.12.0 as connection...how do i open that/different database and recordset

    Thanks again

  9. #9
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    hello again Girly,

    Honestly, I don't want to provide any more help for ADO because I don't like it. So the DAO equivalent to what you're doing is this:

    Code:
    Sub test333_1()
     
    Dim db As dao.Database
    Dim rs As dao.Recordset
    
    Set db = OpenDatabase("E:\Personal_Files\Access\MAINDB.accdb", False, False, "MS Access;PWD=password")
    Set rs = db.OpenRecordset("BTLog")
     rs.MoveFirst
     While Not rs.EOF
     Debug.Print rs.Fields(1)
     rs.MoveNext
     Wend
    rs.Close
    db.Close
     
    rs.Close
    db.Close
     
    End Sub
    You should also read some help files in access and on the net. Educate yourself and you should be fine. For instance, since you're hung up right now on the OPEN method of the connection object, simply type in vba help "connection.open" and you'll see the help file for ADO right at the top. Since you're using the ACE engine, you either have version 07 or 2010. Some good reading for you would be either of these to start:

    http://support.microsoft.com/default...b;EN-US;209953

    http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

    Connection strings come from library (DLL) resources, like ADO and DAO. JET and ACE are database engines. They do the internal work for requests from users, like queries and such. JET/ACE is not really part of programming and automating. ADO and DAO are. Don't get them confused.

  10. #10
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    Thank you thank you ajetrumpet, thanks a alot!
    You have a great day! perfect!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to declare Global Variable
    By ganeshvenkatram in forum Access
    Replies: 1
    Last Post: 06-16-2011, 05:18 AM
  2. Replies: 2
    Last Post: 02-24-2011, 02:23 AM
  3. Declare a Global Variable and Use it on Forms
    By jackkent in forum Access
    Replies: 9
    Last Post: 10-07-2010, 10:19 AM
  4. Replies: 2
    Last Post: 06-23-2010, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums