Solved

Using UPDATE statement in Access VBA

Posted on 2014-11-13
3
135 Views
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

0
Comment
Question by:marku24
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
Comment Utility
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.Edit
    rs!RebateApplied = strRebateApplied
    rs.Update
    rs.movenext 
WEND

rs.Close
Set rs = nothing

Open in new window

0
 

Author Comment

by:marku24
Comment Utility
single record
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now