Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Code to run Sql Iff
Rex Williams 
   
4 years ago
How do I get this    IIf([testId]=995,105,[Activity ID])
into a
DoCmd.RunSQL "Update [FinalId], IIf([testId]=991,1102,[Activity Id]) From InterestsT")

[Activity Id} is some number between 1 and 100
[TestID] is a number between 1 and 70
I want to update FinalID to either a new number or the existing [Activity Id} number.

say [Activity Id] = 66  and [TestID] = 9945
the number I want returned is 25 if testId = 9945 otherwise 66
table name "InterestsT"

This works in a update query

IIf([testId]=995,105,[Activity ID])

I need to run it as a one line sql I know I will have a 100 lines of code to get all the numbers
Adam Schwanz  @Reply  
           
4 years ago
TestID is a number between 1 and 70, but then it's 9945? I don't really understand the logic with getting your numbers, but you can just use if/then. Something along the lines of this

Dim X as Integer
If TestID=991 Then ' this would be a great spot to not having to right 100 lines of code if you have a method to this number
X=1102 'same as the line above
Else
X=ActivityID
End If

Currentdb.execute "Update InterestsT SET FinalID=" & X & " WHERE ID=" & ID
Rex Williams OP  @Reply  
   
4 years ago
Answer to Docmd.RunSqlSQL
Sentyx
    DoCmd.RunSQL ("Update[Interests] Set[FinalId]= IIF([TestID] = 9921,21,[Finalid])")
The break down of the syntyx
[Interests]  The Table to update
[FinalId] The field to be updated
[TestId] the field to evaluate
9921 the item to be evulated
21 the value to be returned if evualation is true  [FinalId] would = 21
[FinalIDd] The value to return if evualation is false  [FinalId] = [FinalId]
Scott Axton  @Reply  
      
4 years ago
Is this a one time thing or something you will need to do on going?

Rex Williams OP  @Reply  
   
4 years ago
Thanks for the reply.  One time run. Renumbered about 100 items.  Like 1 and 4 became 56; 3 became 9.  
the iff sql worked great just had to copy and paste and change numbers then run a button update to run all 100 sql statements.
Thanks again for the reply.  I just couldn't get the docmd.runsql syntex correct.
Rex
Scott Axton  @Reply  
      
4 years ago
OK so for the future - if this is a one time occurrence  -
I would use a simple update query.  Then just adjust for each case you need to change.

Creating a complex iif and debugging it probably took longer to do than just running an update.
On the other hand you did learn the ins and outs of making the complex iff so there is some benefit there.

My suggestion was going to be to make a real basic table with the "what it is" and the "what it needs to be" fields. then do a DLookup for the records looping through it.  IIF is really not meant to be nested that many levels deep.
I don't know where you are in your knowledge level but even a SELECT CASE statement would be easier to write one time that trying to nest 100 IIFs

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 7/26/2026 1:02:25 PM. PLT: 0s