Using UPDATE statement in Access VBA

Posted on 2014-11-13
Medium Priority
Last Modified: 2014-11-13
I am having a problem getting the below statement to work.  It doesn't give me an error but also does not update the value in my table.

strSQL = "Select * from [tblSourcedHires] where SourceHireID = " & intSourceHireID & " "

                    Set rs = db.OpenRecordset(strSQL)

                        If rs.RecordCount > 0 Then

                            strSQL = "UPDATE [tblSourcedHires] SET(RebateApplied) = " & strRebateApplied & " where SourceHireID = " & intSourceHireID & ""

                            db.Execute (strSQL)
                        End If

Open in new window

Question by:marku24
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 48

Accepted Solution

Dale Fye earned 2000 total points
ID: 40441257
Is this supposed to only update a single record, or multiple?
strSQL = "SELECT * from [tblSourcedHires] " _
       & "WHERE SourceHireID = " & intSourceHireID

Set rs = db.OpenRecordset(strSQL)

While not rs.EOF
    rs!RebateApplied = strRebateApplied

Set rs = nothing

Open in new window


Author Comment

ID: 40441266
single record
LVL 48

Expert Comment

by:Dale Fye
ID: 40441299
The syntax I provided above would handle single or multiple, although I prefer to use an update query if there are multiples.

Or you could simply use:
strSQL = "UPDATE [tblSourcedHires] " _
     & "SET [RebateApplied] = " & strRebateApplied & " " _
     & "WHERE SourceHireID = " & intSourceHireID
debug.print strsql
Currentdb.Execute strsql, dbfailonerror

Open in new window

If strRebateApplied is actually a string, then you would probably want to use:
strSQL = "UPDATE [tblSourcedHires] " _
     & "SET [RebateApplied] = '" & strRebateApplied & "' " _
     & "WHERE SourceHireID = " & intSourceHireID
debug.print strsql
Currentdb.Execute strsql, dbfailonerror

Open in new window

'note that this latter version contains single quotes.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question