MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 15th, 2002, 01:14 PM   #1
homebuilder
New Member
 
Join Date: May 2002
Location: Kansas City => Grand Rapids, MI
Posts: 5
Default

Two part question:
1) is it possible to use sql to search a database in a similar manner to the VLOOKUP command (i enter data into a cell and it spits out specific data into another cell)?
2) how?
homebuilder is offline   Reply With Quote
Old May 15th, 2002, 01:23 PM   #2
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Hello,

Here's a function I posted a little while ago which carries out a similar function to VLOOKUP. Before using it you'll need to go into Tools, References and choose Microsoft ActiveX Data Objects 2.n Library (where n will depend on the version of ADO installed on your machine).

The function takes 4 arguments:-

TableName - name of the table in your Access DB.
LookupFieldName - the name of the Field whose values you want to search.
LookupValue - the value you want to search for in LookupFieldName
ReturnField - the name of the field whose value you want to return.

E.g. if you have a table called tblAccountMapping (which lists account numbers, account names, etc) which contains various fields including AccountNumber and AccountDescription. You want to return the account description of the account number in cell A1.

=DBVLookUp("tblAccountMapping","AccountNumber",A1,"AccountDescription")

Here's the code:-

Code:
Dim adoCN As ADODB.Connection
Dim strSQL As String
    
Public Function DBVLookUp(TableName As String, LookUpFieldName As String, LookupValue As String, ReturnField As String) As Variant
Dim adoRS As ADODB.Recordset
If adoCN Is Nothing Then SetUpConnection

Set adoRS = New ADODB.Recordset
strSQL = "SELECT " & LookUpFieldName & ", " & ReturnField & " FROM " & TableName & " WHERE " & LookUpFieldName & _
         "='" & LookupValue & "';"    ' If lookup value is a number then remove the two '
adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
DBVLookUp = adoRS.Fields(ReturnField).Value
adoRS.Close
End Function

Sub SetUpConnection()
On Error GoTo ErrHandler

Set adoCN = New Connection

adoCN.Provider = "Microsoft.Jet.OLEDB.4.0"  'Change to 3.51 for Access 97
adoCN.ConnectionString = "K:financemanagement reportingfinance reporting 2002.mdb" 'Change to your DB path
adoCN.Open
Exit Sub

ErrHandler:
MsgBox Err.Description, vbExclamation, "An error occurred"

End Sub
HTH,
Dan

dk is offline   Reply With Quote
Old May 15th, 2002, 01:39 PM   #3
homebuilder
New Member
 
Join Date: May 2002
Location: Kansas City => Grand Rapids, MI
Posts: 5
Default

That looks great except that I'm not looking in Access, it's an external database. And I don't have the references sub-menu in tools.
Also (I'm very new to VBA) where does the code get entered and how is it then linked to a specific cell?
homebuilder is offline   Reply With Quote
Old May 15th, 2002, 01:51 PM   #4
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Hi,

Sorry, I don't know why I assumed it was an Access database. First of all, the code goes in a standard module :-

Open the VB Editor (Alt+F11)
Click Insert, Module
Paste the code into the module

The function can then be used just like any other function such as SUM.

Now, onto the second problem. What sort of database are you querying? The ActiveX Data Objects (which is what this code uses) was designed to allow a connection to all sorts of databases and still use the same sort of code. These are the lines which you'll need to change:-

adoCN.Provider = "Microsoft.Jet.OLEDB.4.0" 'Change to 3.51 for Access 97
adoCN.ConnectionString = "K:\finance\management reporting\finance reporting 2002.mdb" 'Change to your DB path

Provider describes the type of database you want to connect to.

ConnectionString is the path to the database (either on a server or on a normal network folder).

If you let me know the exact type of database I may be able to help.

Regards,
Dan
dk is offline   Reply With Quote
Old May 15th, 2002, 02:19 PM   #5
jkpd2000
Board Regular
 
Join Date: May 2002
Posts: 91
Default

Hi DK,

I am also facing problem to connect to oracle. could you help.



[ This Message was edited by: jkpd2000 on 2002-05-15 13:19 ]
jkpd2000 is offline   Reply With Quote
Old May 15th, 2002, 02:22 PM   #6
sen_edp
Board Regular
 
sen_edp's Avatar
 
Join Date: Mar 2002
Location: Hellas
Posts: 553
Default

Hi Jkpd

How are you trying to connect to oracle?
through odbc or ado?
__________________
Best Regards
Andreas
sen_edp is offline   Reply With Quote
Old May 15th, 2002, 02:25 PM   #7
jkpd2000
Board Regular
 
Join Date: May 2002
Posts: 91
Default

odbc
jkpd2000 is offline   Reply With Quote
Old May 15th, 2002, 02:29 PM   #8
sen_edp
Board Regular
 
sen_edp's Avatar
 
Join Date: Mar 2002
Location: Hellas
Posts: 553
Default

hello again

What is the problem, in detail please !

__________________
Best Regards
Andreas
sen_edp is offline   Reply With Quote
Old May 15th, 2002, 02:32 PM   #9
jkpd2000
Board Regular
 
Join Date: May 2002
Posts: 91
Default

it is saying getting connected but never ending
jkpd2000 is offline   Reply With Quote
Old May 15th, 2002, 02:35 PM   #10
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Hi,

Sorry, but my experience of connecting to databases other than Access is limited but bear with me. The way I understand it is that the code will work with many databases including Oracle. The only part that needs to be change is the Provider and the ConnectionString. This link may help:-

http://www.topxml.com/conference/wro...c/text/dov.asp

I'd like to know if you manage to get a connection so please post back if you're successful.

Regards,
Dan
dk is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 12:21 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes