473,395 Members | 1,677 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,395 developers and data experts.

Managing DSN-Less Connections

Seth Schrock
2,965 Expert 2GB
Introduction
Until just recently, I would use system DSNs for all of my MS Access databases that connected to MS SQL Server backends. It is really easy to setup and didn't require all the code that I was seeing online for the DSN-Less connections. However, it is a pain to try to switch to a test server, make sure that you have the correct DSN setup on the client computer (changes depending on 32 or 64 bit client system), etc. So now I have made the switch to DSN-Less connections and decided to post how I did it (since I can't find everything that I need online).

Setup
First, I created two local tables to store the different SQL Server locations and another on for the list of tables and where they should be connected to.
Expand|Select|Wrap|Line Numbers
  1. tblBE
  2. BE_ID_pk      AutoNumber
  3. Driver        Text
  4. Server        Text
  5. DatabaseName  Text
  6.  
  7.  
  8.  
  9. tblTableLocation
  10. TableID_pk       AutoNumber
  11. TableName        Text
  12. RemoteTableName  Text
  13. LocationID_fk    Number
  14.  
  15. One-to-Many join on tblBE.BE_ID_pk = tblTableLocation.LocationID_fk
Code
First, I have the code to add a table to the TableDefs collection. This function adds the table to the TableDefs and then adds the table to the table list in tblTableLocation.
Expand|Select|Wrap|Line Numbers
  1. Public Function AddTable(TableName As String, RemoteTable As String, Location As Long) As Boolean
  2. On Error GoTo Error_Handler
  3.  
  4. Dim db As DAO.Database
  5. Dim td As TableDef
  6. Dim strCon As String
  7. Dim strAddQry As String
  8.  
  9. Set db = CurrentDb
  10. strCon = "ODBC;" & GetLocation(Location) & "Trusted_Connection=YES"
  11. Set td = db.CreateTableDef(TableName, dbAttachSavePWD, RemoteTable, strCon)
  12.  
  13. db.TableDefs.Append td
  14.  
  15. strAddQry = "INSERT INTO tblTableLocation (TableName, RemoteTableName, LocationID_fk) " & _
  16.             "VALUES ('" & TableName & "', '" & RemoteTable & "', " & Location & ")"
  17.  
  18. db.Execute strAddQry, dbFailOnError
  19.  
  20. AddTable = True
  21.  
  22. Exit_Procedure:
  23.     Set db = Nothing
  24.     Set td = Nothing
  25.     Exit Function
  26.  
  27. Error_Handler:
  28.     AddTable = False
  29.     Resume Exit_Procedure
  30.  
  31. End Function
  32.  
  33.  
  34.  
  35. Private Function GetLocation(LocationID As Long) As String
  36. On Error GoTo Error_Handler
  37.  
  38. Dim db As DAO.Database
  39. Dim rst As DAO.Recordset
  40. Dim strRst As String
  41. Dim strLoc As String
  42.  
  43. strRst = "SELECT * FROM tblBE WHERE BE_ID_pk = " & LocationID
  44.  
  45. Set db = CurrentDb
  46. Set rst = db.OpenRecordset(strRst, dbOpenDynaset)
  47.  
  48. With rst
  49.     strLoc = "DRIVER=" & !Driver & ";SERVER=" & !Server & ";DATABASE=" & !DatabaseName & ";"
  50. End With
  51.  
  52. GetLocation = strLoc
  53.  
  54. Exit_Procedure:
  55.     On Error Resume Next
  56.     rst.Close
  57.     Set db = Nothing
  58.     Set rst = Nothing
  59.  
  60.     Exit Function
  61.  
  62. Error_Handler:
  63.     Resume Exit_Procedure
  64.  
  65. End Function
The parameters are as follows
  1. TableName: The name that you want to appear in Access
  2. RemoteTable: The name of the table in SQL Server (includes the schema name, ie. dbo.Customers)
  3. Location: The PK value from tblBE for the server/database that you want to connect to. The location information comes from the GetLocation() function.

The code to move a table between servers is very much the same, but before it adds the table to the TableDefs collection, it first deletes it. So the linked table from the old location is deleted and then a new linked table is created that is linked to the new location.
Expand|Select|Wrap|Line Numbers
  1. Public Function MoveTable(LocalTable As String, RemoteTable As String, Location As Long) As Boolean
  2. On Error GoTo Error_Handler
  3.  
  4. Dim td As TableDef
  5. Dim strCon As String
  6. Dim db As DAO.Database
  7.  
  8. Set db = CurrentDb
  9.  
  10. db.TableDefs.Delete LocalTable
  11.  
  12. strCon = "ODBC;" & GetLocation(Location) & ";Trusted_Connection=YES"
  13.  
  14. Set td = db.CreateTableDef(LocalTable, dbAttachSavePWD, RemoteTable, strCon)
  15. db.TableDefs.Append td
  16.  
  17. MoveTable = True
  18.  
  19. Exit_Procedure:
  20.     Set db = Nothing
  21.     Set td = Nothing
  22.  
  23.     Exit Function
  24.  
  25. Error_Handler:
  26.     AttachDSNLessTable = False
  27.     Resume Exit_Procedure
  28.  
  29. End Function

Calling the Code
I have an unbound form that allows me to enter all my information in textboxes and comboboxes. The exact design isn't important, but I have two sections: one to add a table and one to move tables to another server.

First, adding a table. A textbox for the local table name, remote table name, and a combobox for the server/database. I think that the control names are fairly obvious as to what they are.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAttachTable_Click()
  2. On Error GoTo Error_Handler
  3.  
  4. If Me.txtLTable & "" <> "" And Me.txtRTable & "" <> "" And Not IsNull(Me.cboLoc) Then
  5.     If AddTable(Me.txtLTable, Me.txtRTable, Me.cboLoc) = False Then
  6.         MsgBox "Attempt to attach " & Me.txtLTable & " to the database failed.", vbCritical
  7.  
  8.     End If
  9. Else
  10.     MsgBox "All three fields need to be populated in order to attach a table.", vbInformation
  11. End If
  12.  
  13. Exit_Procedure:
  14.     Exit Sub
  15.  
  16. Error_Handler:
  17.     Resume Exit_Procedure
  18.  
  19. End Sub
Moving the tables. Comboboxes for the From and To server/database.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdChangeBackend_Click()
  2. On Error GoTo Error_Handler
  3.  
  4. Dim strTables As String
  5. Dim db As DAO.Database
  6. Dim rstTables As DAO.Recordset
  7. Dim strMsg As String
  8.  
  9. If Not IsNull(Me.cboFrom) And Not IsNull(Me.cboTo) Then
  10.     strTables = "SELECT * FROM tblTableLocation WHERE LocationID_fk = " & Me.cboFrom
  11.  
  12.     Set db = CurrentDb
  13.     Set rstTables = db.OpenRecordset(strTables, dbOpenDynaset)
  14.  
  15.     With rstTables
  16.         Do While Not .EOF
  17.  
  18.             If AttachDSNLessTable(!TableName, !RemoteTableName, Me.cboTo) = False Then
  19.                 strMsg = "Connection attempt for " & !TableName & " failed."
  20.                 MsgBox strMsg, vbCritical, "Connection Attempt Failed"
  21.             Else
  22.                 .Edit
  23.                 !LocationID_fk = Me.cboTo
  24.                 .Update
  25.             End If
  26.  
  27.             .MoveNext
  28.         Loop
  29.  
  30.     End With
  31. Else
  32.     MsgBox "Both drop-down boxes must have values"
  33. End If
  34.  
  35. Exit_Procedure:
  36.     On Error Resume Next
  37.     rstTables.Close
  38.  
  39.     Set db = Nothing
  40.     Set rstTables = Nothing
  41.  
  42.     Exit Sub
  43.  
  44. Error_Handler:
  45.     Resume Exit_Procedure
  46.  
  47. End Sub
All of my SQL Server backend databases have two databases that they connect to. One is the database for the data and one is an Employee database that lists what their permissions are for that database (A custom built database that I did so that I don't have to create it for every database that I do. I might make another article about it later.). Because of this, I can't just move all tables from one server to another. Having the From combobox lets me only move the tables that are one server/database to another server/database and leave the other tables alone.

Conclusion
I have already used this once and I can tell you it is so much simpler than using the Linked Table Manager and making it ask for a new location. For me, it asks for the new location for every single table. When working with 10+ tables (I've got one right now with 24), this is very annoying. Now all I have to do is make two selections and click a button. Done.
Jul 16 '14 #1
4 10620
NeoPa
32,556 Expert Mod 16PB
Hi Seth.

Nice work :-)

Have you considered using the techniques included in Relinking Tables using VBA to adjust your process so that it relinks the existing tables rather than deletes them and recreates them. An advantage of this approach is that it doesn't lose any saved changes to the TableDef that might be useful for a designer to have available when they open it up. Obviously, there may be other reasons to keep the existing TableDefs but that one springs to mind.
Aug 3 '14 #2
zmbd
5,501 Expert Mod 4TB
Neopa has a good point there and I have a variation on a theme, basically the same code, that I've used for years because the first time I split databases I lost a ton of stuff and had to rebuild things... made me read more though (^_^)
Aug 4 '14 #3
Seth Schrock
2,965 Expert 2GB
I'll confess that I hadn't because I only look at that article to relink to Access backends. Strange since the title says ODBC Tables :) I'll admit that I don't know of an example of changes to the TableDef that would be lost nor any other reasons not to delete the table, but I will take your word for it that there is and redo my code to make it just change the connection string.

I assume that it is the first post that I need to look at and not the second one?
Aug 4 '14 #4
NeoPa
32,556 Expert Mod 16PB
There are various saveable settings you may choose to have associated with a Front-End table Seth. Sort order for instance. Another might be the column width for a particular column, or even the order the fields are displayed in.

As for the changes to the code - I expect it would be very similar in essence to the code I posted in the linked thread at post #2. Although there are 62 lines there in all, only 2 of them are directly involved in making the change. If you look at lines #31 & #33 you'll see it's simply a matter of changing the .Connect property followed by calling the .Relink() method.

Clearly, it makes sense to check that it worked, which is what much of the other lines of code are about, but the meat and drink are in those two lines. I expect porting that across to your own article would be pretty straightforward for you.
Aug 6 '14 #5

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

Similar topics

4
by: shank | last post by:
I'm trying to setup an Access DSNLess connection on my local computer. The following string works fine within the Dreamweaver interface. But when I upload to my server (locally) I get this error....
4
by: pardhi a via SQLMonster.com | last post by:
Hi I am a dba for sql server recently we are facing problems with the no of connections. we have a database called ( x ) every day almost million users is using that databse after some time...
2
by: Tony Epton | last post by:
<crossposted to several groups> Is there any way to create a DSNless connection to SQL server from access 97 (DAO) All the samples of connection strings I am finding - all have a reference to a...
2
by: banduraj | last post by:
I am working on starting and managing TCP connections manually. I build the IP headers and TCP packets manually and send them on my own. The problems I run into seems to be related to the Sockets....
2
by: Bijoy Naick | last post by:
I have a ASPX page which needs to make several writes to a database, using data from a form. I am using an MS Access db. Currently, I am managing my connections/writes as follows.. ...
14
by: Nick Gilbert | last post by:
Hi, I have an asp.net application which runs from a CD-ROM using Cassini. As such, it is single user only. The application connects to an Access database when it is loaded, and keeps the same...
1
by: AJ | last post by:
Hi all, Just throwing something out to the newsgroup to get a feel for what others are doing. I am noticing that with my current coding practice, every time i want to update a database i...
4
by: Mike | last post by:
Assume i have a db connection and a datareader for that connection. Now I perform a query that retrieves 100MB of data. I know that the data reader gets 1 row at a time. However, there is a...
9
by: Greg | last post by:
I'm creating a tcp socket connection from the thread in the c# threadpool. Since the default workers thread is 500, sometimes my program tries to open up 500 different tcp socket connections and...
5
by: Cirene | last post by:
I just deployed my new ASP.NET (3.5 FW) site to the hosting company I'm using, webhost4life. NOTE: I HAVE deployed other SQL Server sites to the same account with no issues. Now I'm getting...
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: 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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.