[AccessD] Test for a Particular Reference in Code.

Mitsules, Mark S. (Newport News) Mark.Mitsules at ngc.com
Wed Aug 4 13:00:44 CDT 2004


More from my Gustav archives:))) ...


*****************
Hi Mark

> Does anyone know of a way to set references through code, rather than
> manually checking the references on each PC?  I have searched the KB, but
> cannot find a thing.

Do you wish to add or to check references?

This summer Paul Hobbs asked something similarly (we never got any
feedback).

Maybe you can get some inspiration from this which checks one specific
reference; if you
experience that it's the same reference that seems to be dropped from your
app, you can check for
the GUID of this instead for the GUID of Word:

Public Sub CheckWordRef()

  Dim refA      As Reference
  Dim refW      As Reference
  Dim strGuid   As String
  Dim lngMajor  As Long
  Dim lngMinor  As Long

  strGuid = "{00020905-0000-0000-C000-000000000046}"
  lngMajor = 8
  lngMinor = 1

  ' Look for the reference to Word in the database.
  For Each refA In Application.References
    If refA.Name = "Word" Then
      Set refW = refA
      Exit For
    End If
  Next

  If Not refW Is Nothing Then
    ' Remove the Reference.
    References.Remove refW
  End If
  On Error Resume Next
  ' Try adding Word 2000 library.
  References.AddFromGuid strGuid, lngMajor, lngMinor
  ' Try adding Word 97 library.
  ' This will fail if Word 2000 library was added successfully.
  lngMinor = lngMinor - 1
  References.AddFromGuid strGuid, lngMajor, lngMinor

  Set refA = Nothing
  Set refW = Nothing

End Sub


Alternatively, you may verify the references:

Public Function VerifyReferences(ByVal booErrorDisplay As Boolean) As
Boolean

' Verify Access' external references and re-establish these if possible.
' Uses function IsBroken97().
'
' 2001-07-29. Cactus Data ApS, CPH.

  Dim refA                    As Reference
  Dim refX                    As Reference
  Dim strRefFullPath          As String
  Dim booNotBuiltInRefExists  As Boolean
  Dim booIsBroken             As Boolean
  Dim booRefIsMissing         As Boolean
  Dim strMsgTitle             As String
  Dim strMsgPrompt            As String
  Dim strMsgHeader            As String
  Dim strMsgFooter            As String
  Dim lngMsgStyle             As Long

  ' No special error handling.
  On Error Resume Next

  ' User oriented error message.
  strMsgTitle = "Missing support file"
  strMsgHeader = "One or more supporting files are missing:" & vbCrLf
  strMsgFooter = vbCrLf & vbCrLf & "Report this to IT support." & vbCrLf
  strMsgFooter = strMsgFooter & "Program execution cannot continue."
  lngMsgStyle = vbCritical + vbOKOnly

  ' Look for the first reference in the database other than
  ' the built in "Access" and "Visual Basic for Applications".
  For Each refA In Application.References
    If refA.BuiltIn = False Then
      ' At least one not built in reference is in use.
      booNotBuiltInRefExists = True
      ' Check if the reference is not broken.
      If IsBroken97(refA) = False Then
        ' The first not missing not built in reference is found.
        Set refX = refA
        Exit For
      End If
    End If
  Next

  If booNotBuiltInRefExists = False Then
    ' Only built in references are in use.
    ' Nothing more to do.
  Else
    If refX Is Nothing Then
      ' All not built in references are missing.
      ' Don't remove missing references as there is no way to
      ' re-establish a reference if its identity is lost.
    Else
      ' Remove this not built in reference and add it back to
      ' force Access to revalidate all references.
      ' This may or may not rebuild links to missing references.
      With References
        strRefFullPath = refX.FullPath
        .Remove refX
        .AddFromFile strRefFullPath
      End With
      Set refX = Nothing
    End If
    ' Check references if any should be missing.
    ' If so, no attempt to read a reference is done as it most likely
    ' either is not installed or has been moved to an unknown directory.
    For Each refA In References
      booIsBroken = IsBroken97(refA)
      If booIsBroken = True Then
        ' Build list of missing files.
        strMsgPrompt = strMsgPrompt & vbCrLf & refA.FullPath
      End If
      booRefIsMissing = booRefIsMissing Or booIsBroken
    Next

    ' If any reference is broken, display error message if requested.
    If booRefIsMissing = True And booErrorDisplay = True Then
      strMsgPrompt = strMsgHeader & strMsgPrompt & strMsgFooter
      DoCmd.Beep
      MsgBox strMsgPrompt, lngMsgStyle, strMsgTitle
    End If
  End If

  Set refA = Nothing

  VerifyReferences = Not booRefIsMissing

End Function


Supporting function:

Public Function IsBroken97(ByVal ref As Reference) As Boolean

' Alternative method to check if a reference is broken
' as the IsBroken property cannot be used in Access97.
'
' 2000-03-19. Gustav Brock. Cactus Data ApS.

' Refer to this article at Microsoft Technet:
'
' Article ID: Q186720
'
' The information in this article applies to:
' Microsoft Access 97
'
' SYMPTOMS
' In Microsoft Access, IsBroken is a property of the References collection.
' The Microsoft Access Help topic on the Isbroken property states the
following:
'
' The IsBroken property returns a Boolean value indicating whether a
' Reference object points to a valid reference in the Windows Registry.
'
' Although this statement is correct, to receive this Boolean value
' you must trap for errors that are generated by the broken reference.
' Also, the IsBroken property becomes True only when the file being
referenced
' is deleted and the Microsoft Windows Recycle Bin is emptied.
' This article details the steps necessary to receive the Boolean value.

  Dim booRefOK As Boolean
  On Error GoTo Err_IsBroken97

  If Dir(ref.FullPath) <> vbNullString Then
    booRefOK = Not ref.IsBroken
  End If

Exit_IsBroken97:
  IsBroken97 = Not booRefOK
  Exit Function

Err_IsBroken97:
  ' Ignore non existing servers, drives, and paths.
  Resume Exit_IsBroken97

End Function


Please note that I haven't used these for A2000/2002 - these might behave
differently than A97.
Also, running any of these functions will leave your app not compiled; this
may not cause trouble
but you should pay attention.

/gustav
*****************

-----Original Message-----
From: Greg Smith [mailto:GregSmith at starband.net] 
Sent: Wednesday, August 04, 2004 1:39 PM
To: accessd at databaseadvisors.com
Subject: [AccessD] Test for a Particular Reference in Code.


Hi everybody!

I'm trying to distribute an app that I've modified and I'm running into
references issues at the client's site.  It's in Access97 (but not for
much longer).  I'm having to reference Microsoft Office 8.0 Object Library
(mso97.dll) and I have it update the reference through the code.  However,
if it already is referenced, then I get the error that you can't have a
reference with the same name, etc.

What I need to do is test to see if this reference already exists.  If it
does, then don't add it.  But I can't figure out how to test for it
specifically.  Is there a way to test for a particular reference (in this
case, the one for the MSOffice 8.0 Obj lib?

TIA!

Greg Smith
gregsmith at starband.net


-- 
_______________________________________________
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



More information about the AccessD mailing list