473,473 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Linking tables in SQL via DAO

8 New Member
I am trying to come up with a "hands free" and "DSN-less" way for MS Access 2010 to dynamically link to six tables on SQL server 2010. I found the below code on WiseOwl.co.uk(Posted by David Wakefield on 26 June 2012) but I'm embarrassed to say I don't know where to put this code. Does it go inside a module as a function and then get called in a macro as "runcode"? If it gets called as a function, how do I reference the function in the RunCode? I need some help with the "missing" pieces I need to make this work, as I am not very familiar with VBA or modules, but I think VBA is the only way I can get this dynamic linking to work without a DSN. Also, I don't know how to get this code to dynamically loop through a list of tables and keys, to automatically link all six tables without user intervention.

David's post says...

Using DAO

The following code is slightly more long winded, but crucially it allows you to set the key field(s) in code:


Expand|Select|Wrap|Line Numbers
  1. ' we will need to create this table using DAO
  2. Dim tdf As DAO.TableDef
  3.  
  4. ' Some variable to make the code more generic
  5. Dim strConnectionString As String
  6. Dim strNameInAccess As String
  7. Dim strNameInSQLServer As String
  8. Dim strKey As String
  9.  
  10.  
  11. ' set the connection string
  12. strConnectionString = _
  13.    "ODBC;DRIVER=SQL Server; " & _
  14.    "SERVER=.\SQLExpress;DATABASE=MyDatabase;Trusted_Connection=Yes"
  15.  
  16.  
  17. ' specify the tables you want to link. The table can be
  18. ' known by a different name in Access than the name in SQL server
  19.  
  20. strNameInAccess = "tblYacht"
  21. strNameInSQLServer = "tblSailingBoat"
  22.  
  23. ' specify the key field
  24. strKey = "SailingBoatID"
  25.  
  26. ' Delete the table from the local database if it exists
  27. On Error Resume Next
  28.  
  29. DoCmd.RunSQL "drop table " & strNameInAccess
  30.  
  31. On Error GoTo 0
  32.  
  33. ' Create a table using DAO give it a name in Access.
  34. ' Connect it to the SQL Server database.
  35. ' Say which table it links to in SQL Server.
  36. Set tdf = CurrentDb.CreateTableDef(strNameInAccess)
  37.  
  38. tdf.Connect = strConnectionString
  39. tdf.SourceTableName = strNameInSQLServer
  40.  
  41. ' Add this table Definition to the collection
  42. ' of Access tables
  43. CurrentDb.TableDefs.Append tdf
  44.  
  45. ' Now create a unique key for this table by
  46. ' running this SQL
  47.  
  48. On Error Resume Next
  49.  
  50. DoCmd.RunSQL _
  51.    "CREATE UNIQUE INDEX UniqueIndex ON " _
  52.    & strNameInAccess & " (" & strKey & ")"
  53.  
  54. On Error GoTo 0
May 21 '14 #1
1 3321
zmbd
5,501 Recognized Expert Moderator Expert
This is a more robust explanation of the method...

Using DSN-Less Connections (by Doug Steele (Access MVP))

You really MUST have a firm Coding background before you get into the deep-end with this integration. There are still a lot things that I run into with between the servers and my frontend that without a good core-understanding I'd be lost.
May 21 '14 #2

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

Similar topics

3
by: Tym | last post by:
OK - daft question of the day time... If I have database A which contains all the live data, and Database B which contains linked tables to those is A (i.e. a front end) is there a way of seeing...
18
by: Mark P | last post by:
I have a bit of vb code that uses Tables.Append to programatically link tables from Oracle and DB2 datasources. The problem I am having on some client machines is that the link will take anywhere...
5
by: Christoph Sticksel | last post by:
Hi, I'm having problems with attaching two tables stored in an SQL Server 2000 to an Access 97 database. It worked well for all other tables except those two. This is what I did: Choose the...
2
by: Matthew Wells | last post by:
Good morning... I have an Access front end that uses SQL Server linked tables. SQL Server uses Windows authentication. I have one Windows group that all Access users are a member of. I added...
1
by: deiopajw | last post by:
I have a Back end database on a network drive. The copies of the front end are located on individual pc's (in their C drive). The problem arises when a laptop user naturally hooks up to the...
4
by: trevordixon | last post by:
Does linking tables with JOIN offer any significant performance advantage or does it just make things more simple from a programming standpoint? Trevor
2
by: watto | last post by:
I have a large database application with user data in a backend and forms, code etc in a frontend. It includes a facility for migrating data from an earlier backend to the current backend by...
1
by: bhobbs | last post by:
We have a PostgreSQL database that contains several linking tables (all they contain is two foreign keys to link 2 other tables together). Is it necessary and/or advisable to specify the two...
3
by: misscrf | last post by:
In a database, I am creating a new db. From there, I am setting up the tables, so that I can eventually create a front end (usually access, but I may attempt to be brave and lose the shell.) ...
7
by: Dopey Fletcher | last post by:
Hello all, Current Situation Access (front end) MSSQL (back end) We have 2 environments, one test and one production We develop in the test environment, and users work in the production...
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...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.