Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2003, 07:36 AM
Authorized User
 
Join Date: Nov 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default using ADODB in updating and add records

I am currently writing code in order to add , modify (edit), and delete records.

i have few text box holding data. besides that, i am using ADODB to connect to my database. I am able to connect and retrieve records from tables. but I couldnt add new record, update modified record and delete record.

When this program running, it showed no problems in all this operations, there is totally no error. but when i check back my database, there is no record has been added or updated.

The following is some of my code:

Dim cn As New ADODB.connection
Dim rs As New ADODB.Recordset
Dim strsql As String

Private Sub Form_Load()
On Error Resume Next

     'Create Connection Object
    cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.3.51;" & _
                          "data source=C:\data.mdb;"
    cn.Open

    rs.CursorLocation = adUseClient

    strsql = "SELECT * FROM data;"

    'Create Recordset Object
    rs.Open strsql, cn, adOpenKeyset, adLockBatchOptimistic
    Set rs.ActiveConnection = Nothing

End Sub

'Add new record
Private Sub Add_Click()
On Error Resume Next

    tbRecordNo.text = rs.RecordCount + 1

End Sub

Private Sub Save_Click()
            rs.AddNew
End Sub

Private Sub Delete_Click()
On Error Resume Next
        rs.Delete
End Sub

Above is some of my code. In addition, I got no idea on how to modify and update my records.

Advanced thanks to those who able to help me.

 
Old November 30th, 2003, 08:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You set the recordset's connection to nothing right after you open it, so it is no longer connected to the database. If it is not connected to the database, then no updates to it will be written through to that database...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 3rd, 2003, 12:50 AM
Authorized User
 
Join Date: Dec 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you can do this by two ways:
1.set recordset's field property like this:
rst("fieldname1")=textbox1.text
...
rst("fieldnamen")=textboxn.text
then,call rst.update method.
2.send sql to DBMS:
if you want to add a new record,
strSQL="INSERT INTO tablename(field1,...,fieldn) VALUES(value1,...valuen)"
if you want to modify a exist record,
strSQL="UPDATE tablename SET field1=value1,...,fieldn=valuen"
then call Command object's Execute method like this:
oCmd.CommandText = strSQL
oCmd.Execute
 
Old December 3rd, 2003, 02:22 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Do not use
"On Error Resume Next"
You need to at least get the error so that you know where to trouble shoot.




Sal
 
Old December 10th, 2003, 05:14 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I usually use this code for unbound forms:

rs.AddNew
rs!field1 = Trim(Text1)
rs!field2 = Trim(Text2)
rs.Update
rs.Close
Set rs = Nothing
 
Old August 25th, 2004, 04:40 AM
rjp rjp is offline
Registered User
 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know this was posted last year but i thought it might help someone in the future to know what the actual answer to the question was.

You wanted to know why when you opened the recordset using
something of the nature:
sql = "SELECT * FROM <sometable>"

why it does not update the table <sometable> when you make changes to the recordset and why know errors appear either.

THE MAGIC ANSWER---
Your recordset is not that of the table it is a copied portion of the table (even if it is a complete copy). In order to edit a table using ADODB recordsets you must open the table

eg:
Dim condatabase As New ADODB.connection
Dim rst As New ADODB.recordSet
Set condatabase = CurrentProject.connection
rst.ActiveConnection = condatabase

'Open Recordset Object
rst.Open <sometablename>, cn, adOpenKeyset, adLockBatchOptimistic


Hence we are now editing the table and not a copy of it.
Hope this helps someone.






Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in updating records & finding records naveed77 VB Databases Basics 1 January 16th, 2007 12:12 PM
problem in updating records & finding records naveed77 VB How-To 1 January 16th, 2007 12:10 PM
ADODB Finding matching records rjp Access VBA 0 August 25th, 2004 07:47 PM
Updating records help bspradeep Classic ASP Databases 29 September 9th, 2003 07:00 AM
Updating records help bspradeep Classic ASP Databases 0 July 7th, 2003 10:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.