Results 1 to 15 of 15

Thread: What is wrong with my codes...cannot update/edit

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    What is wrong with my codes...cannot update/edit

    Dear Experts,

    I am going to edit what is in the database BUT it still does not work...why..??

    VB Code:
    1. Private Sub CommandButton2_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=" _
    6.           & "c:\exercise.mdb"
    7.  
    8.  
    9.  
    10.  
    11.   'update the data
    12.     Dim strSQL As String
    13.     strSQL = "UPDATE item " _
    14.         & " SET Item_Code = " & TextBox1.Text _
    15.         & " WHERE Item_Code = " & Trim$(cmb_item)
    16.        
    17.     strSQL = "UPDATE item " _
    18.         & " SET Description = " & TextBox2.Text _
    19.         & " WHERE Item_Code = " & Trim$(cmb_item)
    20.    
    21.     strSQL = "UPDATE item " _
    22.         & " SET Qty = " & TextBox3.Text _
    23.         & " WHERE Item_Code = " & Trim$(cmb_item)
    24.    
    25.     strSQL = "UPDATE item " _
    26.         & " SET Unit_Price = " & TextBox4.Text _
    27.         & " WHERE Item_Code = " & Trim$(cmb_item)
    28.    
    29.     con.Execute strSQL
    30.    
    31. End Sub

    Please have a look and let me know what is wrong with this codes...when I click "Update" it wont happen...

    Thanks a lot...

    Jennifer

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: What is wrong with my codes...cannot update/edit

    Your code should be giving you an error. I'm surprised it isn't. When dealing with text in INSERTING or UPDATING, you need to encapsulate it with single quotes. Try this
    VB Code:
    1. strSQL = "UPDATE item "
    2.  strSQL = strSQL & " SET Item_Code = '" & TextBox1.Text & "' "
    3.  strSQL = strSQL& " WHERE Item_Code = '" & Trim$(cmb_item) & "' "

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: What is wrong with my codes...cannot update/edit

    Quote Originally Posted by Hack
    strSQL = strSQL
    I tried and this is error....

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: What is wrong with my codes...cannot update/edit

    Quote Originally Posted by zach007
    I tried and this is error....
    What error? I use that type of syntax all the time.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: What is wrong with my codes...cannot update/edit

    What is the error.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: What is wrong with my codes...cannot update/edit

    Hi,

    It works..!!!...Thanks a lot...!!!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: What is wrong with my codes...cannot update/edit

    Hi,

    How about this one please....because, it could not delete data in database...here is my code:

    VB Code:
    1. Private Sub cmdDelete_Click()
    2.     If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "Delete?") = vbNo Then      'check if you really want to delete this record
    3.         Exit Sub 'exit the command
    4.     Else
    5.         If Not (rst.BOF = True Or rst.EOF = True) Then
    6.             rst.Delete 'delete the current record
    7.             If Not (rst.BOF = True Or rst.EOF = True) Then
    8.                 rst.MoveNext 'move next
    9.             If rst.EOF Then rst.MoveLast
    10.                 fillfields
    11.             End If
    12.         End If
    13.     End If
    14.    
    15.     Application.ScreenRefresh
    16. End Sub

    What is wrong with it...???

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: What is wrong with my codes...cannot update/edit

    I believe you'll have to refresh the datasource once you perform the delete. In cases like this though, I usually reccommend that you use the connection object's Execute() method to delete the record, and then refill the recordset.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: What is wrong with my codes...cannot update/edit

    How to do that..???..could you please show me the codes..???...which is it is better for me to learn....

    Thanks very much...

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

    Re: What is wrong with my codes...cannot update/edit

    I think mendhak is leading you to something like the ff...

    VB Code:
    1. con.Execute "DELECT * FROM Table WHERE Field='Criteria'"
    2. adoRecordset.Requery
    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

  11. #11
    Addicted Member Quizton's Avatar
    Join Date
    Dec 2005
    Location
    VB Forums
    Posts
    209

    Re: What is wrong with my codes...cannot update/edit

    Try something like this it works


    Private Sub cmdDelete_Click()
    If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "DeleteRecord") = vbNo Then
    Exit Sub
    Else
    rs.Delete
    Set rs = db.OpenRecordset("enter your datatable here", dbOpenTable)
    list
    txtFirstName.Text = vbNullString
    txtFirstName.Enabled = True

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: What is wrong with my codes...cannot update/edit

    Hi,

    I did this code:

    VB Code:
    1. Private Sub cmdDelete_Click()
    2.     If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "Delete?") = vbNo Then      'check if you really want to delete this record
    3.         Exit Sub 'exit the command
    4.     Else
    5.         'If Not (rst.BOF = True Or rst.EOF = True) Then
    6.             rst.Delete 'delete the current record
    7.             'If Not (rst.BOF = True Or rst.EOF = True) Then
    8.                 'rst.MoveNext 'move next
    9.             'If rst.EOF Then rst.MoveLast
    10.                 'fillfields
    11.                 Set rst = db.OpenRecordset("SELECT * FROM Item_Code", dbOpenTable)
    12.                 TextBox1.Text = vbNullString
    13.                 TextBox1.Text = True
    14.                 TextBox2.Text = vbNullString
    15.                 TextBox2.Text = True
    16.                 TextBox3.Text = vbNullString
    17.                 TextBox3.Text = True
    18.                 TextBox4.Text = vbNullString
    19.                 TextBox4.Text = True
    20.             End If
    21.         'End If
    22.     'End If
    23.    
    24.     Application.ScreenRefresh
    25. End Sub

    and received error message: Compile Error: Variable not defined...dbOpenTable is in blue highlight....what kind of variable should I set for this...???..i.e. Dim dbOpentable As ?????"

  13. #13
    Addicted Member Quizton's Avatar
    Join Date
    Dec 2005
    Location
    VB Forums
    Posts
    209

    Re: What is wrong with my codes...cannot update/edit

    set your variables at top of page as a general declaration
    note i used private instead of Dim you can use either
    Option Explicit
    Private db As Database
    Private rs As DAO.Recordset
    Dim sSQL As String
    Private ws As Workspace

    something like this
    Last edited by Quizton; Jan 18th, 2006 at 10:06 AM.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: What is wrong with my codes...cannot update/edit

    Hi,

    I am not using DAO...but, I am using ADO connection..???

  15. #15
    Addicted Member Quizton's Avatar
    Join Date
    Dec 2005
    Location
    VB Forums
    Posts
    209

    Re: What is wrong with my codes...cannot update/edit

    Set rst = db.OpenRecordset("SELECT * FROM Item_Code", dbOpenTable)
    TextBox1.Text = vbNullString
    TextBox1.Text = True
    TextBox2.Text = vbNullString
    TextBox2.Text = True
    TextBox3.Text = vbNullString

    Sorry zach forgot to mention that textbox.text = vbNullString will clear you text boxes .
    Also in my example the app consists of 10 text boxes and a listbox my listbox displays data & data on each row if you click on the name presented in the listbox then all the textboxes fill with data. The texboxes can be anything you would want and if one of those ten textboxes is a idno or something you would like to search for then it would do so all linked to a database.also you can save, add, edit &, delete If this is generaly what your trying to do then pm me and ill pm you the example It's in the codebank but I dont know how to link you to the page so ill just send you it
    Last edited by Quizton; Jan 18th, 2006 at 03:57 PM.

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