473,396 Members | 1,773 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.

Editing saved UID and PWD through relinking ODBC tables

I have a number of databases in MS Access 97. Some tables in these
databases are linked to ODBC tables in Oracle9/10. We would like to
change the user name and password on the Oracle tables so each
database in MS Access linking to the tables in Oracle will need to be
relinked with the new credentials.

We would also like to automate this process of dropping the old links
and making a new one through a module that can be run in every
database. Piecing things together from various places on the internet,
this is what we have so far:

Option Compare Database
Option Explicit

'*********** Code Start ************
Const IntAttachedTableType As Integer = 4
Const ALLFILES = "All Files"

Function Replace(ByVal Valuein As String, ByVal WhatToReplace As _
String, ByVal Replacevalue As String) As String
Dim Temp As String, P As Long
Temp = Valuein
P = InStr(Temp, WhatToReplace)
Do While P 0
Temp = Left(Temp, P - 1) & Replacevalue & _
Mid(Temp, P + Len(WhatToReplace))
P = InStr(P + Len(Replacevalue), Temp, WhatToReplace, 1)
Loop
Replace = Temp
End Function
Function fRefreshLinks() As Boolean

Dim dbs As Database
Dim rst As Recordset, rstTry As Recordset
Dim tdf As TableDef
Dim strOldConnect As String, strNewConnect As String
Dim strFullLocation As String, strDatabase As String, strMsg As String
Dim objTableDef As TableDef
Dim strTableName As String
Dim strSourceTableName As String
Dim strNewConString As String

Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("SELECT MSysObjects.Connect,
MsysObjects.Database, " & _
"MSysObjects.Name from MSysObjects " & _
"WHERE MSysObjects.Type = " & IntAttachedTableType)
If rst.RecordCount <0 Then
rst.MoveFirst
Do
On Error Resume Next
Set rstTry = dbs.OpenRecordset(rst![Name].Value)
If Err = 1 Then
rstTry.Close
Set rstTry = Nothing
Else
On Error GoTo fRefreshLinks_Err
strFullLocation = rst.Name
strDatabase = FileName(strFullLocation)
Set tdf = dbs.TableDefs(rst![Name].Value)
strOldConnect = tdf.Connect
strNewConnect = strOldConnect
'strNewConnect = findConnect(strDatabase, tdf.Name, strOldConnect)
'If strNewConnect = "" Then
'Err.Raise
'Else
strTableName = tdf.Name
strSourceTableName = tdf.SourceTableName
strNewConString = rst("Connect")
strNewConString = Replace(strNewStr, "UID=XXX", "UID=XXX")
strNewConString = Replace(strNewStr, "PWD=XXX", "PWD=XXX")

Set objTableDef = New TableDef
With objTableDef
..Name = strTableName & "Temp"
..SourceTableName = strSourceTableName
..Connect = strNewConString
End With
CurrentDb.TableDefs.Append objTableDef
For Each tdf In dbs.TableDefs
If tdf.Connect = strOldConnect Then
tdf.Connect = strNewConnect
tdf.RefreshLink
End If
Next tdf
dbs.TableDefs.Refresh
'End If
End If
Err = 0
rst.MoveNext
If rst.EOF Then
Exit Do
End If
Loop
End If

fRefreshLinks_End:
Set tdf = Nothing
Set rst = Nothing
Set rstTry = Nothing
fRefreshLinks = True
Exit Function

fRefreshLinks_Err:
fRefreshLinks = False
Select Case Err
Case 3024:

Case Else:
strMsg = "Error Information..." & vbCrLf & vbCrLf
strMsg = strMsg & "Function: fRefreshLinks" & vbCrLf
strMsg = strMsg & "Description: " & Err.Description & vbCrLf
strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf
MsgBox strMsg, vbOKOnly + vbCritical, "Error"
End Select
Exit Function
End Function

This will create a new link with the same name as the old one with
'temp' appended to the end of it. It should be identical in every
other way to the old link except with the updated username and
password in the connection string.

The trouble we are having is that it will not accept the new
connection string. In the line

strNewConString = rst("Connect")

we pull the old connection string from the recordset created from the
MSysObjects table. We could pull the old string from the tabledefs by
saying

strNewConString = tdf.Connect

instead and the code works flawlessly creating the new link, however
tdf.connect does not contain the username and password information
that MSysObjects does.

Is there something we have done wrong, or is there another way to
write the new connection string into the new tdf?

Jul 19 '07 #1
2 4575
Wow!

What a complicated bit of code!

If there is only one database to connect to you might try something
simpler...

Public Function FixODBCConnection()
Dim sConnection1 As String

sConnection1 = ' set full connection string...

Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Set dbs = CurrentDb()
For Each tdf In dbs.TableDefs
If Left(tdf.Connect, 4) = "ODBC" Or Left(tdf.Connect, 3) = "DSN"
Then
If InStr(tdf.Connect, "Datase Name") Then
tdf.Attributes = DAO.dbAttachSavePWD
tdf.Connect = sConnection1
tdf.RefreshLink
End If
End If
Next
End Function

Regards,
Tom Bizannes
Microsoft Access Development
Sydney,Australia

Jul 22 '07 #2
Thanks for your help Tom,
that indeed looks like a much simpler solution.

I have a few questions regarding that bit of code though.

The first being what does DAO.dbAttachSavePWD do exactly?
We're looking to specify a new password and user name, so where here
do we tell it what these things are? Does this open a prompt for new
credentials?

Also, in the line "If InStr(tdf.Connect, "Datase Name") Then " Where
you say "Datase Name", I am accurate in assuming that this is the name
of the ORACLE database that we're trying to link to?

The ultimate goal of this module is for it to be placed in any of our
existing Access databases and simply run where it will copy every link
to our ODBC database only altering the PWD and UID for it, and then
dropping the old link.

Jul 23 '07 #3

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

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
4
by: Frank Bishop | last post by:
I have a MSDE database and I need to delete the contents of the exiting tables and then import new data on a scheduled basis from an ODBC data source (preferable through a system DSN). This was...
3
by: Kyle Arrington | last post by:
I have a network server with MySQL on it and I am trying to make access update the information it has in its linked table on startup or command. I know nearly nothing about macros or modules. I...
11
by: DraguVaso | last post by:
Hi, I want to make a small application in VB.NET that relinks all the query's and tables in an Access database that are linked via ODBC to an SQL Server. It must be able to relink all the tables...
0
by: jrhowcroft | last post by:
I have a number of databases in MS Access 97. Some tables in these databases are linked to ODBC tables in Oracle9/10. We would like to change the user name and password on the Oracle tables so each...
1
by: atlbearcat | last post by:
Hello all I have a database that uses quite a few tables from SQL server. The problem is that when I post a new version of the database, I have to manually relink the SQL tables via the Linked...
10
by: Richard | last post by:
Hi folks, thanks for taking the time to read this (and hopefully point our where I'm going wrong). The scenario: I have a local Access2007 database which links in several read only mySql...
8
by: Neil | last post by:
I just started using Access 2003, and I can't link a SQL Server table. I right-click on the database window; select Link Tables; and select ODBC Databases from the Files of Type dropdown. As soon...
8
MMcCarthy
by: MMcCarthy | last post by:
As a lot of my projects involve using an odbc linked backend data source I have found this piece of code to be very useful. I usually trigger it to run on system startup like in AutoExec macro. It...
81
MMcCarthy
by: MMcCarthy | last post by:
Hi everyone I am using this as a test question to test the new Rate up/down feature for threads and posts. For anyone looking for a legitimate answer to this question please check out this...
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
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.