Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 18th, 2003, 09:46 AM
Authorized User
 
Join Date: Oct 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using A CASE Statement

What is the correct syntax for using a CASE function in a SQL statement that has 4 cases to evaluate when running against an Access 2000 database? In VB the CASE would be as follows:

SELECT CASE Plan
     CASE "MON"
          SupPlan = Amt
     CASE "QUR"
          SupPlan = Amt * 4
     CASE "Ann"
          SupPlan = Amt * 12
     CASE "NON"
          SupPlan = 0
END SELECT

How do I do this same thing with SQL in Access?
 
Old December 18th, 2003, 12:25 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

In SQL:

DECLARE @TestVal int
SET @TestVal = 3

SELECT
    CASE @TestVal
        WHEN 1 THEN 'First'
        WHEN 2 THEN 'Second'
        WHEN 3 THEN 'Third'
        ELSE 'Other'
    END



 
Old December 18th, 2003, 12:30 PM
Authorized User
 
Join Date: Oct 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I can't declare variables in a SQL statement that is going to be used against an Access database. The syntax I am looking for will have to be one long line that can be run against the Microsoft Jet 4.0 engine.
 
Old December 21st, 2003, 06:53 AM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why don't you put your code in a function within a Standard module and invoke it from a query or SQL statement?
 
Old December 24th, 2003, 01:56 AM
Authorized User
 
Join Date: Oct 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I didn't know you could access a VB function with SQL in Access 2000. How do you do that? I have no idea how to create a SQL statement that would reference a function in a VB module.
 
Old December 24th, 2003, 01:39 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Go to modules, add new and put in a public function:
Code:
Public Function GetNow() As Date

  GetNow = Now()
End Function
This one duplicates an existing function but you can do more or less anything.



--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
Case statement cole SQL Language 3 May 8th, 2005 03:02 PM
case statement Hudson40 Access VBA 1 February 11th, 2005 11:31 AM
case statement troubles!! ronny Classic ASP Databases 2 April 10th, 2004 07:32 PM
HELP - IF ELSE & CASE statement savoym SQL Language 3 September 12th, 2003 06:35 AM
case statement jakeone Beginning PHP 10 August 19th, 2003 03:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.