Need help creating User-Defined Functions in Excel to query from a database

Brandont05

New Member
Joined
May 27, 2016
Messages
6
I work in an HR department that does not have any user defined functions(custom functions).

For example: I would like to be able to use an Employee# to pull their First Name from our database.
Example Function: =GetFirstName(Employee#)

I need help writing the VBA coding necessary to facilitate this function.

Function Name: GetFirstName
Database Name: c11e
Data Table: EMPLOYEE
Data Field: FIRST_NAME

I greatly appreciate any help.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
What kind of database is it?
SQL Server, Oracle, Access, etc.

That information will determine what connection string will need to be created.
Also, it will be important to know the name of the Employee ID field since we are going to be looking up on that field (Employee_ID, Emp_ID, etc)
 
Last edited:
Upvote 0
BiocideJ - Thank you for responding. I'm pretty sure it is an SQL Server. Opps I forgot to include that, the employee ID field is just EMPLOYEE.
 
Upvote 0
Not typically a good idea, but unfortunately, all too common. Not to fear, just give me some time to work something up. I don't have SQL Server so I won't be able to test my solution.
 
Upvote 0
OK. This is about as close as I can get you for now.

Rich (BB code):
Public Function GetFirstName(empID As Long) As Variant
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
 
    ' Create the connection string.
    sConnString = "Provider=SQLOLEDB;Data Source=YOUR_SERVER_NAME_HERE;" & _
                  "Initial Catalog=c11e;" & _
                  "Integrated Security=SSPI;"
    
    ' Create the Connection and Recordset objects.
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    ' Open the connection and execute.
    conn.Open sConnString
    Set rs = conn.Execute("SELECT FIRST_NAME FROM EMPLOYEE WHERE EMPLOYEE = " & empID & ";")
    
    ' Check we have data.
    If Not rs.EOF Then
        ' Transfer result.
        GetFirstName = rs!FIRST_NAME
        ' Close the recordset

    Else
        'Not found; return #N/A! error
        GetFirstName = CVErr(xlErrNA)
    End If
    rs.Close
    ' Clean up
    If CBool(conn.State And adStateOpen) Then conn.Close
    Set conn = Nothing
    Set rs = Nothing
    
End Function

Credit for a majority of this code goes to Sektor from this post
http://www.mrexcel.com/forum/excel-...al-basic-applications-connect-sql-server.html

NOTE: You will still need to determine the server address of your database and insert that in the portion of my code that is highlighted red. Any issues, reply back to this thread.
 
Last edited:
Upvote 0
Awesome! I'm not entirely sure how to find the server name. Do you know a quick way?

Also, I'm getting "Compile error: User-defined type not defined". It is highlighting the first line below.

Public Function GetFirstName(empID As Long) As Variant
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
 
Upvote 0
I don't have SQL Server so I'm not sure I can help you there. I suspect you will need to open the database and look at the settings.

As far as the compile error, I forgot to mention that you need to add a reference for the ADODB definitions to work

Tools -> References -> Microsoft ActiveX Data Objects X.X Library
Where X.X is the highest number listed
 
Upvote 0
I'm sorry, but I think I'm actually on an oracle server. I really thought it was an SQL sever. The reason I say this is that when I log in with my username/password it says Microsoft ODBC for Oracle Connect.



 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top