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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

db.RecordsAffected doesn't always work 3

Status
Not open for further replies.

Lhuffst

Programmer
Jun 23, 2003
503
US
I have sql for insert and updating records. At the end, I use the db.RecordsAffected which sometimes seems to work and sometimes not. For example, I just updated my existing data on a form and although the changes were inserted to the databae, the db.recordsaffected was still 0. Does anyone have any ideas why it wouldn't work and/or if there is another way to make sure that my inserts & updates are working?

The code I use is:
Code:
  If db.RecordsAffected = 0 Then
  MsgBox "Your new Contract Was Not Updated."
  'nill = AddErrorLog(0,"New Contract Was Not Updated!", "CitationUpdate", ssql)
  CitationUpdate = False
  Exit Function
End If
Thanks
Lhuffst
 
Lhuffst,

How are you executing your SQL?
(You haven't shown that part of the code).

ATB

Darrylle

Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
Darrylle, here it is. When I check the table, the changes were updated. If I test it via a query, it works but when it runs via the program, it works but the db.recordsaffected = 0. Thanks lhuffst
Code:
Dim db As Database
Dim rs As Recordset

Set db = CurrentDb()
ssql = "Update tblCitationInfo Set " & _
"fldDateSafetyReceived = '" & txtDateSafetyReceived & "', " & _
"fldPoliceDeptReportingCitation= '" & checkTicks(txtPoliceDeptReportingCitation) & "', " & _
"fldPoliceDeptPhone= '" & checkTicks(txtPolicePhone) & "', " & _
"fldCitationMailDate= '" & txtCitationMailDate & "', " & _
"fldCitationNumber= '" & checkTicks(TxtCitationNum) & "', " & _
"FldCitationDueDate= '" & txtCitationDueDate & "', " & _
"fldCitationAmountDue= '" & checkTicks(txtAmountDue) & "', " & _
"fldCitationLocation= '" & checkTicks(txtCitationLocation) & "', " & _
"fldViolationtype= '" & checkTicks(cboViolationType) & "', " & _
"fldCitationDetail= '" & checkTicks(txtCitationDetail) & "',"
ssql = ssql & _
"fldDateObserved= '" & txtViolationDate & "', " & _
"fldtimeObserved= '" & txtViolationTime & "', " & _
"fldPostedSpeed= '" & checkTicks(txtPostedSpeed) & "', " & _
"fldViolationSpeed= '" & checkTicks(txtViolationSpeed) & "', " & _
"fldDescOfVehicle= '" & checkTicks(txtVehicleDescription) & "', " & _
"fldLicensePlateNumber= '" & checkTicks(txtLicensePlate) & "', " & _
"fldVehicleNumber= '" & checkTicks(txtVehicleNumber) & "', " & _
"fldNumOfOccupants= '" & checkTicks(txtNumOccupants) & "', " & _
"fldGroupName= '" & checkTicks(txtGroupName) & "', " & _
"fldLocation= '" & checkTicks(txtEmpWorkLocation) & "',"
ssql = ssql & _
"fldSupervisorName = '" & checkTicks(txtEmpSupervisor) & "', " & _
"fldDateDueToSafety= '" & txtDateDueToSafety & "', " & _
"fldDateSentToSupervisor= '" & txtDateSentToSupervisor & "', " & _
"fldDateReviewedBySafety= '" & txtDateReviewedBySafety & "', " & _
"fldDefensiveDrivingCandidate = " & ChkDefensiveDriving & ", " & _
"fldDriverImprovementClassDate= '" & txtDateDriverImprovementClassTaken & "', " & _
"fldCounty= '" & checkTicks(CboViolationCounty) & "', " & _
"fldNumberofCitations= '" & checkTicks(txtViolationsReceived) & "', " & _
"fldDriverName = '" & checkTicks(txtDriverName) & "', " & _
"fldDatePaid = '" & txtDateFinePaid & "', " & _
"fldPaymentMethod = '" & checkTicks(txtPaymentMethod) & "', " & _
"fldCheckNumber = '" & checkTicks(txtCheckNumber) & "', " & _
"fldReceiptNumber = '" & checkTicks(txtReceiptNumber) & "' " & _
" Where fldCitationNumber = '" & g_CitationNum & "'"
  db.Execute (ssql)
  

  If db.RecordsAffected = 0 Then
  MsgBox "Your new Contract Was Not Updated."
  'nill = AddErrorLog(0,"New Contract Was Not Updated!", "CitationUpdate", ssql)
  CitationUpdate = False
  Exit Function
End If
 
Just for the heck of it, try unwrapping the parentheses from the ssql argument

try
db.execute ssql

I know it sounds silly, but I have had problems with unneeded parens in the past.
 
Kzutter,
Thanks for the suggestion but I still have the problem.
Lhuffst
 
Hi Lhuffst,

Are your records actually being updated? From the help file:

In a Microsoft Jet workspace, if you provide a syntactically correct SQL statement and have the appropriate permissions, the Execute method won't fail — even if not a single row can be modified or deleted. Therefore, always use the dbFailOnError option when using the Execute method to run an update or delete query. This option generates a run-time error and rolls back all successful changes if any of the records affected are locked and can't be updated or deleted.

Cheers, Iain
 
Hi again,

I was aware of the dbFailOnError switch, and my initial question was leading up to that.

I was waiting for your [tt]db.Execute (ssql)[/tt]

This can be trapped with dbFailOnError.

It is failing somewhere, maybe this will tell you why.

ATB

Darrylle



Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
Hi Darrylle

I did try the dbFailOnError at one point but the results were exactly the same.

As far as the records updating (or adding), they seem to work fine.

Should I close the recordset before I do the db.recordsaffected?
Thanks for the suggestions.
 
Hi Lhuffst,

If it's unreliable, why not just work around:

Add a column to your table, say, Update_Key (Double)

Add a couple of lines of code:

dim dblUpdateKey as double

' convert the current time and date to double
set dblUpdateKey = cdbl(now())

ssql = .... & _
"Update_Key = " & dblUpdateKey

if dcount("Update_Key","tblCitationInfo", "Update_Key = " & dblUpdateKey) = 0 then ...

Regards, Iain



 
Great Thanks for letting me know. I put this to good use.
Lhuffst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top