Change back-end path by code?

B

Brian

Hallo,

Is it possible to set/change the back-end path by code (in stead of using
the linked table manager)?

Regards,

Harmannus
 
B

Brian

Hallo,

Thanx for the reply.

Is there no way to set it yourself? I don't want to bother a user with
searching for a back-end. Maybe a table with all your linked tables with the
path as a field?

I make changes on a diferent computer and have to relink the back-end when
putting it back on the server.


Regards,
Harmannus
 
C

Cheryl Fischer

You could comment out the code provided in the link that asks for responses
from the user and hard code in the path to your tables. Or, prior to
distributing a modified front-end, you could use the code to re-link the
tables yourself and save your users from having to do it.
 
H

Harmannus

Hallo,

Thanx for the suggestion. My VBA knowledge is not that good and i do not
understand everthing that is done in the code. I tried to comment out some
parts but can't get it right.

Could you please be more specific as to wat i must use of the code provided
in the link so that i can hard code the path myself.

Thanx for any help.

Regards,
Harmannus
 
C

Cheryl Fischer

Below are two sections from the function fRefreshLinks() which you can
comment out

' Comment out this section if you do not to give the user a choice of doing
the re-linking:

If MsgBox("Are you sure you want to reconnect all Access tables?", _
vbQuestion + vbYesNo, "Please confirm...") = vbNo Then
Err.Raise cERR_USERCANCEL
End If

Then, comment out the following code, which asks the user to select a path
to the file containing back end data:

strMsg = "Do you wish to specify a different path for the Access Tables?"
If MsgBox(strMsg, vbQuestion + vbYesNo, "Alternate data source...") = vbYes
Then
strNewPath = fGetMDBName("Please select a new datasource")
Else
strNewPath = vbNullString
End If

AND, insert the following code:

strNewPath = "\\MyServer\Databases\MyBE_DB.mdb"


That should be all you need to do.
 
H

Harmannus

Hallo,

Tried your suggestion. But it doesn't work

1. Put the code in a form and made a button with the code call
fRefreshLinks.
2. Commented out the below sections.
3. Commented out the select case errors (becase of error can't link tables)
and put on error resume next

In the status bar it says linking tblmytable... and nothing hapens...

Any suggestions on currecting this?

Regards,

Harmannus
 
C

Cheryl Fischer

I tested the suggestions that I supplied to you and they did work, so I am
at a loss at this moment to suggest a cause.

How about putting a breakpoint in the code and then stepping through it
until you can determine the line on which it fails. Then, post the the
function/sub in which the failure occurs along with the exact error message.
 
H

Harmannus

Hallo,

I keep getting a status messages saying linking table "mytable" and then
nothing happens

Below the VBA code of my LinkTable form.

Regards,
Harmannus

----start of code---

Option Compare Database

Private Sub cmdUpdate_Click()
Call fRefreshLinks
End Sub

'***************** Code Start ***************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Function fRefreshLinks() As Boolean
Dim strMsg As String, collTbls As Collection
Dim i As Integer, strDBPath As String, strTbl As String
Dim dbCurr As Database, dbLink As Database
Dim tdfLocal As TableDef
Dim varRet As Variant
Dim strNewPath As String

Const cERR_USERCANCEL = vbObjectError + 1000
Const cERR_NOREMOTETABLE = vbObjectError + 2000

On Local Error GoTo fRefreshLinks_Err

'If MsgBox("Are you sure you want to reconnect all Access tables?", _
' vbQuestion + vbYesNo, "Please confirm...") = vbNo Then
Err.Raise cERR_USERCANCEL

'First get all linked tables in a collection
Set collTbls = fGetLinkedTables

'now link all of them
Set dbCurr = CurrentDb

'strMsg = "Do you wish to specify a different path for the Access
Tables?"

'If MsgBox(strMsg, vbQuestion + vbYesNo, "Alternate data source...") =
vbYes Then
' strNewPath = fGetMDBName("Please select a new datasource")
'Else
' strNewPath = vbNullString
'End If

strNewPath = "k:\databases\mydatabase_be.mdb"

For i = collTbls.Count To 1 Step -1
strDBPath = fParsePath(collTbls(i))
strTbl = fParseTable(collTbls(i))
varRet = SysCmd(acSysCmdSetStatus, "Now linking '" & strTbl &
"'....")
If Left$(strDBPath, 4) = "ODBC" Then
'ODBC Tables
'ODBC Tables handled separately
' Set tdfLocal = dbCurr.TableDefs(strTbl)
' With tdfLocal
' .Connect = pcCONNECT
' .RefreshLink
' collTbls.Remove (strTbl)
' End With
Else
If strNewPath <> vbNullString Then
'Try this first
strDBPath = strNewPath
Else
If Len(Dir(strDBPath)) = 0 Then
'File Doesn't Exist, call GetOpenFileName
strDBPath = fGetMDBName("'" & strDBPath & "' not
found.")
If strDBPath = vbNullString Then
'user pressed cancel
Err.Raise cERR_USERCANCEL
End If
End If
End If

'backend database exists
'putting it here since we could have
'tables from multiple sources
Set dbLink = DBEngine(0).OpenDatabase(strDBPath)

'check to see if the table is present in dbLink
strTbl = fParseTable(collTbls(i))
If fIsRemoteTable(dbLink, strTbl) Then
'everything's ok, reconnect
Set tdfLocal = dbCurr.TableDefs(strTbl)
With tdfLocal
.Connect = ";Database=" & strDBPath
.RefreshLink
collTbls.Remove (.Name)
End With
Else
Err.Raise cERR_NOREMOTETABLE
End If
End If
Next
fRefreshLinks = True
varRet = SysCmd(acSysCmdClearStatus)
MsgBox "All Access tables were successfully reconnected.", _
vbInformation + vbOKOnly, _
"Success"

fRefreshLinks_End:
Set collTbls = Nothing
Set tdfLocal = Nothing
Set dbLink = Nothing
Set dbCurr = Nothing
Exit Function
fRefreshLinks_Err:
fRefreshLinks = False
On Error Resume Next
'Select Case Err
'Case 3059:
'Case cERR_USERCANCEL:
' MsgBox "No Database was specified, couldn't link tables.", _
' vbCritical + vbOKOnly, _
' "Error in refreshing links."
' Resume fRefreshLinks_End
'Case cERR_NOREMOTETABLE:
' MsgBox "Table '" & strTbl & "' was not found in the database" &
_
' vbCrLf & dbLink.Name & ". Couldn't refresh links", _
' vbCritical + vbOKOnly, _
' "Error in refreshing links."
' Resume fRefreshLinks_End
'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"
' Resume fRefreshLinks_End
'End Select
End Function

Function fIsRemoteTable(dbRemote As Database, strTbl As String) As Boolean
Dim tdf As TableDef
On Error Resume Next
Set tdf = dbRemote.TableDefs(strTbl)
fIsRemoteTable = (Err = 0)
Set tdf = Nothing
End Function

Function fGetMDBName(strIn As String) As String
'Calls GetOpenFileName dialog
Dim strFilter As String

strFilter = ahtAddFilterItem(strFilter, _
"Access Database(*.mdb;*.mda;*.mde;*.mdw) ", _
"*.mdb; *.mda; *.mde; *.mdw")
strFilter = ahtAddFilterItem(strFilter, _
"All Files (*.*)", _
"*.*")

fGetMDBName = ahtCommonFileOpenSave(Filter:=strFilter, _
OpenFile:=True, _
DialogTitle:=strIn, _
Flags:=ahtOFN_HIDEREADONLY)
End Function

Function fGetLinkedTables() As Collection
'Returns all linked tables
Dim collTables As New Collection
Dim tdf As TableDef, db As Database
Set db = CurrentDb
db.TableDefs.Refresh
For Each tdf In db.TableDefs
With tdf
If Len(.Connect) > 0 Then
If Left$(.Connect, 4) = "ODBC" Then
' collTables.Add Item:=.Name & ";" & .Connect, KEY:=.Name
'ODBC Reconnect handled separately
Else
collTables.Add item:=.Name & .Connect, Key:=.Name
End If
End If
End With
Next
Set fGetLinkedTables = collTables
Set collTables = Nothing
Set tdf = Nothing
Set db = Nothing
End Function

Function fParsePath(strIn As String) As String
If Left$(strIn, 4) <> "ODBC" Then
fParsePath = Right(strIn, Len(strIn) _
- (InStr(1, strIn, "DATABASE=") + 8))
Else
fParsePath = strIn
End If
End Function

Function fParseTable(strIn As String) As String
fParseTable = Left$(strIn, InStr(1, strIn, ";") - 1)
End Function

---end of code ----
 
C

Cheryl Fischer

Two suggestions:

1. Remove the comments from the error handler (case statements at bottom of
fRefreshLinks) so that you can see the error message.

2. Put a breakpoint early in the fRefreshLinks code and step through the
code, so that you can see which line is failing
 
H

Harmannus

Hallo,

I see the function breakpoints but do not realy understand the workings.

Removed the error and the only thing i am getting is error=0

Seems unsolvable. Thanx for your efford.

Regards,
Harmannus
 
C

Cheryl Fischer

It might be worth your time to step back and use the table re-linking code
"as is" without modifications - as a test. If necessary re-copy it from
the link provided. Delete all of the code you downloaded and modified
previously and copy in the code from the Access Web.

Run the table re-linking code "as is" including all of the prompts that you
eventually want to remove. Does the code run without error or hang-up?
And, are the tables linked correctly? If not, then the issue may not be
with any modifications to the code. If the code does, in fact, run
correctly, then make *only* the following modification to fRefreshLinks:

Replace this:

strMsg = "Do you wish to specify a different path for the Access Tables?"
If MsgBox(strMsg, vbQuestion + vbYesNo, "Alternate data source...") = vbYes
Then
strNewPath = fGetMDBName("Please select a new datasource")
Else
strNewPath = vbNullString
End If

With this:
strNewPath = "k:\databases\mydatabase_be.mdb"

Save and compile the module, then run again.
 
H

Harmannus

Hallo

Done! In the orginal state the code runs ok! So no error in the linked
tables.

On changing the below path and compiling it says. sub or function net
defined. ahtAddFilterItem is highlighted.

Could your clearify what this means?

Regards,

Harmannus
 
H

Harmannus

Now i get a error 3044. I have to be connected to the server...

I am not able to connect to the server. The idea is to do this upfront and
not bothering the user with it.


Regards,
Harmannus
 
C

Cheryl Fischer

If you will return to the link for this code, (
http://www.mvps.org/access/tables/tbl0009.htm ) the last paragraph in the
opening comments says:

"If the database specified for the linked table is not present, the code
brings up the "GetOpenFileName" dialog so that the user can select an
alternative source. Note: You must include that code in your application for
this example to work."

The link for the GetOpenFileName dialog is:

http://www.mvps.org/access/api/api0001.htm

and you should have this code in your "arsenal" as it eliminates the need to
use the CommonDialog OCX control to allow users to select files. You may
not need it now, but you will at some time in the future.
 
C

Cheryl Fischer

Your options in this case are ..

1. Test the code on your stand-alone computer, hard-coding in the name of a
database that you *can* find; i.e., one that is located on your computer's
hard drive. Then, if the test works, change the code so that the routine
will look for the database on the "k:" drive and deliver to your users.

2. Test the code on a computer that is connected to the network.
 
H

Harmannus

Hallo,

I *simply* want to be able to hard code a *server* path to a back-end
database that i do not have access to. For develop reasons i use a local
back-end to make changes in the front-end and sent it back. For this purpose
i want to reset the path in the front-end to its orginal state. The code
checks for a valid connection as far as i do understand the code. This check
should not be made. It should simply update the linked tables to the
original path...

Maybe this cannot be done....

Thanx for all your afford.

Regards,
Harmannus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top