UNC Links (1 Viewer)

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
I have password protected the back end of my split database. Works well when I re linked the front end. However, I am unable to change the links UNC using code. Do I have to live with hard directory refs or is there a tweak I need to make to the UNC code I am using. Curently when I use the function, I get the message that it has been done but it does not happen.

Option Explicit

Declare Function apiWNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" _
(ByVal strLocalName As String, ByVal strRemoteName As String, _
ByRef rlngRemoteNameLen As Long) As Long

Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
intPos = InStr(strItem, vbNullChar)
If intPos > 0 Then
TrimNull = left(strItem, intPos - 1)
Else
TrimNull = strItem
End If
End Function

Public Function GetUNC(ByVal strPath As String) As String
'Note, this function will only return the UNC for network drives.
'Non-net drives and errors get the original value returned to them
On Error GoTo Err_GetUNC
Const lngcBuffer As Long = 257
Dim strUNCPath As String
Dim strDrive As String
If left(strPath, 2) Like "[a-z, A-Z]:" Then
strDrive = left(strPath, 2)
strUNCPath = strUNCPath & Space(lngcBuffer)
'The function will automatically fill the strUNCPath unless there
'is an error (return<>0), fill strPath if error
If apiWNetGetConnection(strDrive, strUNCPath, lngcBuffer) = 0 Then
strUNCPath = TrimNull(strUNCPath) & Mid(strPath, 3)
Else
strUNCPath = strPath
End If
End If

If Len(Trim(strUNCPath)) = 0 Then strUNCPath = strPath
GetUNC = strUNCPath
Exit_GetUNC:
Exit Function
Err_GetUNC:
MsgBox "Error " & Err.Number & " " & Err.Description
Resume Exit_GetUNC
End Function


Function SetUNCLink() As Boolean
'Date: Wednesday, 20 August 2003 2:12:04 PM
'Author: Stephen Cooper
'Email: stephen.cooper@xxx.com.au
'Ph: 8963
'In parameters
'Output
'Description: Set the linked tables to UNC paths
'Calls:
'Notes: Generally called in the startup forms on open
'Example: Form_OnOpen SetUNCLink
'Modified: 12 Sep 2007. Had it so that it would look at ini file for backend path

On Error GoTo HandleError:

SetUNCLink = True

Dim strErrorMsg As String
Dim varReturn As Variant
Dim strMessage As String
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strTableName As String
Dim strBEDataBase As String

Set db = CurrentDb

strBEDataBase = "K:\Zircon\Data\Stores_be.mdb"

For Each tdf In db.TableDefs
'Check it is a linked table
With tdf
'Dont bother relinking to temp tables
If InStr(1, .Connect, "TempWorkTables") = 0 Then
If Len(.Connect) > 0 Then
'Only reconnect Access tables, not ODBC
If left(.Connect, 10) = ";DATABASE=" Then
If left(.Connect, 1) <> "~" Then
strTableName = .Name
strMessage = "Relinking " & .Name
'SplashLink , strMessage, strMessage
strErrorMsg = "Connecting to - " & .Connect
.Connect = ";DATABASE=" & GetUNC(strBEDataBase)
.RefreshLink
End If 'Left(.Connect, 1) <> "~"
End If 'Left(.Connect, 10) = ";DATABASE="
End If 'Len(.Connect) > 0
End If 'InStr(1, .Connect, "TempWorkTables") = 0
End With 'tdf
Next tdf

db.TableDefs.Refresh

MsgBox ("All done")


ExitHere:
On Error Resume Next
'Close all recordsets etc here
varReturn = SysCmd(acSysCmdClearStatus)
'SplashLink , , , True
Exit Function

HandleError:
Select Case Err.Number
Case 3042
'Out of MSDOs handles - generally means they dont have access to a drive
MsgBox "Please ensure you have access to all the required drive mappings. Please contact support if you are unsure " & vbCrLf & "Error msg = " & strErrorMsg, vbCritical, "Mapping error"
'still log it, as the user will likely lose the error
'LogError "SetUNCLink|" & CurrentProject.Name & "|" & strErrorMsg & "|" & err.Number & " - " & err.Description
Resume Next
Case 3011
'Cant find table - give message and move on. Message will mean that should eventually get deleted
MsgBox "Table " & strTableName & " could not be found. Please check with master copy if table should be deleted", vbCritical + vbOKOnly, "Missing table"
Resume Next
Case Else
'LogError "SetUNCLink|" & CurrentProject.Name & "|" & strErrorMsg & "|" & err.Number & " - " & err.Description
MsgBox strErrorMsg & " " & Err.Number & " " & Err.Description, vbInformation, "Error"
SetUNCLink = False
Resume ExitHere
End Select

End Function
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:06
Joined
Feb 19, 2013
Messages
16,610
struggling to read your code without indentation - suggest you edit your post and use the code tags (# button in advanced editor) to preserve formatting
 

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
Code:
Option Compare Database
Option Explicit

Declare Function apiWNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" _
  (ByVal strLocalName As String, ByVal strRemoteName As String, _
   ByRef rlngRemoteNameLen As Long) As Long

Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
    intPos = InStr(strItem, vbNullChar)
    If intPos > 0 Then
        TrimNull = Left(strItem, intPos - 1)
    Else
        TrimNull = strItem
    End If
End Function

Public Function GetUNC(ByVal strPath As String) As String
'Note, this function will only return the UNC for network drives.
'Non-net drives and errors get the original value returned to them
On Error GoTo Err_GetUNC
    Const lngcBuffer As Long = 257
    Dim strUNCPath As String
    Dim strDrive As String
    If Left(strPath, 2) Like "[a-z, A-Z]:" Then
        strDrive = Left(strPath, 2)
        strUNCPath = strUNCPath & Space(lngcBuffer)
        'The function will automatically fill the strUNCPath unless there
        'is an error (return<>0), fill strPath if error
        If apiWNetGetConnection(strDrive, strUNCPath, lngcBuffer) = 0 Then
            strUNCPath = TrimNull(strUNCPath) & Mid(strPath, 3)
        Else
            strUNCPath = strPath
        End If
    End If
    
        If Len(Trim(strUNCPath)) = 0 Then strUNCPath = strPath
    GetUNC = strUNCPath
Exit_GetUNC:
    Exit Function
Err_GetUNC:
    MsgBox "Error " & err.Number & " " & err.Description
    Resume Exit_GetUNC
End Function


Function SetUNCLink() As Boolean
'Date:          Wednesday, 20 August 2003 2:12:04 PM
'Author:        Stephen Cooper
'Email:         stephen.cooper@xxx.com.au
'Ph:            8963
'In parameters
'Output
'Description:   Set the linked tables to UNC paths
'Calls:
'Notes:         Generally called in the startup forms on open
'Example:       Form_OnOpen SetUNCLink
'Modified:      12 Sep 2007. Had it so that it would look at ini file for backend path

On Error GoTo HandleError:

SetUNCLink = True

Dim strErrorMsg As String
Dim varReturn As Variant
Dim strMessage As String
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strTableName As String
Dim strBEDataBase As String

Set db = CurrentDb

strBEDataBase = "K:\Zircon\Data\Zircon_be.mdb"

For Each tdf In db.TableDefs
    'Check it is a linked table
    With tdf
        'Dont bother relinking to temp tables
        If InStr(1, .Connect, "TempWorkTables") = 0 Then
            If Len(.Connect) > 0 Then
                'Only reconnect Access tables, not ODBC
                If Left(.Connect, 10) = ";DATABASE=" Then
                    If Left(.Connect, 1) <> "~" Then
                        strTableName = .Name
                        strMessage = "Relinking " & .Name
                        'SplashLink , strMessage, strMessage
                        strErrorMsg = "Connecting to - " & .Connect
                        .Connect = ";DATABASE=" & GetUNC(strBEDataBase)
                        .RefreshLink
                    End If 'Left(.Connect, 1) <> "~"
                End If 'Left(.Connect, 10) = ";DATABASE="
            End If 'Len(.Connect) > 0
        End If 'InStr(1, .Connect, "TempWorkTables") = 0
    End With 'tdf
Next tdf

db.TableDefs.Refresh

MsgBox ("All done")


ExitHere:
    On Error Resume Next
    'Close all recordsets etc here
    varReturn = SysCmd(acSysCmdClearStatus)
    'SplashLink , , , True
    Exit Function

HandleError:
    Select Case err.Number
        Case 3042
            'Out of MSDOs handles - generally means they dont have access to a drive
            MsgBox "Please ensure you have access to all the required drive mappings. Please contact support if you are unsure " & vbCrLf & "Error msg = " & strErrorMsg, vbCritical, "Mapping error"
            'still log it, as the user will likely lose the error
            'LogError "SetUNCLink|" & CurrentProject.Name & "|" & strErrorMsg & "|" & err.Number & " - " & err.Description
            Resume Next
        Case 3011
            'Cant find table - give message and move on. Message will mean that should eventually get deleted
            MsgBox "Table " & strTableName & " could not be found. Please check with master copy if table should be deleted", vbCritical + vbOKOnly, "Missing table"
            Resume Next
        Case Else
            'LogError "SetUNCLink|" & CurrentProject.Name & "|" & strErrorMsg & "|" & err.Number & " - " & err.Description
            MsgBox strErrorMsg & " " & err.Number & " " & err.Description, vbInformation, "Error"
            SetUNCLink = False
            Resume ExitHere
    End Select

End Function
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:06
Joined
Feb 19, 2013
Messages
16,610
looks to me like when you get the unc you are dropping the password

also, I know if you have linked to a non password protected be if you then password protect the be, you have to delete the original linked table and create a new link - modifying the tabledef doesn't do it. This may apply here as well, but I'm not in a position to test it
 

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
Thanks I have re linked after putting a password. Could you help me amend the code to supply the password as part of the UNC linking?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:06
Joined
Feb 19, 2013
Messages
16,610
sorry no - it's 2 in the morning here and I'm hitting the sack:D and I have a full day tomorrow (today) so won't be online much.

Suggest you extract the connection string from the tabledef before updating it to find the password - take a look in the msysobjects table - you'll see how it is constructed there
 

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
Hi CJ

Would you be kind enough to help with some tweaking of the code?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:06
Joined
Feb 19, 2013
Messages
16,610
perhaps something like

.Connect = replace(.Connect,strBEDataBase,GetUNC(strBEDataBase))
 

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
Thanks. I tried but there is no prompt to provide password. Pretty much the same position. Unlike waiting and fixing links, I am getting 'True' in the immediate window.
 

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
Sorry for not being clear. After making necessary adjustment as you suggested, the code still runs but the links have not changed to UNC links and there was no prompt for a password if you were expecting one. For some reason when I run the code now in the immediate window, I get ;True' as the result of the function.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:06
Joined
Feb 19, 2013
Messages
16,610
the adjustment was to your calling code - replacing the line in red.

Code:
 If Left(.Connect, 10) = ";DATABASE=" Then
                    If Left(.Connect, 1) <> "~" Then
                        strTableName = .Name
                        strMessage = "Relinking " & .Name
                        'SplashLink , strMessage, strMessage
                        strErrorMsg = "Connecting to - " & .Connect
                        [COLOR=red].Connect = ";DATABASE=" & GetUNC(strBEDataBase)
[/COLOR]                        .RefreshLink
                    End If 'Left(.Connect, 1) <> "~"
                End If 'Left(.Connect, 10) = ";DATABASE="

I have not looked at the GetUNC function since the issue was about losing the password, So I still have no idea what you mean by

For some reason when I run the code now in the immediate window, I get ;True' as the result of the function
.
 

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
Thanks once again. I added a password to the back end of the DB. After that I re linked the front end by supplying the password. The only issue I had was that the code I always used to use to convert links to UNC format was not working after password was introduced in the back end. Even after running the code, my tables were showing drive letters to the linked back end.

I therefore sought your advice on amending the UNC code so that UNC links can be created with a password protected back end database.

You have kindly given me a line of code that I did replace in my own code. But even after that I continue to have the same result, that is, UNC links are not created. Normally if I run the code through the immediate window, it runs and updates links and I get the message 'all done', But now I get 'True' in the immediate window when I run the code by writing.

? SetUNCLinks()

I hope this recap assists you to understand the issue.
 

khwaja

Registered User.
Local time
Tomorrow, 01:06
Joined
Jun 13, 2003
Messages
254
Thanks a lot. Mine is a MS Access backend. If you could kindly change the code to suit that.
 

vbaInet

AWF VIP
Local time
Today, 16:06
Joined
Jan 22, 2010
Messages
26,374
There are comments inline. Copy the relevant parts from there and add it to yours. Then ask specific questions.
 

Users who are viewing this thread

Top Bottom