473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Link SQL table through VB Code

I have an Access97 database and I would like to link a SQL table to it
without having to create a manual ODBC connection on every system that
needs to use this database.

This is what I have so far to make the ODBC connection:

Dim DBconnect As Object
Dim CnnStr As String

CnnStr = "Provider=sqloledb;" & _
"Data Source=servername;" & _
"Initial Catalog=odbc-connection-name;" & _
"User ID=username;Password=;"

Set DBconnect = CreateObject("ADODB.Connection")
DBconnect.ConnectionString = CnnStr

This code works fine to create the connection. What I need now, is the
code to create the actual link to the "openord" table in the "proddata"
database on that SQL server.

I guess it would be a good idea to check to see if the table exists and
if it does to skip the link connection. Otherwise it may give an error
that the link is already there?

Thanks for any help !

Nov 13 '05 #1
7 12612
Check what I have at http://www.accessmvp.com/DJSteele/DSNLessLinks.html

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

"Bruce Lawrence" <dg*********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have an Access97 database and I would like to link a SQL table to it
without having to create a manual ODBC connection on every system that
needs to use this database.

This is what I have so far to make the ODBC connection:

Dim DBconnect As Object
Dim CnnStr As String

CnnStr = "Provider=sqloledb;" & _
"Data Source=servername;" & _
"Initial Catalog=odbc-connection-name;" & _
"User ID=username;Password=;"

Set DBconnect = CreateObject("ADODB.Connection")
DBconnect.ConnectionString = CnnStr

This code works fine to create the connection. What I need now, is the
code to create the actual link to the "openord" table in the "proddata"
database on that SQL server.

I guess it would be a good idea to check to see if the table exists and
if it does to skip the link connection. Otherwise it may give an error
that the link is already there?

Thanks for any help !

Nov 13 '05 #2
Bruce:

The following KB articles shows how to link an external table. The sample
code shows how to check for the existence of an existing table, as well as
link to an external table. This sample code uses DAO as opposed to ADO.

http://support.microsoft.com/default...b;en-us;159691

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.
"Bruce Lawrence" <dg*********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have an Access97 database and I would like to link a SQL table to it
without having to create a manual ODBC connection on every system that
needs to use this database.

This is what I have so far to make the ODBC connection:

Dim DBconnect As Object
Dim CnnStr As String

CnnStr = "Provider=sqloledb;" & _
"Data Source=servername;" & _
"Initial Catalog=odbc-connection-name;" & _
"User ID=username;Password=;"

Set DBconnect = CreateObject("ADODB.Connection")
DBconnect.ConnectionString = CnnStr

This code works fine to create the connection. What I need now, is the
code to create the actual link to the "openord" table in the "proddata"
database on that SQL server.

I guess it would be a good idea to check to see if the table exists and
if it does to skip the link connection. Otherwise it may give an error
that the link is already there?

Thanks for any help !
Nov 13 '05 #3
David Lloyd wrote:
Bruce:

The following KB articles shows how to link an external table. The sample
code shows how to check for the existence of an existing table, as well as
link to an external table. This sample code uses DAO as opposed to ADO.

http://support.microsoft.com/default...b;en-us;159691


For checking existance of a local table, this will be quicker:

(slightly modified from production code to eliminate global db variable
to make it work standalone)
Function ExistTableDef(pstrTableName As String) As Boolean
' Return true is a Tabledef exists of that name
Dim tdf As TableDef
Dim db As Database
Set db = CurrentDB()
For Each tdf In db.TableDefs
If tdf.name = pstrTableName Then
ExistTableDef = True
Exit For
End If
Next
Set tdf = Nothing
Exit Function
End Function

It's also better than working off an error, MS' example assumes that
only one error could possibly occur. If another error occurs, it will
assume the table exists.

The reason it's quicker is that it can loop the tabledefs a lot faster
than it can set up an error handler (which does take a lot of time). It
doesn't need an error handler itself as any errors in here should be
trapped by the calling process.
Nov 13 '05 #4
Thank you everyone for posting.

Here is what I have so far. I believe some of this came from Mr.
Steele's code. Can't remember as I've tried a gazzillion things so far
with no luck. This is what I posted at Experts Exchange.

Here is what I have so far but I'm getting errors.
__________________________________________________ __
Function CreateDSNConnection()
On Error GoTo CreateDSNConnection_Err

Dim stConnect As String
stConnect = "Driver={SQL
Server};Server=servername;Database=test;Uid=user;P wd=password;"

DBEngine.RegisterDatabase "proddata", "SQL Server", True, stConnect

CreateDSNConnection = True
Exit Function

CreateDSNConnection_Err:

CreateDSNConnection = False
MsgBox "CreateDSNConnection encountered an unexpected error: " &
Err.Description

End Function
__________________________________________________

That code right there gives me "ODBC Call failed". But I know the
username and password I'm trying works because I can create the DSN
manually. If I can manage to create the DSN, I'll manually link the
table in SQL and use this code in the autoexec or something.
Now, here is the other way I'm trying to do it.
__________________________________
Function linktbl()

Dim DBconnect As Object
Dim CnnStr As String
Dim MyDb As Database
Dim tdf As TableDef

CnnStr = "Provider=sqloledb;" & _
"Data Source=servername;" & _
"Initial Catalog=test;" & _
"User ID=user;password=password;"

Set DBconnect = CreateObject("ADODB.Connection")
DBconnect.ConnectionString = CnnStr

Set MyDb = CurrentDb

Set tdf = MyDb.CreateTableDef("openord", dbAttachSavePWD, "openord",
CnnStr)
MyDb.TableDefs.Append tdf
MyDb.TableDefs.Refresh

End Function
_____________________________________________

This code gets down to the "mydb.tabledefs.append" line and gives me an
error "Couldn't find installable ISAM".

Nov 13 '05 #5
I do beleive I've found the answer...

http://support.microsoft.com/kb/q159691/

Not the most glorified but it works! I'm going to try to find a way to
bypass the need for the table and store the connection info another
way.

Nov 13 '05 #6
I do beleive I've found the answer...

http://support.microsoft.com/kb/q159691/

Not the most glorified but it works! I'm going to try to find a way to
bypass the need for the table and store the connection info another
way.

Nov 13 '05 #7
Ok here is the fix... I'll post this as a seperate solution

__________________________________________________ __
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 "dbname", _
"SQL Server", _
True, _
"Description=Production Data" & _
Chr(13) & "Server=myservername" & _
Chr(13) & "Database=SQLdbName"
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = "openord"
strConn = "ODBC;"
strConn = strConn & "DSN=proddata;"
strConn = strConn & "APP=Microsoft Access;"
strConn = strConn & "DATABASE=test;"
strConn = strConn & "UID=username;"
strConn = strConn & "PWD=password;"
strConn = strConn & "TABLE=openord"
If (DoesTblExist(strTblName) = False) Then
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, ("openord"), _
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
________________________________________________

Nov 13 '05 #8

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

Similar topics

1
by: tabonni | last post by:
Hi All I connected MS Exchange Server to MS Access database using Access link table function. I can open the database table and see the fields and data inside the link table. However, the table...
1
by: Colin Steadman | last post by:
I've written an online survey in ASP using Access as the db. To get the results and some data about the user I've written a simple select query in Access and saved it as 'QRY_GET_RESULTS'. ...
4
by: alkhatib | last post by:
Hi Experts, I'm working on Win2k platform, where a process is wrinting to a log (plain text in tabular form) . My goal is to view this grawing log on access database, I linked a
1
by: dwlawren | last post by:
I wrote a C# asp.net app. that uses MS Access as a backend. I forgot include in my design that the Link tables would break when I deployed the application to the "Live" server. The problem is:...
1
by: swatijogdand | last post by:
How to link table of sql server to access database using ado?
1
benchpolo
by: benchpolo | last post by:
Is there a way that I can incorporate the LINK TABLE MANAGER through VBA code instead of creating a custom menu? Thanks.
0
by: eighthman11 | last post by:
Hello everyone, quick question. I have an Access application (Access 2003) which has link tables to a SQL server. When you do anything on the application that requires accessing data of a link...
1
by: Luting | last post by:
Hi everyone, I am working on a project using Access as the frontend of a Oracle database. I'd like to use the Dual table in Oracle so I made a link table in Access for the Dual. However, it...
3
by: Vee007 | last post by:
Following is my code: Dim objCatalog As ADOX.Catalog Dim objTableLink As ADOX.Table Dim objADOConnection As ADODB.Connection Try objADOConnection = New...
10
by: colintis | last post by:
Hi guys, Is there anyone who had and resolved a problem with linked tables? The database works normally while I do the work with Access 2003, but when my PC had upgrade to use Access 2007, I...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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...
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,...

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.