473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

global Database

23
instead to do:

Dim db As Database
Set db= CurrentDb()

so much times

there is way do it one time for all the forms, modules.....? how?

thanks!
Sep 7 '16 #1
1 1412
jforbes
1,107 Expert 1GB
I use this function. It works pretty well for me.
Expand|Select|Wrap|Line Numbers
  1. Public Function dbLocal(Optional bolCleanup As Boolean = False) As DAO.Database
  2.   ' Source: http://stackoverflow.com/questions/1833746/ms-access-is-there-a-significant-overhead-when-using-currentdb-as-opposed-to-db
  3.   ' This function started life based on a suggestion from
  4.   '   Michael Kaplan in comp.databases.ms-access back in the early 2000s
  5.   ' 2003/02/08 DWF added comments to explain it to myself!
  6.   ' 2005/03/18 DWF changed to use Static variable instead
  7.   ' uses GoTos instead of If/Then because:
  8.   '  error of dbCurrent not being Nothing but dbCurrent being closed (3420)
  9.   '  would then be jumping back into the middle of an If/Then statement
  10.   On Error GoTo ErrHandler
  11.     Static dbCurrent As DAO.Database
  12.     Dim strTest As String
  13.  
  14.   If bolCleanup Then GoTo closeDB
  15.  
  16. retryDB:
  17.     If dbCurrent Is Nothing Then
  18.        Set dbCurrent = CurrentDb()
  19.     End If
  20.     ' now that we know the db variable is not Nothing, test if it's Open
  21.     strTest = dbCurrent.Name
  22.  
  23. exitRoutine:
  24.     Set dbLocal = dbCurrent
  25.     Exit Function
  26.  
  27. closeDB:
  28.     If Not (dbCurrent Is Nothing) Then
  29.        'dbCurrent.close ' this never has any effect
  30.        Set dbCurrent = Nothing
  31.     End If
  32.     GoTo exitRoutine
  33.  
  34. ErrHandler:
  35.     Select Case Err.Number
  36.       Case 3420 ' Object invalid or no longer set.
  37.         Set dbCurrent = Nothing
  38.         If Not bolCleanup Then
  39.            Resume retryDB
  40.         Else
  41.            Resume closeDB
  42.         End If
  43.       Case Else
  44.         MsgBox Err.Number & ": " & Err.Description, vbExclamation, "Error in dbLocal()"
  45.         Resume exitRoutine
  46.     End Select
  47. End Function

Here are some examples of usage:
Expand|Select|Wrap|Line Numbers
  1. Public Function test() As Boolean
  2.     On Error GoTo ErrorOut
  3.  
  4.     Dim sSQL As String
  5.     Dim oRst As DAO.Recordset
  6.  
  7.     sSQL = "SELECT * FROM Something"
  8.     Set oRst = dbLocal.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)
  9.     test = oRst.Count
  10. End Function
  11.  
  12. Public Sub executeSQL(ByRef sSQL As String)
  13.     On Error GoTo ErrorOut
  14.     dbLocal.Execute sSQL, dbFailOnError + dbSeeChanges
  15. ExitOut:
  16.     Exit Sub
  17. ErrorOut:
  18.     Call msgBoxError("Could not execute SQL: " & vbCrLf & vbCrLf & Err.Description)
  19.     Resume ExitOut
  20. End Sub
  21.  
  22. Public Function getTableList() As String
  23.     ' Gets a list of Tables, minus System Tables
  24.     Dim oTD As DAO.TableDef
  25.     For Each oTD In dbLocal.TableDefs
  26.         If Not oTD.Name Like "MSys*" Then getTableList = getTableList & ";" & Chr(34) & oTD.Name & Chr(34)
  27.     Next oTD
  28. End Function
Sep 7 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Martin Lucas-Smith | last post by:
I have a class from within which other classes are called. In the constructor, I want to create an instance of a database connection, so that this database can be called elsewhere. <?php #...
0
by: Jack | last post by:
I am setting up a partitioned db environment and am seeing some slow performance when a query goes off the coordinator node. I have run a global database manager snapshot and these numbers raise...
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
12
by: John M | last post by:
Hello, On Microsoft Visual Studio .NET 2003, I want to use some global elements, that can be used in each one of my pages. i.e I put a oleDBConnection on global.asax.vb How can I use it...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
5
by: Slant | last post by:
Here's a question that most will have different answers to. I'm just dying to find a solution that seems halfway automated!! There really are two seperate issues which might be answered by the...
2
by: Spitfire | last post by:
Hi, I am trying to make a web application using C#. I need to access database in every webpage and for that I have created new database connection each time. This is not an efficient way at all....
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
1
by: bigscorpio | last post by:
I am having a desperate struggle finding the correct connection string to connect my VB 2005 project to an Access 2003 database that is password protected! The connection string I using is string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.