Results 1 to 3 of 3

Thread: Problem with delete record

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Question Problem with delete record

    Hi,

    I have a database (DAO) that has two tables - Users (Fields are User ID, and Password), and Categories (Fields are UserID, Category, and SubCategory).

    The following is the code I use to delete a Category...


    Private Sub cmdCatDelete_Click()

    Set db = OpenDatabase(App.Path + "\" + "mydb.mdb")
    Set rs = db.OpenRecordset("SELECT Categories.Category From Categories WHERE Categories.Category = " + Chr$(34) + cmbCategory.Text + Chr$(34) + ";")

    rs.MoveFirst
    Do Until rs.EOF

    With rs
    .Delete
    End With

    rs.MoveNext
    Loop
    db.Close

    End sub


    This works fine except that if two differrent users have named a category the same name, it deletes the other users category also.

    How can I delete just the category of the user that is logged in?

    I hope I explained this well enough, and I appreciate any help I can get on this.

    Thanks,
    Ron

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    When you create the recordset, include the UserID in the WHERE clause. You can use the logged in user's UserID for this.

    Also, instead of creating a recordset, you can directly issue an SQL Delete statement.

    Code:
    strSQL = "DELETE FROM Categories WHERE Category = '" & Something & "' AND UserID = '" & Something & "'"
    db.Execute strSQL
    Change the tablename and fieldnames along with types as necessary. I am assuming both the Category and UserID to be text fields.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Cool

    Hey Honeybee,

    Thanks for the input! Although I didn't try your idea, it got me
    thinking. Since this is my first database, and I have been using
    recordsets...I thought I would stick with them.

    Not only did I need to include the UserID in the WHERE clause,
    but also in the SELECT clause. This is what eventually worked...


    Set rs = db.OpenRecordset("SELECT UserID AND Category From Categories WHERE UserID = " + Chr$(34) + cmbUserID.Text + Chr$(34) + " AND Category = " + Chr$(34) + cmbCategory.Text + Chr$(34) + ";")


    Again, thanks a lot, and have a good one,
    Ron

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width