cannot do rs.AddNew after setting rs=cn.Execute(strsql)

peace

New Member
Joined
Nov 6, 2013
Messages
9
I have opened my recordset rs with adLockOptimistic which set LockType to 3. However after I Set rs = cn.Execute(strSQL) which executes my SQL string, the LockType got reset to 1, which is read-only and I cannot do an rs.AddNew to the recordset.

Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD] Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseServer
rs.Open Source:="Customer", _
ActiveConnection:=cn, _
CursorType:=adOpenDynamic, _
LockType:=adLockOptimistic, _
Options:=adCmdTable
Dim strSQL As String
'New CustId
Range("A2:A100").Select

For Each id In Selection
NewCustId = id.Value
If id.Value <> "" Then
strSQL = "SELECT * FROM Customer " & _
"WHERE CustId = '" & NewId & "' " & ";"


Debug.Print "LT Before:", rs.LockType
Set rs = cn.Execute(strSQL)
Debug.Print "LT After:", rs.LockType


If (rs.BOF And rs.EOF) Then
rs.AddNew
rs.Fields("CustomerID").Value = CustId
rs.Fields("Addr").Value = id.Offset(0, 1).Value


... more customer info


rs.Update
Else
Debug.Print "Already existed: ", id.Value
End If
End If
Next id[/TD]
[/TR]
[TR]
[TD] Using an Execute on my connection, how do I do .AddNew ... .Update on the recordset - make it not for read-only. Please help![/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Do you have these ID's in an Excel spreadsheet? What's the aim of your process. It looks like you are adding customers. Are the customer ID unique (Cannot be duplicated in the table)? How much information are you adding when you say "...more customer info".

Offhand it looks vaguely wrong to select based on a criteria and then add to the same recordset. Plus your recordset type is declared as a table type, but you aren't doing that. I'd probably go about this differently. Not sure. And you normally don't "execute" select queries - though that might work here anyway.
 
Upvote 0
Instead of Execute why not use Open?
 
Upvote 0
Thanks for the quick reply. Yes, CustId is unique and is in a spreadsheet. I just want to store the Custumer table in Access. I do a Select and if the CustId does not exist, I would do an AddNew followed by an update.
 
Upvote 0
Thanks! I already did an Open in the beginning. I'll try to use another Open in the loop as well.
 
Upvote 0
Or you could use an actual UPDATE/INSERT query.
 
Upvote 0
If you want to you can just insert all the records. If they already exist, they just fail on insertion. I'd probably write this from the access side but in principle it's the same if you do it from the excel side.

From Access:
Import all the records to a temp table.
Run an append query to append (duplicates will be rejected)

From Excel:
Insert each record to the table
Duplicates will error (catch the error)

Or to build on your current code:
For each record, check if it exists (open a recordset and check for EOF, or run a count() query)
If the record doesn't, exist, either 1) open a recordset and do the .AddNew stuff, or 2) just create an insert query and run it against the database - which personally I find easier, if you are know how to work with strings in VBA anyway.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top