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 Apr 9th, 2002, 04:39 PM   #1
MPullen
New Member
 
Join Date: Apr 2002
Location: Vancouver, Canada
Posts: 13
Default

I'm using the SQLExecQuery command to pull data from my database. But I want to use a cell within the Excel worksheet as part of the query.
ie.
"SELECT CLIENTS.NAME FROM DATA.CLIENTS WHERE CLIENTS.CLIENT_ID = ??"

Where ??, I'd like to reference cell B4.
Is this possible?
What's the formula?
Thanks
MPullen is offline   Reply With Quote
Old Apr 9th, 2002, 04:43 PM   #2
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

="SELECT CLIENTS.NAME FROM DATA.CLIENTS WHERE CLIENTS.CLIENT_ID = '"&B4&"'"


[ This Message was edited by: Mark W. on 2002-04-09 16:18 ]
Mark W. is offline   Reply With Quote
Old Apr 10th, 2002, 10:09 AM   #3
MPullen
New Member
 
Join Date: Apr 2002
Location: Vancouver, Canada
Posts: 13
Default

Thanks for the help Mark, but I tried your suggestion and I can't get it to work.

Have you any other suggestions please?

Thanks
MPullen is offline   Reply With Quote
Old Apr 10th, 2002, 10:19 AM   #4
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

I always tie myself in knots with quotation marks but you could try...

"SELECT CLIENTS.NAME FROM DATA.CLIENTS WHERE CLIENTS.CLIENT_ID = " & Range("B4").Value

(I'm not sure whether the B4 needs a double set of quotes on either side??)

Or, have a variable...

myClientID = Range("B4").Value

Then

"SELECT CLIENTS.NAME FROM DATA.CLIENTS WHERE CLIENTS.CLIENT_ID = " & myClientID


AJ is offline   Reply With Quote
Old Apr 10th, 2002, 10:19 AM   #5
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Sorry, I didn't recognize this as a VBA command... and, confused it with the SQL.REQUEST worksheet function. VBA isn't my forte, but couldn't you assign Rang.("B4") to a variable and concatenate that to your string? Like they did with "chan" in the example from the SQLExecQuery Function help topic...

databaseName = "Northwind"
queryString = _
"SELECT * FROM product.dbf WHERE (product.ON_ORDER<>0)"
chan = SQLOpen("DSN=" & databaseName)
SQLExecQuery chan, queryString
Set output = Worksheets("Sheet1").Range("A1")
SQLRetrieve chan, output, , , True
SQLClose chan
Mark W. is offline   Reply With Quote
Old Apr 10th, 2002, 01:59 PM   #6
MPullen
New Member
 
Join Date: Apr 2002
Location: Vancouver, Canada
Posts: 13
Default

You help does make sense, but I still can't get it to work.

If I enter a CLIENT_ID ie 'GW0004' into the script, it works fine.
But not when I try to reference a cell on the worksheet.

Here's all the code, just incase I've screwed up somewhere else.
Your continuing help is much appricated.

Sub LeaseName()
Dim DB As Variant
Dim MYID As Variant

MYID = Range("B5").Value

DB = SQLOpen("DSN=Database;UID=User;PWD=Password")

SQLExecQuery DB, "SELECT CLIENTS.NAME FROM DATA_MERGED.CLIENTS CLIENTS WHERE CLIENTS.CLIENT_ID = " & MYID

SQLRetrieve DB, ActiveCell

SQLClose (DB)

End Sub
MPullen is offline   Reply With Quote
Old Apr 10th, 2002, 02:22 PM   #7
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

This is beginning to bug me coz I know it can work! I do the same thing with a mySQL database and have no problems.
My exact code (well, relevant bits anyway) is below...



Dim Chan As Variant
Dim MyBase As Variant
Dim myCompany As Variant

MyBase = "DSN=alexismedical;DB=alexismedical;SERVER=10.10.10.254;UID=xxxx;PWD=xxxx;PORT=;OPTION=3098;STMT=;"

Range("D" & currentrow).Select
myCompany = ActiveCell.Value


Chan = SQLOpen _
(MyBase, , 2)

SQLExecQuery Chan, _
"SELECT company_id, lookup_label FROM company_lookup WHERE lookup_label='" & myCompany & "' "

SQLRetrieve Chan, ActiveSheet.Range("E" & currentrow), , , False, False, False, False

SQLClose (Chan)



Now, the only discernible difference I can see is that I wrap my variable in the SQL statement with 'single' quotes.

Try this. If it doesn't work then I think I'm well stumped.

Rgds
AJ

AJ is offline   Reply With Quote
Old Apr 10th, 2002, 04:40 PM   #8
MPullen
New Member
 
Join Date: Apr 2002
Location: Vancouver, Canada
Posts: 13
Default

Thanks for help...
Works great!!!
MPullen is offline   Reply With Quote
Old Apr 10th, 2002, 04:59 PM   #9
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

Cool!
Very pleased it worked!

And, just for your information, I've just found out why (thanks to a know-it-all mate sat a few desks away!).

For any ANSI compliant database system when you write a query with alpha or alphanumerics in it, you have to enclose them in single quotes so that it can tell whether you are looking for a value in a column or an another actual column itself in the table.

For example, if you say

SELECT ... FROM TABLE... WHERE COLUMNX=BLAHBLAHBLAH,

it will look for a column BLAHBLAHBLAH and see if the value in COLUMNX equals it!

However, if the BLAHBLAHBLAH is in quotes, it will look for the item BLAHBLAHBLAH within COLUMNX.

For numeric stuff though, it's cool (as long as the properties of that field in the table is defined as numeric of course).

The fact that Excel is the database interface is irreleveant when using the xlodbc.xla addin. It sends the query to the database exactly as if you'd done it to the database directly.

Enough of my ramblings!
Hope this makes sense!

Cheers
AJ


AJ 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 05:09 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