Open Outlook Contact from Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dsajones
    New Member
    • Nov 2012
    • 2

    Open Outlook Contact from Access

    I've managed to find code on the web that allows me to link to a contacts database in an Outlook public folder and I've managed to open a contact using the following line:

    Code:
    Set olContact = MyContacts.Items.Find("[FirstName] = ""Darren""")
    olContact is defined as an Outlook.Contact item
    MyContacts contains the name of the folder that these contacts are stored in.
    After setting olContact I can successfully display the contact in the Outlook contact form using this:
    olContact.Displ ay

    All good so far but obviously I now want to replace the literal string "Darren" with a variable populated from my calling form. I've defined a variable called frmFirstName but I cannot get any variation of the following line to work. I've tried every comnbination of quote marks, square brackets and normal brackets but nothing works. Anyone got any ideas?

    Code:
    Set olContact = MyContacts.Items.Find("[FirstName] = ""[frmFirstName]""")
    Cheers
    David
    Last edited by zmbd; Nov 19 '12, 04:30 PM. Reason: [Z{please format code using the <CODE/> button in the toolbar}]
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Try:
    Code:
    Set olContact = MyContacts.Items.Find("[FirstName] = '" & Me![frmFirstName] & "'")

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      David, ADezii's code makes sense if [frmFirstName] is a control on the local form. If, however, that is the name of your form, and it's consistent with a form name but not with a control name, then you need to replace that with :
      Code:
      Set olContact = MyContacts.Items.Find("[FirstName] = '" & Me.[ControlName] & "'")
      These responses assume the code is being run from the associated module of the form that this control is on.

      Comment

      • dsajones
        New Member
        • Nov 2012
        • 2

        #4
        Thanks guys. ADezii's code is working fine as frmFirstName is a control on the form. It's the specific syntax that always catches me out i.e. when to use single versus double quotes, use of ! versus a . as in the two examples you've shown.

        Anyway, it's working a treat and I'm grateful to both of you for taking the time.

        Cheers
        David

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Hi David.

          If you use the search terms :
          Code:
          site:Bytes.com dot bang
          You will see this topic has been covered pretty fully on here already. However, for controls on a form they could be said to be interchangeable . In fact, for such controls neither is generally required as long as the names are unique on the form. As the controls are property objects for the particular form itself, I prefer to recognise them as such and use the dot (.), but there is nothing wrong with using the bang (!) instead in most cases. There are sometimes complications with field references if you've used the default values prompted by Access, but I see you've used a prefix in your name.

          Bringing us neatly onto the prefix. As standard, the prefix "frm" implies the name is referring to a form. Nothing will break if you do that, but such naming will certainly tend to cause confusion and make maintenance more complicated for you and others. A point worth considering I suggest.

          For the other matter see Quotes (') and Double-Quotes (") - Where and When to use them.
          Last edited by NeoPa; Nov 20 '12, 04:05 PM.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            I didn't find it earlier, but I managed to stumble acoss it later and have saved the reference in my Bytes db for later needs. Referencing Controls on a Form discusses the details of this as clearly as any I've found.

            Comment

            Working...