Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with DoCmd.RunSQL Update

Status
Not open for further replies.

IJOC

Technical User
Jan 27, 2009
24
US
I am using the code below to update fields values based on the after update entry in a field. However when I try to run it it wants to update all records in the table and not just the specific CardID. Any ideas on what I am doing wrong?

DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardWantQOH-" & Nz(hold_WLqty, 0) & " where pkeyCardID=""" & Me.pkeyCardID & """"
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardHaveQOH+" & Nz(Me.cmdWantQOHPlus, 0) & " where pkeyCardID=""" & Me.pkeyCardID & """"
Me.intCardWantQOH.Requery
 
Looks like too many double quotes at the end of each SQL string are invalidating your where clauses. Try this:
Code:
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardWantQOH-" & Nz(hold_WLqty, 0) & " where pkeyCardID=""" & Me.pkeyCardID & """
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardHaveQOH+" & Nz(Me.cmdWantQOHPlus, 0) & " where pkeyCardID=""" & Me.pkeyCardID & """

Cogito eggo sum – I think, therefore I am a waffle.
 
SHAME ON ME! Try replacing " " " in the code i posted with " ' " . That is, replace 3 double quotes with double-single-double.

Cogito eggo sum – I think, therefore I am a waffle.
 
So this what I have now and I am still getting the same results. Confused.

DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardWantQOH-" & Nz(hold_WLqty, 0) & " where pkeyCardID=" ' " & Me.pkeyCardID & " ' "
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardHaveQOH+" & Nz(Me.cmdWantQOHPlus, 0) & " where pkeyCardID=" ' " & Me.pkeyCardID & " ' "
Me.intCardWantQOH.Requery
 
I am pretty slow today. Try this:
Code:
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardWantQOH-" & Nz(hold_WLqty, 0) & " where pkeyCardID= ' " & Me.pkeyCardID & " ' "
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardHaveQOH+" & Nz(Me.cmdWantQOHPlus, 0) & " where pkeyCardID= ' " & Me.pkeyCardID & " ' "

Cogito eggo sum – I think, therefore I am a waffle.
 
[banghead] [banghead] [banghead] [banghead]

I meant this:
Code:
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardWantQOH-" & Nz(hold_WLqty, 0) & " where pkeyCardID= '" & Me.pkeyCardID & "'"
DoCmd.RunSQL "Update tblCards set intCardWantQOH=intCardHaveQOH+" & Nz(Me.cmdWantQOHPlus, 0) & " where pkeyCardID= '" & Me.pkeyCardID & "'"

Cogito eggo sum – I think, therefore I am a waffle.
 
I make a practice of always declaring a variable like strSQL, setting the value of strSQL, maybe debug.print strSQL, and then run it. It's too difficult to debug issues when you don't use variables.

Code:
Dim strSQL as String
strSQL = "Update tblCards set intCardWantQOH=intCardWantQOH-" & Nz(hold_WLqty, 0) & " where pkeyCardID= '" & Me.pkeyCardID & "'"
Debug.Print strSQL
DoCmd.RunSQL strSQL
strSQL = "Update tblCards set intCardWantQOH=intCardHaveQOH+" & Nz(Me.cmdWantQOHPlus, 0) & " where pkeyCardID= '" & Me.pkeyCardID & "'"
Debug.Print strSQL
DoCmd.RunSQL strSQL

Duane
Hook'D on Access
MS Access MVP
 
Thanks.

Ok that appears to have gone the trick. Now the only problem I am having is that when I enter a value it shows up for very record in the datasheet but it does only update current records. For example if I am on pkeyCardID 422 and I enter 1 it populates all records in the datasheet with 1 but when i tab out and update using the code it only updates the pkeyCardID 422, which is what I want. I don't want all the other records to say 1. Any ideas?
 
Sorry i meant continuous form view.
 
How are ya IJOC . . .

Also ... I find it hard to believe you intend to update the same field twice! ... as in:
Code:
set [blue]intCardWantQOH[/blue]=intCardWantQOH-"
set [blue]intCardWantQOH[/blue]=intCardHaveQOH+"

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top