INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Hello, I currently have a table

Hello, I currently have a table

(OP)
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 --> VBA

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.

RE: Hello, I currently have a table

(OP)
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.

RE: Hello, I currently have a table

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 --> VBA

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 

RE: Hello, I currently have a table

(OP)
This is perfect! Thank you so much! I made my code more efficient as well. Thanks!

RE: Hello, I currently have a table

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close