Results 1 to 9 of 9
  1. #1
    tagteam is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    486

    Using iif statement to update data

    I am using a form to update data in a table. I use a variable to get the row Id of the record I am going to update. Then I get variables for all the things I want to update.



    In the update query I use the iif statement to update things that need to be updated and leave things that I dont want to change by leaving their fields blank. I have to use the iif statement so I dont update an existing value to a blank value.

    Here is an example of the iif i use

    IIf([Forms]![GUF_GUSDataEntry]![CustomerID] Is Null,[tmpGU_GUS].[CustomerID],[Forms]![GUF_GUSDataEntry]![CustomerID])


    Basically it is iif the update field is null, update the value to its existing value, else update to the form value.

    What i would like to know is if there is a way to have the first if condition basically say do nothing instead of updating it to its existing value.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,016
    Update the value to it's existing value?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,016
    Why are you not using a bound form?
    Build the update sql depending on what has been updated, omitting what has not changed.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    tagteam is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    486
    I usually do this because the user is going to update several records. This allows me to make a temporary table so they can review the data and then process the update. But yes, you are right. If they are going to just update one line a time I can just use something like Me.AccountingPeriod = Me.NewAccountingPeriod and attach it to a button. I use the unbound fields because they will do multiple updates and that way I can retain the value they have entered.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,016
    Well in that case I would build the sql string, from two strings.
    One would hold the field names, and the other the new values, and the concatenate them.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    tagteam is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    486
    I tried the idea of just updating the values using an if statement. I have a form that has all the field values I might want to change on it. It also has an unbound field for each of the data fields prefixed with New. Then i tried to just use and if statement to set the current value to the new value.

    I did this:
    If Me.NewAccountingPeriod Is Not Null Then
    Me.AccountingPeriod = Me.NewAccountingPeriod
    End If

    However, i get the following error: Run-time error 424: Object Required.

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,123
    In VBA you should use the Not IsNull(Me.NewAccountingPeriod) function to check for Nulls:
    https://nolongerset.com/null-handling-in-ms-access/

    https://www.techrepublic.com/article/learn-the-differences-between-is-null-and-isnull-in-access/#:~:text=Is%20Null%20and%20IsNull()%20both%20find% 20null%20values%2C%20but,used%20only%20in%20VBA%20 modules.


    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    tagteam is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    486
    Thanks Gicu, That worked great. I am not a programmer so Not isnull(lkdsfl) is not something I would have thought of.

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,123
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Update statement using if
    By mindbender in forum Queries
    Replies: 24
    Last Post: 09-13-2017, 01:19 PM
  2. VBA Update Statement to update Date field
    By zephyr223 in forum Programming
    Replies: 6
    Last Post: 10-27-2016, 10:45 AM
  3. Update to SQL statement
    By zbaker in forum Queries
    Replies: 5
    Last Post: 01-22-2015, 02:17 PM
  4. UPDATE TO IIF Statement
    By anilytics in forum Queries
    Replies: 5
    Last Post: 03-09-2012, 03:45 AM
  5. SQL Update statement help
    By kalltim in forum Access
    Replies: 6
    Last Post: 01-18-2012, 07:30 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