Microsoft Developer Network > Forums Home > Microsoft ISV Community Center Forums > Visual Basic for Applications (VBA) > A question on inputting data into one field or another based on a conditional statement
Ask a questionAsk a question
 

AnswerA question on inputting data into one field or another based on a conditional statement

  • Friday, November 06, 2009 2:25 AMThe_Rattlesnake Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    I am currently working on a database for a class project.  One of the things I am trying to do is to insert a value into one of two fields based on the results of a conditional statement through the use of a form.  Here is the set up.  You open up the form and you see a combo box with one of two choices, up or down.  Also on this form is a submit button.  Now, here is what I want it to do.  When you choose the value up from the combo box and press submit it will write a value to a field called 'up status' found on the table MLT.  If you choose the value down from the combo box and press submit it will write a value to a field called 'down status' found on the same table. 

    I know this will have to be done through VBA.  I just dont know how.  Any tips I should consider?  Is there any help you can offer?

    Thanks

Answers

  • Friday, November 06, 2009 8:49 AMCinzia PaganiMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi,
    it's not clear from your description, if this value must be added to the table MLT or if it has to update a record already existing.
    So in case you must add a record on click event of Submit button you'll write:

    if me.ComboBox1 = "UP" then
    CurrentDB.Execute "INSERT INTO MLT ([Up status]) VALUES(" & YourValue & ")"
    else
    CurrentDB.Execute "INSERT INTO MLT ([Down status]) VALUES(" & YourValue & ")"
    end if
    
    


    in the UPDATE CASE:

    if me.ComboBox1 = "UP" then
    CurrentDB.Execute "UPDATE MLT  SET [Up status=" & YourValue & " WHERE YOURCONDITION"
    else
    CurrentDB.Execute "UPDATE MLT SET [Down status] =" & YourValue & " WHERE YOURCONDITION"
    end if
    
    

    Cinzia


    Sito RIO
    Il mio Blog

All Replies

  • Friday, November 06, 2009 8:49 AMCinzia PaganiMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi,
    it's not clear from your description, if this value must be added to the table MLT or if it has to update a record already existing.
    So in case you must add a record on click event of Submit button you'll write:

    if me.ComboBox1 = "UP" then
    CurrentDB.Execute "INSERT INTO MLT ([Up status]) VALUES(" & YourValue & ")"
    else
    CurrentDB.Execute "INSERT INTO MLT ([Down status]) VALUES(" & YourValue & ")"
    end if
    
    


    in the UPDATE CASE:

    if me.ComboBox1 = "UP" then
    CurrentDB.Execute "UPDATE MLT  SET [Up status=" & YourValue & " WHERE YOURCONDITION"
    else
    CurrentDB.Execute "UPDATE MLT SET [Down status] =" & YourValue & " WHERE YOURCONDITION"
    end if
    
    

    Cinzia


    Sito RIO
    Il mio Blog