Results 1 to 4 of 4

Thread: Cannot delete..please help...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Cannot delete..please help...

    Dear Experts,

    Has anyone could help me please to fix this code...it always failed I tried many time?...This is a "Delete" button, when I press this button, it should be delete row/item on the database...but it always failed....here is the code:


    VB Code:
    1. Private Sub CommandButton3_Click()
    2.  
    3.  Dim con As ADODB.Connection
    4.     Set con = New ADODB.Connection
    5.     con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\exercise.mdb"
    6.    
    7.     Set rst = Execute_SQL_ReturnRecordSet("Select * from Item", False, "C:\exercise.mdb")
    8.    
    9.          rst.MoveFirst
    10. Do While rst.EOF = False
    11.     If UserForm1.txtqty.Text = rst.Fields("Item_Code").Value Then
    12.        Exit Do
    13.     End If
    14.     rst.MoveNext
    15. Loop
    16.  
    17.     TextBox3.Text = rst.Fields("Qty")
    18.        
    19.     Application.ScreenRefresh
    20. End Sub



    When I select item no 1 (using a combo box), then when I press "Check Current Stock" it show its Qty which is "1"...BUT, when I select other item numbers, let say item code number 3 it still show the Qty for item number 1...???....

    Please help with code corrections...

    Thank you so much...

    Jennifer

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Cannot delete..please help...

    If you want to show a particular record, use SQL to get it - there is no need to loop.

    eg:
    VB Code:
    1. Private Sub CommandButton3_Click()
    2.  
    3.  Dim con As ADODB.Connection
    4.     Set con = New ADODB.Connection
    5.     con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\exercise.mdb"
    6.  
    7.     Set rst = Execute_SQL_ReturnRecordSet("Select * from Item Where Item_Code = '" & UserForm1.txtqty.Text & "'", False, "C:\exercise.mdb")
    8.    
    9. If Not rst.EOF Then
    10.   TextBox3.Text = rst.Fields("Qty")
    11. Else
    12.   MsgBox "not found!"
    13. End If
    14.        
    15.     Application.ScreenRefresh
    16. End Sub
    Notice there is no "rst.MoveFirst", as this is done automatically when you open the recordset.

    Also, why do you create a connection object (con), then pass the path of the database to your function?

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Cannot delete..please help...

    You can try something like the ff....


    VB Code:
    1. Private Sub CommandButton3_Click()
    2.     Dim con As ADODB.Connection
    3.     Set con = New ADODB.Connection
    4.     con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\exercise.mdb"
    5.     con.Execute "DELETE * FROM Item WHERE Field = 'criteria'"
    6. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Cannot delete..please help...

    VB Code:
    1. Private Sub CommandButton3_Click()
    2.  
    3.  Dim con As ADODB.Connection
    4.     Set con = New ADODB.Connection
    5.     con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\exercise.mdb"
    6.    
    7.     Set rst = Execute_SQL_ReturnRecordSet("Select * from Item", False, "C:\exercise.mdb")
    8.    
    9.          rst.MoveFirst
    10. Do While NOT rst.EOF
    11.     If UserForm1.txtqty.Text = rst.Fields("Item_Code").Value Then
    12.        Exit Do
    13.     End If
    14.     rst.MoveNext
    15. Loop
    16.  
    17.     'This one generates an error for when there are no rows being pointed to by rst
    18.     'TextBox3.Text = rst.Fields("Qty")
    19.        
    20.     Application.ScreenRefresh
    21. End Sub

    that's as far as code comments

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