using QueryDef and SQL DELETE

ckib

Board Regular
Joined
Sep 14, 2005
Messages
219
Hi, I'm attempting to delete a record from table tblSegment when the value of a field on a form is set to "N". I think there is a problem with my strSQL, I get the error: Run time error '91': Object variable or With block variable not set. Thanks for any help you can provide.

Here is my code:

If Me!List_active_status.Value = "N" And Not IsNull(Me!cont_projectID) Then
strText = "Are you sure you want to remove this contact from the project?"
theAns = MsgBox(strText, vbOKCancel)

If theAns = 2 Then 'response = cancel ... abort process
Undo
Else 'response = Okay
'remove this project/contact from tblSegment
Dim myDeleteQry As QueryDef, myDeleteRS As Recordset

' Define SQL statement for delete query

strSQL = "DELETE tblSegment.seg_projectID, tblSegment.seg_contactID, tblSegment.seg_num ) " & _
"FROM tblSegment " & _
"WHERE (((tblSegment.seg_projectID)='" & strProjectID & "')" & _
"AND ((tblSegment.seg_contactID)='" & strContactID & "'));"

Set myDeleteQry = myDB.CreateQueryDef("", strSQL) ' create temp delete query

' Run temporary QueryDef

myDeleteQry.Execute
myDeleteQry.Close

End If
End If
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Where do you get the error?

The only thing I can see wrong with the SQL string is there doesn't appear to be a space between ) and AND.
 
Upvote 0
Error '91' when ...

I get the error after I click OK in the msgbox. I click on debug and the line:

Set myDeleteQry = myDB.CreateQueryDef("", strSQL) ' create temp delete query

gets highlighted in yellow. I have successfully used CreateQueryDef for an UPDATE query, but have never tried in with a DELETE query. I tried adding a space after the ")" and the "AND" but that had no affect. Any other ideas?
Thanks!
 
Upvote 0
set myDB

Oops! Okay, I added set myDB but now I get:

Run time error '3464':
Datatype mismatch in criteria expression.

I replaced strContactID with lngContactID because I remembered that seg_contactID is a long integer. But I still get the 3464 message. New code looks like this:

Dim myDB As Database

Dim strProjectID As String
Dim lngContactID As Long

strProjectID = Me!cont_projectID
lngContactID = Me!contact_id

If Me!List_active_status.Value = "Y" And IsNull(Me!cont_projectID) Then
strText = "Active Status may not be 'Y' when Project ID is blank."
theAns = MsgBox(strText, vbExclamation)
Undo
GoTo Exit_List_active_status_AfterUpdate
End If

If Me!List_active_status.Value = "N" And Not IsNull(Me!cont_projectID) Then
strText = "Are you sure you want to remove this contact from the project?"
theAns = MsgBox(strText, vbOKCancel)

If theAns = 2 Then 'response = cancel ... abort process
Undo
Else 'response = Okay
'remove this project/contact from tblSegment
Set myDB = CurrentDb()
Dim myDeleteQry As QueryDef, myDeleteRS As Recordset

' Define SQL statement for delete query

strSQL = "DELETE tblSegment.seg_projectID, tblSegment.seg_contactID, tblSegment.seg_num " & _
"FROM tblSegment " & _
"WHERE (((tblSegment.seg_projectID)='" & strProjectID & "') " & _
"AND ((tblSegment.seg_contactID)='" & lngContactID & "'));"

Set myDeleteQry = myDB.CreateQueryDef("", strSQL) ' create temp delete query

' Run temporary QueryDef

myDeleteQry.Execute
myDeleteQry.Close

End If
End If
 
Upvote 0
Do you need single quotes around lngContactID?
 
Upvote 0
run time error '3464' caused by quotes around integer

I just changed that! Yes, that was it. Now works perfectly. :)
Thanks!!!
 
Upvote 0
I realize this works, but, I wanted to ask if you really needed the querydef?
For action queries, you can use:

DoCmd.RunSQL strSQL


And yes, you'd still have had the same syntax problem putting quotes around a non-string as above. This is just simpler and demonstrates a more direct technique.

Mike
 
Upvote 0
docmd.runsql

Just spotted your reply. I'm new to Access, so when I find something in my developers guide that looks like it will do the job, I try it. I haven't seen the docmd.runsql before. I'll try that.
 
Upvote 0
docmd.runsql

I tried docmd.runsql as you suggested, but it generates a message box which requires confirmation. I don't see a way to turn this off when invoking the runsql method. Any ideas?
 
Upvote 0

Forum statistics

Threads
1,215,366
Messages
6,124,516
Members
449,168
Latest member
CheerfulWalker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top