Results 1 to 5 of 5
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281

    Writing an IIf Statement Within a Query Using an OptionButton

    Hello Again...



    Trying to write a simple INSERT INTO query using an IIf statement for the value of one of the fields

    The query works perfectly until I put in the IIf statement
    Code:
    CurrentDb.Execute "INSERT INTO [TblTermRecords] (FullName, EmpID, ,SeperationType, LastDateWrkd, EffectiveDate) " & _
          "VALUES ('" & Me.Text23 & "', '" & Me.Text25 & "', (IIf(Me.Option17 = ""True"",""Left Company"",""Dept Transfer"")), #" & Me.Text4 & "#, #" & Me.Text6 & "#)"
    As usual, I have tried every iteration that I (and Google) can think of - Error? Of course, a SYNTAX Error...

    Quick Update...I just saw the extra ',' in front of "SeperationType" - Removed it and now I have a "Too few parameters: Expected 1"

    Thank Very much for any help...

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    Concatenate and remove extra comma after EmpID:
    Code:
    CurrentDb.Execute "INSERT INTO [TblTermRecords] (FullName, EmpID, SeperationType, LastDateWrkd, EffectiveDate) " & _
          "VALUES ('" & Me.Text23 & "', '" & Me.Text25 & "', '" & IIf(Me.Option17 = True, "Left Company", "Dept Transfer") & "', #" & Me.Text4 & "#, #" & Me.Text6 & "#)"
    Boolean values should not be in quotes.

    Why are you saving employee name and not just ID?

    Might want to correct spelling of field name in table: SeparationType
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Hey June - Glad to see you again...

    Yes on the option Group Control - I have removed the quotes from around the Boolean and because my boss requested a flat table for this data storage..

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    You probably read my previous post before I edited it. Should review again.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    June Thank You as always - I've made all the edits - Ran the code and pulled back an 'Error 2427 You entered an expression that has no value'

    After snooping around and revisiting my controls I realized the Frame Control is what is bound to the Form and the option buttons inside were labeled/named as 1 and 2

    So I modified accordingly and gave it a go... The following works perfectly - but if you see something that may cause grief or is inefficient please let me know...

    As for the flat table using both EmpName & ID this is just a quick & dirty stand alone app - once it evolves the tables will be put in SQL and properly normalized...

    Again, Thanks for all of your help June as always...
    Code:
    CurrentDb.Execute "INSERT INTO [TblTermRecords] (FullName, EmpID, SeparationType, LastDateWrkd, EffectiveDate, TermCondition) " & _
          "VALUES('" & Me.TxtEmpName & "', '" & Me.TxtEmpID & "', '" & IIf(Me.FrameTermType = 1, "Left Company", "Dept Transfer") & "', " & _
          "#" & Me.TxtLstWrkDay & "#, #" & Me.TxtEffDate & "#,'" & IIf(Me.FrameTermCond = 1, "Voluntary", "Involuntary") & "' )"

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

Similar Threads

  1. Replies: 3
    Last Post: 07-12-2017, 12:03 PM
  2. Help Writing an If Then Statement in a Query
    By tanyalee123 in forum Queries
    Replies: 1
    Last Post: 09-09-2014, 04:23 PM
  3. Replies: 6
    Last Post: 02-24-2014, 09:26 AM
  4. Replies: 6
    Last Post: 04-06-2010, 03:00 PM
  5. I need help writing a query statement
    By dking in forum Queries
    Replies: 1
    Last Post: 02-25-2009, 09:43 AM

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