PDA

View Full Version : Solved: VBA Update SQL Query Error



Bopo2
11-30-2009, 09:15 AM
Hey all

When I try to run the following line of code, I always get

Object Required runtime error

Here's my update statement, was wondering if anyone can see anything wrong with it, as I've been trying for 2 hours to figure out what's wrong :(

db.Execute ("UPDATE TblResourceType SET TblResourceType.Quantity = " & stockValue & _
" WHERE TblResourceType.ResourceType= " & Forms![FrmResource]![cboresourcetype] & "")

Edited 1-Dec-09 by geekgirlau. Reason: insert line breaks

geekgirlau
11-30-2009, 03:48 PM
There are a couple of things to check here.

First, is your SQL statement correct? You need to check that the SQL statement would actually perform as you expect it to.

Have you declared "db" as a variable?

I generally use

DoCmd.RunSQL <statement>

but this is a personal preference. I like to assign my SQL statement to a variable to make it easier to test it - it's all too easy to accidentally leave off a space, quotation mark or comma!

Bopo2
12-01-2009, 03:43 AM
Thanks for the suggestion! I tried DoCmd.RunSQL and after playing a little more using msg boxes and figured that Access uses literal strings, so inserting " ' " did the trick.

Although I'm prompted within a update message box, is there a method of overiding this? I really don't want the user to be prompted with this. It seems DoCmd.RunSQL accepts another parameter, but it doesn't look like it's related.

geekgirlau
12-01-2009, 03:45 PM
DoCmd.SetWarnings False
DoCmd.RunSQL <statement>
DoCmd.SetWarnings True


Just be careful when using " ' ". Although this will work in most cases, if there is the remotest possibility that your data may contain an apostrophe you are going to have issues. Below are examples of a couple of different methods you can use:


Dim strSQL As String


' option 1
strSQL = "UPDATE TblResourceType " & _
"SET TblResourceType.Quantity = " & stockValue & " " & _
"WHERE TblResourceType.ResourceType= " & _
Chr(34) & Forms![FrmResource]![cboresourcetype] & Chr(34)

' option 2
strSQL = "UPDATE TblResourceType " & _
"SET TblResourceType.Quantity = " & stockValue & " " & _
"WHERE TblResourceType.ResourceType= """ & Forms![FrmResource]![cboresourcetype] & """"


DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

CreganTur
12-02-2009, 08:50 AM
Whenever you play with the SetWarnings function you must be certain to set it back to True when you're done. If you don't do this, then your alerts will be turned off for almost everything until you set the value to True again. It can cause a number of problems, so be aware of this.

geekgirlau
12-02-2009, 08:08 PM
Good point Randy.

Of course normally you would have your error handling there as well, so you might end up with something like this to ensure that the warnings are turned back on:


'...
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL

ExitHere:
DoCmd.SetWarnings True
Exit Sub

ErrHandler:
MsgBox "(" & Err.Number & ")" & vbCrLf & _
Err.Description, vbCritical, "Unexpected Error"
Resume ExitHere
End Sub

Bopo2
12-03-2009, 08:48 AM
Thanks for the suggestions, a final query (;)) I have, can DoCMD return a value, for example

sqlQuery = "SELECT COUNT (EmpNum) FROM TblRsrcEmp WHERE EmpNum = " & employeeName & " AND Returned = 'No'"
DoCmd.RunSQL (sqlQuery)
I'd like to store the result on that query in a variable, however currently I haven't found a method of DoCMD's that would provide this functionality.

OBP
12-03-2009, 09:55 AM
You can set a Recordset to your SQL and that will return the data for you to put it in to a variable.
Like this.
Dim er As Double, rs As Object, SQL As String
SQL = "SELECT LabourRates.* " & _
"FROM LabourRates " & _
"WHERE Date1 <= #" & Format(Me.Date, "mm/dd/yyyy") & "#" & _
"And Date2 > #" & Format(Me.Date, "mm/dd/yyyy") & "#"
Set rs = CurrentDb.OpenRecordset(SQL)
er = rs.EndUserRate
rs.Close
Set rs = Nothing

Bopo2
12-04-2009, 10:05 AM
Cheers for the example.