Sql If

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jollywg
    New Member
    • Mar 2008
    • 158

    Sql If

    I am in desperate need to know how to do a simple IF statement in access 2007 sql. ie.

    if( check = true) then tbl1.col1 = (tbl1.col1*tbl2 .col2)

    thank you for any help
  • questionit
    Contributor
    • Feb 2007
    • 553

    #2
    Originally posted by Jollywg
    I am in desperate need to know how to do a simple IF statement in access 2007 sql. ie.

    if( check = true) then tbl1.col1 = (tbl1.col1*tbl2 .col2)

    thank you for any help
    Hi

    You cant execute sql just like this.

    To change a value in a table, you need to run sql query. You can use RecordSet or you can also use Execute command to run your query.

    If , with your code:
    [code=vb]
    if( check = true)
    [/code]
    you are trying to find out a check-box on your form is selected or not, then the syntax would be something like this.
    [code=vb]
    If checkBoxName = -1 Then //(-1 means it is selected)
    // Do something
    [/code]

    Hope it helps
    Qi

    Comment

    • PianoMan64
      Recognized Expert Contributor
      • Jan 2008
      • 374

      #3
      Originally posted by Jollywg
      I am in desperate need to know how to do a simple IF statement in access 2007 sql. ie.

      if( check = true) then tbl1.col1 = (tbl1.col1*tbl2 .col2)

      thank you for any help
      Do so that within an SQL Statement in Access you would do the following:

      Code:
       
      SELECT Calc:=IIF(tbl1.checkboxname=true,tbl.col1*tbl2.col2,0) FROM tbl1 LEFT JOIN tbl2 ON tbl1.ID = tbl2.LinkID
      You're going to have to have some primary key (i.e. ID) in tbl1 and a Linking ID that will relate the Primary ID in Table 1 to a Reference Key value in Table 2. This way you can have all the child records related to the Parent record all associated together. If there is a One to one relationship, then you'll need to replace LEFT JOIN with INNER JOIN and have the common ID's between the two tables be the same.

      Hope that helps,

      Joe P.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        You've probably got this from the other posts, but SQL doesn't support an IF statement.

        What it can handle is returning different values based on a condition. This is done within the IIf() function. The syntax is basically :
        Code:
        IIf(Condition, ValueIfTrue, ValueIfFalse)

        Comment

        Working...