473,665 Members | 2,827 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(st rTblName As String) As Boolean
On Error Resume Next
Dim db As Database, tbl As TableDef
Set db = CurrentDb
Set tbl = db.TableDefs(st rTblName)
If Err.Number = 3265 Then ' Item not found.
DoesTblExist = False
Exit Function
End If
DoesTblExist = True
End Function

Function CreateODBCLinke dTables() As Boolean
On Error GoTo CreateODBCLinke dTables_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.Regist erDatabase "nameofdsn" , _
"SQL Server", _
True, _
"Description=DS Ndescription" & _
Chr(13) & "Server=SQLserv ername" & _
Chr(13) & "Database=SQLdb Name"
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = "AccessTableNam e"
strConn = "ODBC;"
strConn = strConn & "DSN=nameofdsn; "
strConn = strConn & "APP=Micros oft Access;"
strConn = strConn & "DATABASE=SQLdb name;"
strConn = strConn & "UID=userna me;"
strConn = strConn & "PWD=passwo rd;"
strConn = strConn & "TABLE=SQLtable name"
If (DoesTblExist(s trTblName) = False) Then
Set tbl = db.CreateTableD ef(strTblName, _
dbAttachSavePWD , ("SQLtablename" ), _
strConn)
db.TableDefs.Ap pend tbl
Else
Set tbl = db.TableDefs(st rTblName)
tbl.Connect = strConn
tbl.RefreshLink
End If

CreateODBCLinke dTables = True
MsgBox "Refreshed ODBC Data Sources", vbInformation
CreateODBCLinke dTables_End:
Exit Function
CreateODBCLinke dTables_Err:
MsgBox Err.Description , vbCritical, "MyApp"
Resume CreateODBCLinke dTables_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 9412
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.goo glegroups.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(st rTblName As String) As Boolean
On Error Resume Next
Dim db As Database, tbl As TableDef
Set db = CurrentDb
Set tbl = db.TableDefs(st rTblName)
If Err.Number = 3265 Then ' Item not found.
DoesTblExist = False
Exit Function
End If
DoesTblExist = True
End Function

Function CreateODBCLinke dTables() As Boolean
On Error GoTo CreateODBCLinke dTables_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.Regist erDatabase "nameofdsn" , _
"SQL Server", _
True, _
"Description=DS Ndescription" & _
Chr(13) & "Server=SQLserv ername" & _
Chr(13) & "Database=SQLdb Name"
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = "AccessTableNam e"
strConn = "ODBC;"
strConn = strConn & "DSN=nameofdsn; "
strConn = strConn & "APP=Micros oft Access;"
strConn = strConn & "DATABASE=SQLdb name;"
strConn = strConn & "UID=userna me;"
strConn = strConn & "PWD=passwo rd;"
strConn = strConn & "TABLE=SQLtable name"
If (DoesTblExist(s trTblName) = False) Then
Set tbl = db.CreateTableD ef(strTblName, _
dbAttachSavePWD , ("SQLtablename" ), _
strConn)
db.TableDefs.Ap pend tbl
Else
Set tbl = db.TableDefs(st rTblName)
tbl.Connect = strConn
tbl.RefreshLink
End If

CreateODBCLinke dTables = True
MsgBox "Refreshed ODBC Data Sources", vbInformation
CreateODBCLinke dTables_End:
Exit Function
CreateODBCLinke dTables_Err:
MsgBox Err.Description , vbCritical, "MyApp"
Resume CreateODBCLinke dTables_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
11224
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 only one column: txtOutput I want to use the DB front end (MS Access) to send the text string to the SQL backend, then have the SQL Server create a file to a path, such as F:/myfiledate.txt that holds the text in txtOutput, then the trigger...
3
6993
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 have a Table, in exactly the same Field format, in each of these 'Weekly' DataBases. I want to set-up a new 'Master' Database, so that I can write one Query to search all the 'Weekly' Databases and produce one new Table in the 'Master' Database...
1
3349
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, ============================================================================== A1 |A2 A3 A4 A5 A6 A7 A8 |A9 A10 A11 | 01/02/04 |02/02/04 StaffName |Work Hr OT Hr Slot1...
6
2478
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 a,b,c,d,e etc). We send temps possessing various skills (cashier, cook, hostess, etc) to the individual units as needed. What I'm trying to do is create a schedule we can use each day, for example: Monday, June 1, 2004 Company A, unit e Alice...
1
12247
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. allow the user to view the text contained in the text file via an Access form. I have been able to achieve this, albeit with one major limitation. That is that the name of the text file needs to be known prior to runtime. In other
3
10578
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 Con.Open(m_sAccessDbPath) Cat.ActiveConnection = Con
4
8119
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 Access DB and then add a Linked Table pointing to the table on the SQL Server and then run an "Insert Into (linked table)" query to add the new rows. I am having a problem getting the syntax to add that linked table to my local Access DB. When I...
4
3259
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 Table Queries and Indexing via SQL. For example ...
15
7781
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 over a bunch of separate databases. I don't have a choice about this, as the data is simply too large for a single MDB. As yet, I'm not prepared to go to SQL Server. Possibly in the not-too-distant future, but we'll see. In the meantime, I have this...
0
8438
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7376
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4186
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2765
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 we have to send another system
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.