Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 12th, 2005, 03:44 AM
koinics koinics is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 koinics User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 39 sec
Reputation Power: 0
VB6 ADO Updating Record

Hi, I'm quite new in VB6/ADO and I have encountered a problem in updating records. Below is my code:

Private Sub cmdEdit_Click()
Dim strMsg As String
Dim strDisplay As String
Dim recExist As Boolean
Dim blnPerLot As Boolean

Dim adoConnection As New ADODB.Connection
Dim rstProduct As New ADODB.Recordset

Set adoConnection = New ADODB.Connection
Set rstProduct = New ADODB.Recordset

adoConnection.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Persist Security Info=False;" & _
"Mode=Read|Write;" & _
"Data Source=C:\Program Files\" & _
"Microsoft Visual Studio\VB98\koinics\kafps.mdb"

adoConnection.CursorLocation = adUseClient
adoConnection.Open

rstProduct.CursorLocation = adUseClient
rstProduct.Open "Product", _
adoConnection, adOpenDynamic, _
adLockOptimistic, adCmdTable

'get all Product records...
Set rstProduct = adoConnection.Execute _
("SELECT * FROM Product ORDER BY ProductCode;")

'check if Product record exists before editing...
recExist = False
rstProduct.MoveFirst
Do Until rstProduct.EOF
If AreEqualStrings(rstProduct!ProductCode, txtProductCode) Then
recExist = True
Exit Do
End If
rstProduct.MoveNext
Loop 'Do Until rstProduct.EOF

If Not recExist Then
strDisplay = "Cannot edit, Product record must exists."
strMsg = MsgBox(strDisplay, vbExclamation, "Error Message")
txtProductCode.SetFocus 'Put focus on product code textbox
Exit Sub
End If

If recExist Then 'Product Code exists

rstProduct!ProductCode = txtProductCode
rstProduct!ProductDesc = txtProductDesc
rstProduct!PurchasePrice = txtPurchasePrice
rstProduct!PerLot = IIf(optPerLot, True, False)
rstProduct!ItemsPerLot = txtItemsPerLot
rstProduct.Update

strDisplay = "Product record " & txtProductCode & " updated."
strMsg = MsgBox(strDisplay, vbInformation, "F.Y.I.")
End If

'closing of object variables before
'exiting the procedure to release them...
If rstProduct.State = adStateOpen Then
rstProduct.Close
End If
If adoConnection.State = adStateOpen Then
adoConnection.Close
End If

'setting object variables to Nothing which releases
'the pointers and frees up the memory they consume...
Set rstProduct = Nothing
Set adoConnection = Nothing

End Sub 'cmdEdit_Click()


In debug mode, when the program executes the "rstProduct!ProductCode = txtProductCode" statement before the "rstProduct.Update", a message box indicating a "Run-time error '3251': Current Recordset does not support updating. This may be a limitation of the provider, or of the selected locktype." appears.

I have tried to experiment with other codes suggested by books and web articles but to no success. Appreciate any help. Many thanks!

Nick

Reply With Quote
  #2  
Old July 12th, 2005, 04:58 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 19 m 36 sec
Reputation Power: 181
Make sure there is a primary key on the table.

Also make sure the .mdb file isn't set as "read-only" on the disk.
__________________
======
Doug G
======
I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain

Reply With Quote
  #3  
Old July 12th, 2005, 10:23 PM
koinics koinics is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 koinics User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 39 sec
Reputation Power: 0
Quote:
Originally Posted by Doug G
Make sure there is a primary key on the table.

Also make sure the .mdb file isn't set as "read-only" on the disk.


Hi Doug,

Thanks for your reply. The table was created with a primary key and is set in read/write mode. I'm able to perform edit (and all other functions) using the Add-Ins Visual Data Manager but couldn't crack how to do it using my program. Appreciate additional advise.

Thanks again.

Reply With Quote
  #4  
Old July 12th, 2005, 10:50 PM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,867 mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 4 Weeks 15 h 20 m 54 sec
Reputation Power: 1762
shouldn't where you reference the fields in the database be written like this?
Code:
rstProduct("ProductCode") = txtProductCode
__________________
Come JOIN the party!!!

Quote of the Month:
Retirement: Because you've given so much of yourself to the company that you don't have anything left we can use.

Questions to Ponder:
What do you do when you see an endangered animal eating an endangered plant?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright© 2008 sbenj69

Reply With Quote
  #5  
Old July 12th, 2005, 11:04 PM
koinics koinics is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 koinics User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 39 sec
Reputation Power: 0
Quote:
Originally Posted by mehere
shouldn't where you reference the fields in the database be written like this?
Code:
rstProduct("ProductCode") = txtProductCode


Same outcome mehere.

"Database fields can be accessed by their name using the RecordSet!FieldName schema, by their index RecordSet.Fields(intIndex) or sometimes by their name from the fields collection RecordSet.Fields("FieldName")." quote taken from www.timesheetsmts.com\adotutorial.htm

Reply With Quote
  #6  
Old July 12th, 2005, 11:23 PM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,867 mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 4 Weeks 15 h 20 m 54 sec
Reputation Power: 1762
since i don't ever use the rs.Update statements ... i'm trying to see where the issue is, but why are you opening the recordset twice?
Code:
Here you open the table itself for updating:
rstProduct.CursorLocation = adUseClient 
rstProduct.Open "Product", _ 
adoConnection, adOpenDynamic, _ 
adLockOptimistic, adCmdTable 

And here, you're using a sql statement ... 
'get all Product records... 
Set rstProduct = adoConnection.Execute _ 
("SELECT * FROM Product ORDER BY ProductCode;") 


I'm just trying to see where the issue is.

Reply With Quote
  #7  
Old July 12th, 2005, 11:44 PM
koinics koinics is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 koinics User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 39 sec
Reputation Power: 0
Quote:
Originally Posted by mehere
since i don't ever use the rs.Update statements ... i'm trying to see where the issue is, but why are you opening the recordset twice?
Code:
Here you open the table itself for updating:
rstProduct.CursorLocation = adUseClient 
rstProduct.Open "Product", _ 
adoConnection, adOpenDynamic, _ 
adLockOptimistic, adCmdTable 

And here, you're using a sql statement ... 
'get all Product records... 
Set rstProduct = adoConnection.Execute _ 
("SELECT * FROM Product ORDER BY ProductCode;") 


I'm just trying to see where the issue is.


the first open statement is to open it for update. the sql is to validate if the record (key) exists before the actual update.

any recommendation?

Reply With Quote
  #8  
Old July 18th, 2005, 06:15 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 19 m 36 sec
Reputation Power: 181
Try using the Jet 4.0 OLEDB provider in your connection string. Examples of most connection strings you might ever need can be found at www.able-consulting.com

Reply With Quote
  #9  
Old August 16th, 2005, 12:51 PM
Frost Frost is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 448 Frost User rank is Corporal (100 - 500 Reputation Level)Frost User rank is Corporal (100 - 500 Reputation Level)Frost User rank is Corporal (100 - 500 Reputation Level)Frost User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 27 m 31 sec
Reputation Power: 7
Any luck with this?? I'm getting the same error. By the way, I'm using the Jet 4.0 OLEDB provider and it makes no difference.

The update code I have works great with SQL, unfortunately I have to use access in this case.

I suspect it's got something to do with the lock options that are part of the connection, but I don't know what.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > VB6 ADO Updating Record


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT