Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 196751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:47:39+00:00 2026-05-11T16:47:39+00:00

I want to write the following function which should be used in an Excel

  • 0

I want to write the following function which should be used in an Excel worksheet:

=GetRecField("Foo Record Key", "FooField1")

…which will connect internally through ODBC to an SQL database, execute there an

SELECT FooField1 FROM MyTable WHERE KEY_FIELD='Foo Record Key';

and will return the resulting value as the result of the function GetRecField. The above SQL is granted to return only one record (IOW KEY_FIELD has an unique constraint).

Of course, the above function can be called multiple times in a worksheet so, please try to avoid a blind QueryTables.Add

TIA.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:47:39+00:00Added an answer on May 11, 2026 at 4:47 pm

    You can write a custom function to do that

    1. Open the VBA editor (ALT-F11)
    2. Open Tools -> References, and make sure the “Microsoft ActiveX Data Objects 2.8 Library” and “Microsoft ActiveX Data Objects Recordset 2.8 Library” are selected
    3. Right click VBAProject, and choose Insert -> Module
    4. Open the module. Now you can create a custom function, like:
        Public Function GetItem(field As String, id As Integer) As String
            Set oConnection = New ADODB.Connection
            Dim oRecordset As ADOR.Recordset
            oConnection.Open "provider=sqloledb;data source=yourserver;" & _
                "Trusted_Connection=yes;initial catalog=yourdatabase;"
            Set oRecordset = oConnection.Execute( & _
                "select " & field & " from table where id = " & id)
            If oRecordset.EOF Then
                GetItem = "n/a"
            Else
                GetItem = oRecordset(field)
            End If
        End Function
    
    1. You can now call the function from a cell:

      =GetItem(“fieldname”;2)

    The module is required because non-module functions can’t be called from inside the spreadhseet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 99k
  • Answers 99k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at Visual Studio Gallery, it's the place… May 11, 2026 at 7:41 pm
  • Editorial Team
    Editorial Team added an answer In general, referencing an external path in done through a… May 11, 2026 at 7:41 pm
  • Editorial Team
    Editorial Team added an answer A better way is to use the Stopwatch class: using… May 11, 2026 at 7:41 pm

Related Questions

I'm currently refactoring some code on a project that is wrapping up, and I
This is a detail question for C#. Suppose I've got a class with an
In a constructor in Java, if you want to call another constructor (or a
In my database I have tables that define types for example Table: Publication Types
I am developing an application for pocket PC which should run in landscape mode.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.