473,506 Members | 9,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create DSN and Link SQL Table in Access - Solution

I know this works for Access 97. Not sure about other versions.

This will create a DSN to a SQL server in your ODBC connections and
then link a table of your choise from that SQL DB to the Access
Database. You could modify it anyway you like. Such as passing
variables to the values you have to change vs. manually typing them in.
Microsoft uses a table to pass the data here...
http://support.microsoft.com/kb/q159691/

I just changed it a bit because I wasn't crazy about having that info
in a table that could accidently be deleted or whatever.

__________________________________________________ __
Function DoesTblExist(strTblName As String) As Boolean
On Error Resume Next
Dim db As Database, tbl As TableDef
Set db = CurrentDb
Set tbl = db.TableDefs(strTblName)
If Err.Number = 3265 Then ' Item not found.
DoesTblExist = False
Exit Function
End If
DoesTblExist = True
End Function

Function CreateODBCLinkedTables() As Boolean
On Error GoTo CreateODBCLinkedTables_Err
Dim strTblName As String, strConn As String
Dim db As Database, rs As Recordset, tbl As TableDef
' ---------------------------------------------
' Register ODBC database(s)
' ---------------------------------------------
Set db = CurrentDb
DBEngine.RegisterDatabase "nameofdsn", _
"SQL Server", _
True, _
"Description=DSNdescription" & _
Chr(13) & "Server=SQLservername" & _
Chr(13) & "Database=SQLdbName"
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = "AccessTableName"
strConn = "ODBC;"
strConn = strConn & "DSN=nameofdsn;"
strConn = strConn & "APP=Microsoft Access;"
strConn = strConn & "DATABASE=SQLdbname;"
strConn = strConn & "UID=username;"
strConn = strConn & "PWD=password;"
strConn = strConn & "TABLE=SQLtablename"
If (DoesTblExist(strTblName) = False) Then
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, ("SQLtablename"), _
strConn)
db.TableDefs.Append tbl
Else
Set tbl = db.TableDefs(strTblName)
tbl.Connect = strConn
tbl.RefreshLink
End If

CreateODBCLinkedTables = True
MsgBox "Refreshed ODBC Data Sources", vbInformation
CreateODBCLinkedTables_End:
Exit Function
CreateODBCLinkedTables_Err:
MsgBox Err.Description, vbCritical, "MyApp"
Resume CreateODBCLinkedTables_End
End Function
________________________________________________

Not sure if this is the most streamlined way to do it but it works.

Have Fun !

Nov 13 '05 #1
1 9392
Even easier not to bother with the DSN!

Check http://www.accessmvp.com/DJSteele/DSNLessLinks.html for one approach.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Bruce Lawrence" <BL*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I know this works for Access 97. Not sure about other versions.

This will create a DSN to a SQL server in your ODBC connections and
then link a table of your choise from that SQL DB to the Access
Database. You could modify it anyway you like. Such as passing
variables to the values you have to change vs. manually typing them in.
Microsoft uses a table to pass the data here...
http://support.microsoft.com/kb/q159691/

I just changed it a bit because I wasn't crazy about having that info
in a table that could accidently be deleted or whatever.

__________________________________________________ __
Function DoesTblExist(strTblName As String) As Boolean
On Error Resume Next
Dim db As Database, tbl As TableDef
Set db = CurrentDb
Set tbl = db.TableDefs(strTblName)
If Err.Number = 3265 Then ' Item not found.
DoesTblExist = False
Exit Function
End If
DoesTblExist = True
End Function

Function CreateODBCLinkedTables() As Boolean
On Error GoTo CreateODBCLinkedTables_Err
Dim strTblName As String, strConn As String
Dim db As Database, rs As Recordset, tbl As TableDef
' ---------------------------------------------
' Register ODBC database(s)
' ---------------------------------------------
Set db = CurrentDb
DBEngine.RegisterDatabase "nameofdsn", _
"SQL Server", _
True, _
"Description=DSNdescription" & _
Chr(13) & "Server=SQLservername" & _
Chr(13) & "Database=SQLdbName"
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = "AccessTableName"
strConn = "ODBC;"
strConn = strConn & "DSN=nameofdsn;"
strConn = strConn & "APP=Microsoft Access;"
strConn = strConn & "DATABASE=SQLdbname;"
strConn = strConn & "UID=username;"
strConn = strConn & "PWD=password;"
strConn = strConn & "TABLE=SQLtablename"
If (DoesTblExist(strTblName) = False) Then
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, ("SQLtablename"), _
strConn)
db.TableDefs.Append tbl
Else
Set tbl = db.TableDefs(strTblName)
tbl.Connect = strConn
tbl.RefreshLink
End If

CreateODBCLinkedTables = True
MsgBox "Refreshed ODBC Data Sources", vbInformation
CreateODBCLinkedTables_End:
Exit Function
CreateODBCLinkedTables_Err:
MsgBox Err.Description, vbCritical, "MyApp"
Resume CreateODBCLinkedTables_End
End Function
________________________________________________

Not sure if this is the most streamlined way to do it but it works.

Have Fun !

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
11205
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
3
6969
by: G rumpy O ld D uffer | last post by:
This is probably a 'Low-Level' question to all the ACCESS experts but I've only been using ACCESS for a couple of weeks. I've been given 30+ (and counting) separate 'Weekly' Databases which all...
1
3338
by: poohnie08 | last post by:
i have a excel spreadsheet showing staff name, date,work hour, ot hour, slot1, slot2, slot3, slot4 and others). The "()" will keep repeating from day 1 until end of month. eg in excel spreadsheet,...
6
2465
by: skgolden | last post by:
My husband and I own a small temporary labor company and deal with 4 major clients (A,B,C & D), each of which has about 2 dozen units in our tristate area that we deal with (ie, Company A, units...
1
12198
by: Andrew Chanter | last post by:
I am writing a little routine to perform the following operations from an Acces 97 mdb: 1. create a fixed width text file 2. create/establish a table type link to the text file in Access 3....
3
10564
by: Kent | last post by:
I am try to link a SQL 2000 table to Access 2000 using VB.Net 2003 Here is my code: Dim Con As New ADODB.Connection Dim Cat As New ADOX.Catalog Dim tbl As New ADOX.Table ...
4
8103
by: Wayne Wengert | last post by:
I am trying to create a VB.NET Windows application to move some data from a local Access DB table to a table in a SQL Server. The approach I am trying is to open an OLEDB connection to the local...
4
3250
by: Owen Jenkins | last post by:
Hi, No-one replied to this when I sent it last week. Any bites out there today?? ----- My application allows users to create a new back end for separate purposes. It does this by using Make...
15
7719
by: Killer42 | last post by:
Hi all. Ok, I'm using VB6 but I think the answer to this (if there is one) is more likely to be found in the Access forum. I have a situation where I've got tens of millions of records, spread...
0
7105
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...
1
7023
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
7479
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...
0
5617
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
5037
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
4702
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
3188
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
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
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.