Results 1 to 10 of 10
  1. #1
    shital is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    3

    write stored procedure with 'if else'

    hi,
    Hi all,



    I want to write a stored procedure in MS Access 2007, which will have 'If else condition'

    example:

    if (@flag=0)
    Select * from Table1;
    else if (@flag=1)
    Select * from Table2;
    else
    Select * from Table3;
    How can i write the Stored Procedure in MS Access 2007, which will give me the above output?

    Thanks in advance....

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    the quetion that I would like to ask here is what are you trying achieve:

    here is a simple example:

    I have a text Box Text1 based on its value i will change the row source of a Combobox in the form Combo1. The code is attached to a command button:

    Dim strSQL as String

    If Me.Text1=1 then
    strSQl="Select * From Table1"
    elseid Me.Text1=2 then
    strSQL="Select * From Table2"
    Else
    strSQL="Select * From Table 3"
    End if

    Me.Combo1.RowSource=strSQL

  3. #3
    shital is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    3
    hi,
    in above query 'flag' will be assigned runtime in c#.net application and Access is my backend.
    As per the 'flag' value i want to fetch data from different table.

  4. #4
    Peter M is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2017
    Posts
    67
    If you only have two options then If ... Then .... Else ... End If would work but you might want to use the Case statement instead.

    Select Case varflag 'Go to the "Case based on the varFlag value

    case 1
    select * from Table1

    case 2
    select * from Table2

    case 3
    select * from Table3

    case else
    msgbox "Invalid Flag!"

    end select


    Peter

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,111
    Hi!

    Would you please explain some more about your intended use of this. Access doesn't really have stored procedures, you will probably need to use VBA. How do you intend to call this from your c#.net application? What do you except it to return (ADO recordset, DAO recordset)?

    Cheers,
    Vlad

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    maybe don't expect too many responses. The last post before this recent activity was almost 8 years ago?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,111
    Thanks Micron, never really looked at that until now....

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    Maybe anything this old that isn't archived somehow (like the ones marked Solved) should be automatically. IMO nobody ought to be restarting something this old, even if you're the OP. This is one of many that I've clicked on that I wish the OP would mark so that we're not wasting time looking at solved (or in this case, outdated) stuff. Don't know what view you like to use, but in "New Posts" list, the number of views is a clue. This one is over 2,800 as of today.
    BTW, you're not the first!

  9. #9
    Peter M is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2017
    Posts
    67
    My apologies! - I never noticed. It was near the top (not sure how my view is sorted - I'll check!) but sounded like something that I could contribute to. I'll check the dates from now on!

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    No apologies needed. That didn't come across right. I had hijackers and the op in mind, not the likes of you and me. Pretty sure I'm guilty of the same thing.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. creating Stored Procedure in Access 2007
    By shraddha in forum Access
    Replies: 5
    Last Post: 08-03-2010, 09:43 AM
  2. Replies: 0
    Last Post: 05-12-2010, 09:41 AM
  3. Pass image parameter to stored procedure
    By Kencao in forum Programming
    Replies: 3
    Last Post: 04-28-2010, 11:51 PM
  4. Passing variable values to Stored Procedure
    By rodrigopcnet in forum Access
    Replies: 1
    Last Post: 04-14-2010, 10:35 AM
  5. Replies: 1
    Last Post: 11-17-2009, 06:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums