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

Hello, I currently have a table 1

Status
Not open for further replies.

CVesta81

Technical User
May 17, 2012
23
CA
Hello,

I currently have a table called "tblPictureIndex" that is referenced on my form in Access. I have a button on my form that is meant to clear out two fields in the table based on a field on the form called "CutCode" (also referenced in the same table). My code is listed below:

Code:
Private Sub delPic1_Click()

Dim strUpdate As String
Dim db As DAO.Database

Set db = CurrentDb

strUpdate = "UPDATE tblPictureIndex " & _
    "SET tblPictureIndex.Pic1 = NULL " & _
    "WHERE tblPictureIndex.CutCode = " & "'" & Me.CutCode & "';"
Debug.Print strUpdate
db.Execute strUpdate, dbFailOnError
Set db = Nothing

strUpdate = "UPDATE tblPictureIndex " & _
    "SET tblPictureIndex.PicID1 = NULL " & _
    "WHERE tblPictureIndex.CutCode = " & "'" & Me.CutCode & "';"
Debug.Print strUpdate
db.Execute strUpdate, dbFailOnError
Set db = Nothing

End Sub

The first set of code runs smoothly. On my form the field Pic1 is referenced by a Text Box. However, when Access goes to run the second bit of code, I get Runtime Error 91. The only thing I can think of is that instead of a textbox, PicID1 is referenced by a Combo Box in my form. Does this have anything to do with it or is there something else I am missing? The code specifically stumbles at the second db.Execute.

Thanks for your assistance.
 
Also, I apologize for not putting a title on this post. I was too focused on re-reading it prior to sending that I forgot to add a subject line.
 
Its because you are clearing the DB definition after the first entry. Get rid of the first "Set DB = Nothing" and it should be fine.

However, this can be further optimised. You should be able to combine both fields into a single update as they are on the same table with the same value, something like:

Code:
Private Sub delPic1_Click()

Dim strUpdate As String
Dim db As DAO.Database

Set db = CurrentDb

strUpdate = "UPDATE tblPictureIndex " & _
    "SET tblPictureIndex.Pic1 = NULL, tblPictureIndex.PicID1 = NULL " & _
    "WHERE tblPictureIndex.CutCode = " & "'" & Me.CutCode & "';"
Debug.Print strUpdate
db.Execute strUpdate, dbFailOnError
Set db = Nothing

End Sub
 
This is perfect! Thank you so much! I made my code more efficient as well. Thanks!
 
You can also eliminate some other 'stuff':

Code:
strUpdate = "UPDATE tblPictureIndex " & _
    "SET Pic1 = NULL, PicID1 = NULL " & _
    "WHERE CutCode = '" & Me.CutCode & "';"

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top