Results 1 to 6 of 6

Thread: Trying not to open a database exclusively.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Location
    Leicester, England (City of Kings)
    Posts
    156

    Trying not to open a database exclusively.

    Hi

    I have been trying to print ot a report in Acess from VB. I havew worked out the code to do this, but I have already opened the database up in my project & so I get an error about the database being opened exclusively already by another user.

    This might be because in opening the database at the start of the program I have used

    VB Code:
    1. Dim bln1ExclusiveMode As Boolean
    2.     Dim bln1ReadOnly As Boolean
    3. '
    4. '   Declare workspace and open database
    5. '
    6.     bln1ExclusiveMode = True
    7.     bln1ReadOnly = False
    8.     str3Database = App.Path & "\Sam.mdb"
    9.     Set wsp3workspace = DBEngine.Workspaces(0)
    10.     Set dba3Database = wsp3workspace.OpenDatabase(str3Database, bln1ExclusiveMode, bln1ReadOnly)

    to cut down on any problems with many people opening up the database. The problem is that when I call the report I use the code:

    VB Code:
    1. Private Sub ReportBorrowed()
    2.  
    3. Dim AccessDB As Access.Application
    4. Dim strFilter As String
    5. Dim strWhere As String
    6.  
    7.  
    8.     Set AccessDB = New Access.Application
    9.     AccessDB.OpenCurrentDatabase App.Path & "\Sam.mdb"
    10.     AccessDB.DoCmd.OpenReport "Borrowed", acViewNormal, strFilter, strWhere
    11.     AccessDB.Visible = True
    12.     AccessDB.Quit acQuitSaveAll
    13.     Set AccessDB = Nothing

    which causes the problem. I am trying to combat this but have drawn a blank with the methods I've used so far.Could anyone suggest how to:

    a) open the database in a mode that isn't exclusive

    or

    b) get the repots to use the same connection to the database that I made on opening the program.

    Thanks

    Sam

  2. #2
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040
    hi there,

    try this

    the error may be here

    VB Code:
    1. bln1ExclusiveMode = True


    try replacing true by false


    hope u can advise me if this work

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Location
    Leicester, England (City of Kings)
    Posts
    156
    That was the first thing that I tried that but it didnt make any difference to the problem.

    I don't quite understand why that would not work, but it doesn't.

    Is there anything else I'm missing?

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    What you are missing ... If you opened the database exclusive already... Do not open attempt to open another connection to the database in your other routine. Use the same connection variable. So you must set the connection variable as a global so the rest of your program can use the same connection.

    ie..

    Global dba3Database as database


    This should do the trick. Just reference the connection as you would normally do anywhere in your program.
    Last edited by randem; Dec 8th, 2002 at 04:55 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Location
    Leicester, England (City of Kings)
    Posts
    156
    Randem,,

    Thanks for your response, you're making a lot of sense. Would you mind applying what you said about using the same connection variable for using the database for other purposes to the example I supplied. Namely, how would I code:

    VB Code:
    1. Private Sub ReportBorrowed()
    2.  
    3. Dim AccessDB As Access.Application
    4. Dim strFilter As String
    5. Dim strWhere As String
    6.  
    7.  
    8.     Set AccessDB = New Access.Application
    9.     AccessDB.OpenCurrentDatabase App.Path & "\Sam.mdb"
    10.     AccessDB.DoCmd.OpenReport "Borrowed", acViewNormal, strFilter, strWhere
    11.     AccessDB.Visible = True
    12.     AccessDB.Quit acQuitSaveAll
    13.     Set AccessDB = Nothing


    Thanks for your help so far

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Cool

    I re-wrote your code so that it works and you will have a better understanding on database access techniques.

    You will need to put in so references for this to work, I imagine you have a few in already. DAO, Access, Script Control.

    The Table 'Contacts' is just a substitution for the table that is in your database. So without opening another connection I can access the all the objects in the Access database through DAO. If you are going to use the dba3Database object in other places in your code you will want to make it a global in a module, this way you can use it everywhere in your program.

    The SQL and the OpenRecordset statement are just there to show you how to access the tables from the database on the same connection.

    Bad database techniques use many connection to a database. Let say that a database can only support 1000 connections. Each of your apps takes up five connections... Only 200 of your apps can access the database at anytime as opposed to 1000 of your apps if you use good database techniques.

    Just a friendly thought!!! Always keep the end results in mind when writing an app.


    Enjoy

    VB Code:
    1. Global dba3Database As Database
    2. Global AccessDB As Access.Application
    3.  
    4. Private Sub Main()
    5. Dim bln1ExclusiveMode As Boolean
    6. Dim bln1ReadOnly As Boolean
    7. Dim snpTable As Recordset
    8. Dim SQL As String
    9.  
    10. '
    11. '   Declare workspace and open database
    12. '
    13.     bln1ExclusiveMode = True
    14.     bln1ReadOnly = False
    15.     Set AccessDB = New Access.Application
    16.    
    17.     str3Database = "Sam.mdb"
    18.     AccessDB.OpenCurrentDatabase App.Path & "\" & str3Database, bin1exclusivemode
    19.     Set wsp3workspace = DBEngine.Workspaces(0)
    20.     Set dba3Database = AccessDB.CurrentDb
    21.    
    22.     SQL = "Select * from Contacts"
    23.     Set snpTable = dba3Database.OpenRecordset(SQL, dbOpenDynaset, dbReadOnly)
    24.    
    25.     AccessDB.DoCmd.OpenReport "Borrowed", acViewNormal, strFilter, strWhere
    26.     AccessDB.Visible = True
    27.     AccessDB.Quit acQuitSaveAll
    28.     snpTable.Close
    29.     Set snpTable = Nothing
    30.    
    31. End Sub
    32.  
    33. Private Sub Terminate() ' or what ever exit routine you will use
    34.     dba3Database.Close
    35.     Set dba3Database = Nothing
    36.     AccessDB = Nothing
    37. End Sub
    Last edited by randem; Dec 8th, 2002 at 01:50 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
  •  



Click Here to Expand Forum to Full Width