locked
Conditional insert query RRS feed

  • Question

  • I am new to Ms Access.  have a table with three field’s field1, field2 and field3. How do I write an insert query when sum of field2 and field3 equals? 

    thanks

    CurrentDb.Execute "Insert into table1(field1,field2,field3) Values ('" & Me!Combo1 & "','" & Me!textbox1.Text & "','" & Me!textbox2.Text & "');"
    MsgBox "Record Saved !!!", vbInformation, "Success"
    


    Wednesday, January 2, 2019 8:59 AM

All replies

  • You talk about fields, but your data is coming from a form, thus controls, no?

    You'd have to use a IF Statement to compare the value and act accordingly, so something along the lines of

        Dim db                    As DAO.Database
    
        Set db = CurrentDb
        If Me!textbox1.Text = Me!textbox2.Text Then
            db.Execute "INSERT INTO table1(field1, field2, field3) " & _
                       "VALUES ('" & Me!Combo1 & "', '" & Me!textbox1.Text & "', '" & Me!textbox2.Text & "');"
            If db.RecordsAffected <> 0 Then
                MsgBox "Record Saved !!!", vbInformation, "Success"
            End If
        End If
        Set db = Nothing


    Daniel Pineault, 2010-2018 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Wednesday, January 2, 2019 12:54 PM
  • thank you so much for reply. i just changed .text to .value and it worked 

    Thursday, January 31, 2019 8:31 AM