Set dll references with vba?

Posted on 2000-03-23
Medium Priority
Last Modified: 2010-11-15
Is there a way in VBA to set a reference to an object library in Access97?

I need to set a reference to the Outlook object library without going to each machine and using Tools/References to set it.  I know the name and location of the file I need to set, I just can't find any help in the knowledge base.

Anyone have any ideas?

Question by:tradenut
LVL 10

Accepted Solution

paasky earned 800 total points
ID: 2650927
Hello tradenut,

This is from Access help:

The following example creates a reference to a specified type library:

Function ReferenceFromFile(strFileName As String) As Boolean
      Dim ref As Reference

      On Error GoTo Error_ReferenceFromFile
      Set ref = References.AddFromFile(strFileName)
      ReferenceFromFile = True

      Exit Function

      MsgBox Err & ": " & Err.Description
      ReferenceFromFile = False
      Resume Exit_ReferenceFromFile
End Function

You could call this function by using a procedure such as the following, which creates a reference to the calendar control:

Sub CreateCalendarReference()
      If ReferenceFromFile("C:\Windows\System\Mscal.ocx") = True Then
            MsgBox "Reference set successfully."
            MsgBox "Reference not set successfully."
      End If
End Sub

Hope this helps,

Expert Comment

ID: 2650970
Access 97 exposes the references through the "Application.References" object.  This code will print the name and full path to the file of all references currently set:

dim myref as reference
for each myref in application.references
   debug.print myref.name
   debug.print myref.fullpath

Add Outlook to the references on you machine and this will display the full path.  The Outlook 8.0 references a file called "msoutl8.olb"  Since the paths to it may be different on different machines, you may need to code to search for it.  Once you get the full path, you can add it to the references with:

Application.References.AddFromFile "C:\Program Files\Microsoft Office\Office\msoutl8.olb"

Hope it helps...

Expert Comment

ID: 2650993
Here's a routine I stole from Microsoft for importing calendar items to a table...

Public Sub GetAppointments(strCriteria As String)
   Dim objOutlook As New Outlook.Application
   Dim objNS As NameSpace
   Dim objInboxItems As Items
   Dim Appt As Object
   'Dim Criteria As String
   Dim db As Database
   Dim rst As Recordset
   Set objNS = objOutlook.GetNamespace("MAPI")
   'Get all items in Calendar folder
   Set objInboxItems = objNS.GetDefaultFolder(olFolderCalendar).Items
   'Criteria for Find method
   'Criteria = "[Start]>""2/15/98"" and [Start]< ""3/1/98"""
   'Omit recurring apointments
   strCriteria = strCriteria & " and [IsRecurring]=False"
   'Find first Appointment matching the criteria
   Set Appt = objInboxItems.Find(strCriteria)
   'Loop until no match
   Set db = DBEngine(0)(0)
   Set rst = db.OpenRecordset("Calendar")
   Do While Not (Appt Is Nothing)
      'Print the Appointment Start, End, and Subject
      'Debug.Print Appt.Start; Appt.End; Appt.Subject
      'Write a new Record
      With rst
            !Subject = Appt.Subject
            !StartDate = Format$(Appt.Start, "Short Date")
            !StartTime = Format$(Appt.Start, "Long Time")
            !EndDate = Format$(Appt.End, "Short Date")
            !EndTime = Format$(Appt.End, "Long Time")
            '!AllDayEvent = Appt.AllDayEvent
            '!Reminderonoff = Appt.ReminderSet
            '!BillingInformation = Appt.BillingInformation
            !Description = Appt.Body
      End With
      'Find the next Appointment matching the same criteria used with the Find method
      Set Appt = objInboxItems.FindNext
   Set Appt = Nothing
   Set objInboxItems = Nothing
   Set objNS = Nothing
   Set objOutlook = Nothing
End Sub

I happen call it with a date range like this: Call GetAppointments("[Start]>=""3/16/2000"" and [Start]< ""3/18/2000""")

If it ain't too purdy, it's cuz it's a tool I use for me, myself, and I to transfer Outlook appointments into our billing system.
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.


Expert Comment

ID: 2650998
Oops, guess I *do* still have a "hard" reference to msoutl85.olb, "Micorsoft Outlook 98 Object Model"... thot I didn't :(  But you should still be able to avoid the hard reference!  (ahem.)

Expert Comment

ID: 2651410
I use the following on my machine to find the correct name for the reference I am looking to set

For intI = 1 To References.Count
    Set ref = References(intI)
    MsgBox "Reference is " & ref.Name
Next intI

This will display the name of each reference and give you the correct name to use in the following routine in your production program

' set a reference to the registry manipulation dll if necessary
If Not FindReference("regObj") Then
    If Not ReferenceFromFile(gblstrSysPath & "regObj.dll") Then
    End If
End If

This set the reference to regObj in the Windows System path. You need to use the correct path to the library or dll. most are in the win system path

You need to be aware with this, though, if there are instructions in your startup code that depend on this reference and if the code is compiled when the user starts the app a compile error will occur. the problem is that the reference must be set before the module will compile and the module hass to run to set the reference - Catch 22!


Author Comment

ID: 2652031
Thanks to everyone who commented, I really appreciate the help!

Paasky gets the points as he was the first to get the answer.

And also thanks to Ray for pointing out that the reference needs to be set before the application is run.  That's going to save me some time in the morning!

LVL 10

Expert Comment

ID: 2652099
Glad I could help you tradenut.

Best regards,

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question