Results 1 to 19 of 19

Thread: [RESOLVED] updating an Access database from VBA (via ADO)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Resolved [RESOLVED] updating an Access database from VBA (via ADO)

    Dear Experts,

    Has anyone could help me please for this scenario:

    I have a word document with its pop up form window...it contains of "Item Code", "Description", "Qty", "Unit Price" and "Total"...and it connected to a database access..my question is, what is the code if I fill out the quantity ("Qty") for order .i.e. I put "2"...then it will automatically reduce 2 qty into the database access...secondly, if the "qty" less than 1...it will show a message box as a warning...

    Furthermore, I attach my file for your review and get understood about what I mean...

    Please help me....

    Thank you very much for your kind helps...

    Jennifer
    Attached Files Attached Files
    Last edited by zach007; Jan 8th, 2006 at 10:18 PM.

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

    Re: Please help me...vb and database access

    I haven't looked into your code but I would like to ask, do you know how to update records in a database? There is a tutorial in the top-most part of this section for you to study...
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Please help me...vb and database access

    Hi,

    I tried to learn but I really stack about this...I dont know how to "update" codes...and when I tried to have a look into the tutorial within this forum, I could not found any "update" data codes...Would you please help me for this code please...I would be very happy if you have a look for a while into my file that I attached before...

    Thank you for your kind attention and helps...

    Jennifer

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

    Re: Please help me...vb and database access

    I think this is answered in this thread.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Please help me...vb and database access

    Hi,

    Would you please have a look trough my code please..???..because it is different case...please try by selecting combobox then put let say 2 qty....I think it is different...

    I have been tried one night but still stack...

    Please have a look and let me know the codes if you know....

    Thank you very much...

    Jennifer

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

    Re: Please help me...vb and database access

    You have an ADO recordset, so the code you need to Update is as explained in the other thread.

    All you need to do is make sure you have selected the correct record first.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Please help me...vb and database access

    Hi,

    I tried many times and lastly I stack with this codes:

    VB Code:
    1. Private Sub txtqty_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    2.    
    3.         Dim ItemCode As String
    4.                        
    5.         ItemCode = Trim$(cmb_item)
    6.  
    7.     If ItemCode <> "" Then
    8.         ' Restrict/filter the recordset by the selected Item_Code
    9.         Debug.Print "Item_Code=" & ItemCode
    10.         rst.Filter = "Item_Code = " & ItemCode
    11.                          
    12.        
    13.         rst.Fields("Qty") = Val(txtqty.Text) - rst.Fields("Qty").Value
    14.         rst.Update 'this updates the recordset etc.
    15.    
    16.  
    17.     'Do your total code here
    18.     txttotal.Text = Val(txtqty.Text) * Val(txtunit.Text)
    19.    
    20.     End If
    21.        
    22. End Sub

    and when I compiled it, there was an error message:

    Run Time Error '3251':

    Current recordset does not support updating. This may be a limitation of the provider, or of the selected lockedtype.

    Please help me repair my codes...

    Thanks a lot for your helps...

    Jennifer

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

    Re: Please help me...vb and database access

    How did you open rst?
    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Please help me...vb and database access

    Hi,

    Sorry...I dont know what do you mean...do you know the code please..????

    I really need help for codes please...???..

    I put my new attachment for your review...it is "update" button for updating "Qty" field in the database...

    Thanks for your helps..

    Jennifer
    Attached Files Attached Files
    Last edited by zach007; Jan 10th, 2006 at 04:00 AM.

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

    Re: Please help me...vb and database access

    What dee-u means, is what do you have on the rst.open line?

    I could be wrong, but I think that the rst.Filter could be causing problems here.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Please help me...vb and database access

    Hi,

    Would you please have a look into my file to see my case and please provide me the code.. (for "update" the "Qty" field)???..Please..???...I think that is the best solution since I can learn it from codes...

    Thank you very much...

    Jennifer

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

    Re: Please help me...vb and database access

    I have deleted the other copies of your question, as multiple copies cause confusion (and can annoy people who read all of them).

    You also aren't doing yourself any favours with attaching your code as Word files, as we need to download & open your file (hoping you haven't got any dodgy autorun code) before we can even see it - whereas if you post just the code in question (in this case - one line and a sub) we can see it instantly. We also don't need to have Word installed on the computer we are using at the time.

    In this case it was useful to see it, but we could have asked for it


    The problem seems to be the rst.Filter, which invalidates the recordset. You can use rst.Find instead, or (better) re-load the recordset with just the appropriate data (using a Where clause). You should also only load data for the combo that you need, rather than all of it.

    I had a bit of a fiddle around, but as you have the Execute_SQL_ReturnRecordSet function it is a bit confusing. I couldn't get the update working right without reverting to much more standard ADO code - but this required more work elsewhere.

    I would recommend instead just using an Update statement instead, but for this you will need a Connection object to execute it (if you had your connection object in the form, you wouldn't need half of this code). Here's how you could do it:
    VB Code:
    1. Private Sub cmd_update_Click()
    2.  
    3.   'set up a connection object
    4. Dim con As ADODB.Connection   'dont use Dim .. As New ..  , as it is slower
    5.   Set con = New ADODB.Connection
    6.   con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
    7.           & "c:\exercise.mdb"
    8.  
    9.     'update the data
    10. Dim strSQL as String
    11.   strSQL = "UPDATE item " _
    12.          & " SET Qty = Qty - " & txtqty.Text _
    13.          & " WHERE Item_Code = " & Trim$(cmb_item)
    14.   con.Execute strSQL
    15.  
    16.    'close our temporary connection
    17.   con.Close
    18.   Set con = Nothing
    19.  
    20.    'show the changes in the recordset (may not be needed)
    21.   rst.ReQuery
    22.  
    23. End Sub

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Please help me...vb and database access

    Hi,

    It works..!!!...I am very very happy now.....thank you for helping me...because, it is easier for me to learn from codes...otherwise, I could not imagine the operation will be....Additionally, I just make additional codes like this:

    VB Code:
    1. Private Sub cmd_update_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. If rst.Fields("Qty") > 2 Then
    11.   'update the data
    12.     Dim strSQL As String
    13.     strSQL = "UPDATE item " _
    14.          & " SET Qty = Qty - " & txtqty.Text _
    15.          & " WHERE Item_Code = " & Trim$(cmb_item)
    16.     con.Execute strSQL
    17.     Else
    18.     MsgBox "Your have only 2 stock left."
    19.     End If
    20.    
    21. End Sub

    What shoud I add in this messagebox to show: "Your Eraser has only 2 stock left." which is "Eraser" is the item from Item Description and can be changed based on selected item....

    Thank you so much for your helps...

    Jennifer

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

    Re: Please help me...vb and database access

    Assuming the recordset is pointing to the correct record, like this:
    VB Code:
    1. MsgBox "Your " & rst.Fields("Description") & " have only 2 stock left."

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Please help me...vb and database access

    Hi,

    It works..!!!!...Thank you very much for your kind helps and walktrough...!!!!

    Anyway...from I need your advice, what is the appropriate title of this thread in order everybody can learn about this QTY update please..??? Because, I would like to change this title so anyone can learn from this thread about QTY update....

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

    Re: [RESOLVED] updating an Access database from VBA (via ADO)

    That's very thoughtful of you.

    As the moderator of this forum I can change the thread title*, so I have set it to what I think would be best for others.

    *(as a Super Mod. I can do this for all forums)

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: [RESOLVED] updating an Access database from VBA (via ADO)

    Dear Experts,

    What is the correct codes for this (I have done this codes but errors)...This codes are for updates...but why always errors..???

    VB Code:
    1. Private Sub cmdUpdate_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.   'update the data
    9.     Dim strSQL As String
    10.     strSQL = "UPDATE item " _
    11.         & " SET Item_Code = " & Val(TextBox1.Text) _
    12.         & " SET Description = " & TextBox2.Text _
    13.          & " SET Qty = " & Val(TextBox3.Text) _
    14.          & " SET Unit_Price = " & Val(TextBox4.Text) _
    15.          & " WHERE Item_Code = " & Trim$(cmb_item)
    16.     con.Execute strSQL
    17.    
    18. End Sub

    Because, it said: Run Error Time '2147217900(80040e14)': Syntax Error (Missing operator) in query expression '4 SET Description = Box SET Qty=20 SET Unit_Price = 8'

    Thanks a lot...
    Last edited by zach007; Jan 13th, 2006 at 04:47 AM.

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

    Re: [RESOLVED] updating an Access database from VBA (via ADO)

    Ah yes, I should have mentioned that... just like in VB you need to put quotes around text values, except it needs to be single quotes ( ' ) instead of doubles ( " ).

    Also, instead of Set for each value to set, just separate them with commas, eg:
    eg:
    VB Code:
    1. strSQL = "UPDATE item " _
    2.         & " SET Item_Code = " & Val(TextBox1.Text) _
    3.         & ", Description = '" & TextBox2.Text _
    4.          & "', Qty = " & Val(TextBox3.Text) _
    5.          & ", Unit_Price = " & Val(TextBox4.Text) _
    6.          & " WHERE Item_Code = " & Trim$(cmb_item)

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: [RESOLVED] updating an Access database from VBA (via ADO)

    Hi,

    Thank you so much.....It works...!!!!!.....I think this thread is very good one to learn about 'updating'

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