473,378 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,378 developers and data experts.

Connecting to Different Databases from MS Access

pod
298 100+
Here is some vba code I use to connect to three types of databases.

The "somefunctioncall" function makes use of the "opendb" function to connect to a database...
You will have to put in your parameters in the latter function and uncomment the "preferred connection string"


You might have some fiddling to do with this code, but it should work... I got those strings off the web by googling "connection strings" ...

Those databases do not have to be on the same PC, they can be on any server as long as you have access to them.

[2014-09-27: z:per OP request, revised code block follows ]
Expand|Select|Wrap|Line Numbers
  1.  
  2.     Public Function opendb() As ADODB.Connection
  3.             ' This function returns an ADODB.Connection object 
  4.             ' which is required for accessing different types of databases
  5.             '
  6.             ' Her we declare three strings to access database other than MS Access files on your network
  7.             ' as it was in my case
  8.             Dim ORACLE_ConnString As String
  9.             Dim SQL_ConnString As String
  10.             Dim MSACCESS_ConnString As String
  11.             '*****************************************************************
  12.             ' This variable is used only when accessing a MS Access database
  13.             ' NOTE: I always keep the form separate from the database when using Access for Database and front end forms
  14.             Dim MSAdbPathAndName As String 
  15.             MSAdbPathAndName = "C:\Reports\Database\msaccessdatabase.mdb"
  16.             'Probably could use a MapPath function here ...
  17.             '*****************************************************************
  18.  
  19.             Dim DBN As String ' "DATABASENAME"
  20.             Dim UID As String '"yourusername"
  21.             Dim PWD As String '"yourPASSWORD"
  22.             Dim DBS As String '"SERVERNAME"
  23.  
  24.             'Here is where you want to set your parameters for connecting to the desired database
  25.             DBS = "thisismywebservername"
  26.             UID = "thisisme"
  27.             PWD = "thisismypassword"
  28.             DBN = "thisismydbname"
  29.  
  30.             'The connection strings are different for each type of database and network setup
  31.             ORACLE_ConnString = "Provider=msdaora;Data Source=" & DBN & _
  32.                                 ";User Id=" & UID & ";Password=" & PWD & ";"
  33.             SQL_ConnString = "Provider=SQLOLEDB;DATA SOURCE=" & DBS & _
  34.                                 ";UID=" & UID & _
  35.                                 ";PWD=" & PWD & _
  36.                                 ";Initial Catalog=" & DBN & ";"
  37.             MSACCESS_ConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
  38.                                     MSAdbPathAndName & ";"
  39.  
  40.             'Here we declare and set the ADODB.Connection
  41.             opendb = New ADODB.Connection
  42.  
  43.             'And finally you must decide which type of connection you are going to use... only one :)
  44.  
  45.             '    *************** preferred connection string ***********
  46.             '    UNCOMMENT the ONE you need: if you want to connect to MS SQL then SQL_ConnString is the one you want
  47.             '    ******************************************************* 
  48.  
  49.             '    opendb.Open ORACLE_ConnString
  50.             opendb.Open(SQL_ConnString)
  51.             '    opendb.Open MSACCESS_ConnString
  52.  
  53.         End Function
  54.         Public Function stringReturningFunctionCall() As String
  55.             ' The connection object is required for accessing the database and must be declared
  56.             Dim objConn As ADODB.Connection
  57.             ' The  recordset object is required for storing the queried dataset and must be declared
  58.             Dim recSet As ADODB.Recordset
  59.             'This functioncal return a string, we declare and set it to be empty
  60.             Dim stringToReturn As String
  61.             stringToReturn = ""
  62.             'setting the connection object from a function that returns an ADODB.Connection 
  63.             objConn = opendb
  64.             'setting the Recordset object using the Connection object to excute the SQL statement
  65.             recSet = objConn.Execute("SELECT [field1],[field2],[field3] FROM [sometable] order by [somefield] ")
  66.             Do While Not rs.EOF ' run through the recordset until at the end
  67.                 'add your own code here
  68.                 stringToReturn = someStringAppendingFunction(stringToReturn, recSet(0) & _
  69.                                                                 ";" & recSet("field2") & _
  70.                                                                 ";" & recSet("field3"), ";")
  71.                 recSet.MoveNext()
  72.             Loop
  73.             'clean up time
  74.             objConn.Close()
  75.             objConn = Nothing
  76.             recSet = Nothing
  77.             'return the result
  78.             stringReturningFunctionCall = stringToReturn
  79.         End Function
Sep 15 '11 #1
17 5855
pod
298 100+
Here is a simplified version of using the "dbopen" function


Expand|Select|Wrap|Line Numbers
  1. Public Function opendb(ConnString As String) As ADODB.Connection 
  2.     Set opendb = New ADODB.Connection 
  3.     opendb.Open ConnString   
  4. End Function
Apr 4 '13 #2
Lysander
344 Expert 100+
Hi Pod,
If this was an answer to a specific question, it would be great. As an article, it would be nice to have more comments in the code and some more explanatiaroy text as to what this does and why it can be useful to people.
Apr 10 '13 #3
pod
298 100+
Lysander, I agree with you.
I will work on a better presentation with a bit more explanatory text to go with it. Thank you.


P:oD

p.s.
Is it just me? I noticed that if I look at this page with Internet Explorer 8, the original portion of the code does not display unless I click "Expand"... with Firefox it displays correctly.
Apr 10 '13 #4
NeoPa
32,556 Expert Mod 16PB
It's not just you Pod ;-)
Apr 10 '13 #5
pod
298 100+
It took a while but there it is with slightly better comments...
If one of the moderators want to put this at the top, you have my blessing :)


P:oD

Expand|Select|Wrap|Line Numbers
  1. Public Function opendb() As ADODB.Connection
  2.         ' This function returns an ADODB.Connection object 
  3.         ' which is required for accessing different types of databases
  4.         '
  5.         ' Her we declare three strings to access database other than MS Access files on your network
  6.         ' as it was in my case
  7.         Dim ORACLE_ConnString As String
  8.         Dim SQL_ConnString As String
  9.         Dim MSACCESS_ConnString As String
  10.         '*****************************************************************
  11.         ' This variable is used only when accessing a MS Access database
  12.         ' NOTE: I always keep the form separate from the database when using Access for Database and front end forms
  13.         Dim MSAdbPathAndName As String 
  14.         MSAdbPathAndName = "C:\Reports\Database\msaccessdatabase.mdb"
  15.         'Probably could use a MapPath function here ...
  16.         '*****************************************************************
  17.  
  18.         Dim DBN As String ' "DATABASENAME"
  19.         Dim UID As String '"yourusername"
  20.         Dim PWD As String '"yourPASSWORD"
  21.         Dim DBS As String '"SERVERNAME"
  22.  
  23.         'Here is where you want to set your parameters for connecting to the desired database
  24.         DBS = "thisismywebservername"
  25.         UID = "thisisme"
  26.         PWD = "thisismypassword"
  27.         DBN = "thisismydbname"
  28.  
  29.         'The connection strings are different for each type of database and network setup
  30.         ORACLE_ConnString = "Provider=msdaora;Data Source=" & DBN & _
  31.                             ";User Id=" & UID & ";Password=" & PWD & ";"
  32.         SQL_ConnString = "Provider=SQLOLEDB;DATA SOURCE=" & DBS & _
  33.                             ";UID=" & UID & _
  34.                             ";PWD=" & PWD & _
  35.                             ";Initial Catalog=" & DBN & ";"
  36.         MSACCESS_ConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
  37.                                 MSAdbPathAndName & ";"
  38.  
  39.         'Here we declare and set the ADODB.Connection
  40.         opendb = New ADODB.Connection
  41.  
  42.         'And finally you must decide which type of connection you are going to use... only one :)
  43.  
  44.         '    *************** preferred connection string ***********
  45.         '    UNCOMMENT the ONE you need: if you want to connect to MS SQL then SQL_ConnString is the one you want
  46.         '    ******************************************************* 
  47.  
  48.         '    opendb.Open ORACLE_ConnString
  49.         opendb.Open(SQL_ConnString)
  50.         '    opendb.Open MSACCESS_ConnString
  51.  
  52.     End Function
  53.     Public Function stringReturningFunctionCall() As String
  54.         ' The connection object is required for accessing the database and must be declared
  55.         Dim objConn As ADODB.Connection
  56.         ' The  recordset object is required for storing the queried dataset and must be declared
  57.         Dim recSet As ADODB.Recordset
  58.         'This functioncal return a string, we declare and set it to be empty
  59.         Dim stringToReturn As String
  60.         stringToReturn = ""
  61.         'setting the connection object from a function that returns an ADODB.Connection 
  62.         objConn = opendb
  63.         'setting the Recordset object using the Connection object to excute the SQL statement
  64.         recSet = objConn.Execute("SELECT [field1],[field2],[field3] FROM [sometable] order by [somefield] ")
  65.         Do While Not rs.EOF ' run through the recordset until at the end
  66.             'add your own code here
  67.             stringToReturn = someStringAppendingFunction(stringToReturn, recSet(0) & _
  68.                                                             ";" & recSet("field2") & _
  69.                                                             ";" & recSet("field3"), ";")
  70.             recSet.MoveNext()
  71.         Loop
  72.         'clean up time
  73.         objConn.Close()
  74.         objConn = Nothing
  75.         recSet = Nothing
  76.         'return the result
  77.         stringReturningFunctionCall = stringToReturn
  78.     End Function
  79.  
Sep 23 '14 #6
NeoPa
32,556 Expert Mod 16PB
What exactly do you want me to do here? Replace the original code in the first post with this new version?
Sep 25 '14 #7
pod
298 100+
Bonjour NeoPa

Yes, I think it would be nicer to have better comments right at the top.

Thanks


P:oD
Sep 25 '14 #8
zmbd
5,501 Expert Mod 4TB
I had started to ask that same question. (^_^)

Pod, with your kind permission, if NeoPa hasn't done this by this afternoon, I'll copy the new code to the OP replacing the original code and clean up the thread.
Sep 26 '14 #9
pod
298 100+
Yes you may "do that voodoo that you do so well"

Can someone tell me where I stole this quote ?

P:oD
Sep 26 '14 #10
zmbd
5,501 Expert Mod 4TB
"do that voodoo that you do so well"
Cole Porter, Ella Fitzgerald, Blazing Saddles...

Please double check the revised code, Simple copy and paste but goofyness is known to happen.
Once you're happy, I'll clean the the thread up.
Sep 27 '14 #11
pod
298 100+
Funny, I must have watched Blazzing Saddles over 50 times when I was younger but I did not remember it was in it ... I've heard, Ella's version but not Cole Porter. MASH's Colonel Potter also says that line in the episode where Winchester gets his back pain cured by acupuncture performed by three visiting buddhist doctors refered as Larry, Curly and Moe ... a classic :)
Sep 29 '14 #12
pod
298 100+
Zed, it looks fine, the logic is what counts anyway...

Wrap it up

Thank you
Sep 29 '14 #13
zmbd
5,501 Expert Mod 4TB
I forgot about the M*A*S*H episode.
That was a show. They managed to jerk us from ROTFL to out-right bawling... but that was when writers could write.

Well worth a visit thru youtube to find the Cole Porter version - just because it's a classic.

Anyway, I'll clean up the thread here in an little bit (^_^)
Sep 29 '14 #14
NeoPa
32,556 Expert Mod 16PB
Sorry PoD. I was away.

@Z.
Don't forget to clean up if you're all ready. This post can go too of course.
Oct 2 '14 #15
pod
298 100+
Knock, knock Neo ... did you follow the white rabbit? :)
Oct 2 '14 #16
NeoPa
32,556 Expert Mod 16PB
:-D

I seem to recall having him to a tea-party once. Always in a rush!
Oct 2 '14 #17
zmbd
5,501 Expert Mod 4TB
I knew I should have taken the blue pill
Oct 2 '14 #18

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

Similar topics

3
by: Shaun | last post by:
Hi, I have recently been given an ASP site to administer, however I cannot get the site to connect to the access the Access database. This is the current connection file: <% '...
1
by: Dishaa V | last post by:
Hello I need to connect to an access db which is on the internet. I do know that URL of the same. its http://www.vallury.com/balance/bank.mdb. Its just a test db. How can I connect to that db...
3
by: Bart Schelkens | last post by:
Hi, i have this website which connects to an Access-database. I've made 2 local copies : 1 on a Win2000 and 1 on a Win XP. When I start the site on my Win2000 I don't have any problem. When...
7
by: molemoore | last post by:
Im trying to connect to an Access database using a class. I think I have been able to establish the connection but when trying to test it by pulling data from the database im getting the error: ...
2
by: campbellbrian2001 | last post by:
Thanks in advance! I have recently started migrating my MS-Acesss front-end from the Access platform to a VB.net2003 front-end for more flexibility. The access db is not split. I am having trouble...
1
by: msch-prv | last post by:
I have difficulties declaring and using an Access connection string. The web.config declaration is: <connectionStrings> <add name="DbConn" connectionString="~/App_Data/CRM2K_be.mdb"...
1
by: =?Utf-8?B?VGVycnk=?= | last post by:
Hi, I am a newbe here and am working through some books on using ADO.Net. These database books were writen for VS2003. I have both VS2003 andVS2005. In VS2003 all works fine. Right click 'Data...
5
by: Anthony1312002 | last post by:
Hello everyone. I just recently moved to Windows Vista Ultimate from XP Pro on my desktop machine which of course means I'm running IIS 7. I'm able to view asp pages that don't connect to an access...
1
by: Yew12 | last post by:
Hi, I'm having problems connecting to a Microsoft Access database. Im using PHP and ODBC but im not using a DNS. The scripts below give no errors but I they only give me a blank white screen. ...
3
by: armyofus | last post by:
I want to access a MS Access database where I can pull information from and display it. The trouble is, I cannot get my code to access it. When I look at my code it just doesn't seem right, too. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.