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

Run time Error 91 2

Status
Not open for further replies.

merlynsdad

Programmer
Nov 18, 2010
175
US
I'm taking data from a form and putting it into a table. ID is an autonumber in the table and Description is memo.
Code:
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strUpdate As String
Dim varID As Variant
Dim strSQL As String

varID = Forms!frmEditTicket!ID
strUpdate = Forms!frmEditTicket!txtUpdate

strSQL = "UPDATE tblMaster " _
    & "SET Description =  ('" & strUpdate & "') " _
    & "WHERE ID = ('" & varID & "');"
debug.print strSQL
    db.Execute strSQL
The SQL is ok, and varID and strUpdate have the correct values in the watch window. I'm still getting an object variable or block variable not set error.

If the square peg won't fit in the round hole, sand off the corners.
 
You missed this:
Set db = CurrentDb

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Plus, if "ID is an autonumber", why do you push it as a string?

Consider:
Code:
strSQL = "UPDATE tblMaster " _
    & "SET Description = ('" & Replace(strUpdate, "'", "''") & "') " _
    & "WHERE ID = (" & varID & ");"
The Replace part is in case you would have any single quotes in strUpdate.

Have fun.

---- Andy
 
Can you actually explain exactly what the Replace is doing? I know Find/Replace, and I've seen how that works in a SQL string, but I'm confused about what's getting replaced in your example above. I think I understand it's so if a word like "can't" appears in the string, the single apostrophe in the word won't throw off the code. Is that correct, or am I completely missing the point?

If the square peg won't fit in the round hole, sand off the corners.
 

Yes, that’s exactly what the Replace is for.

Single quote will end your Update statement ‘pre-maturely’

You may want to try the Update without the Replace part and make sure you have some single quote(s) in the strUpdate and see what will happen. Then try the same with Replace in your SQL.


Have fun.

---- Andy
 
That actually happened this morning, so your Tuesday post was perfectly timed. Now I understand it. Thanks.

If the square peg won't fit in the round hole, sand off the corners.
 

I use the Replace anywhere where there is a possibility of a quote, especially where user can type anything.

Have fun.

---- Andy
 
I thought I had this down, but I'm having trouble with Replace in the following phrase:
Code:
"Ticket " & intTixNum & " has been entered into Ticket Tracker by " & strSEID & " concerning " & strIssue & ". '" & Replace(strDescription, "'", "''") & "' "
strDescription is the employee's description of the problem. Somewhere I know there's an extra apostrophe lurking there, but I can't find it!

If the square peg won't fit in the round hole, sand off the corners.
 
Create another string variable

Dim sSomeStr as string

sSomeStr=Replace(strDescription, "'", "''")

Put a STOP in your code after that and then look at sSomeStr in your immediate window



"Ticket " & intTixNum & " has been entered into Ticket Tracker by " & strSEID & " concerning " & strIssue & ". '" & sSomeStr& "' "

Put a STOP in your code after that and then look at it in your immediate window
 
Also, one other troubleshooting technique is to use the immediate window to create a string you can execute in the query interface SQL window. Just do a

?strSQL

in the immediate window, end your program, and then cut and paste the results into a blank Query sql window, and then either try to execute it or display it in Design view, and you will get much more detailed information on what your actual error is and you will see how the query processor is interpreting what you are trying to pass to it.

 
Good ideas, but my apostrophes are now showing up as quote marks.

If the square peg won't fit in the round hole, sand off the corners.
 
Myself, I would deal with a situation like this by coding it in ADO instead of trying to do a SQL UPDATE. That would take me like, ten minutes, instead of spending days on a forum trying to figure out how to parse it out.
 
VBAJock, I know the code "thinks" I wanted it to show me apostrophes as quotation marks. The reason I asked the question is, I'm trying to figure out what I did that makes it think that. If you have an answer, I'd appreciate the help. Thanks.

If the square peg won't fit in the round hole, sand off the corners.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top