Recordset = ConnectionString, preserving Locktype problem

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
Hello

I've followed the MSN brief on my problem as best I can, but still have issues!

I understand why it does it and why it happens, but can't seem to set my RecordSet and preserve optimistic locktype and dynamic cursor.

If I understood it (probably not) correctly, I need to Open the connection, but not execute it, then set the locktype, then execute the sql string to get it all together, but it still reverts to the default locked argument. I need to open locktype optimistic (or just not readonly) because I have to trim all the values in one field before proceeding with the guts of the code.

Code:
Global RS As Recordset, CS As Worksheet, cstrw As Long

Public Function Multi()

Dim chans As New Scripting.dictionary, iCh As Variant, ChK As Variant
Dim xWb As String, xWbSource As String
Set RS = CreateObject("ADODB.Recordset")
    xWb = ThisWorkbook.Sheets("Multipass").Range("B8").Value

    If Len(Dir$(xWb)) = 0 Then
        Call Err.Raise(vbObjectError + 1024, , "File does not exist!")
    Else
        xWbSource = Left$(xWb, InStrRev(xWb, Application.PathSeparator))
        xWb = Dir$(xWb)
    End If

ImportToRecSet xWb, xWbSource

'...

Public Function ImportToRecSet(ByVal BK As String, Src As String)

Dim strFilespec As String
Dim strConn As String
Dim sqlStr As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'If Not RS Is Nothing Then Set RS = Nothing
Set cnimportconn = New ADODB.Connection
strConn = "Provider=Microsoft.ace.oledb.12.0;Data Source=" & Src & "\;Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
strsql = "SELECT * From [" & BK & "]"

With cnimportconn
        .CursorLocation = adUseServer
        .ConnectionString = strConn
        .Open
        .CommandTimeout = 0
End With


RS.LockType = adLockOptimistic
RS.CursorLocation = adUseClient
RS.CursorType = adOpenDynamic
Set RS = cnimportconn.Execute(strsql)
RS.MoveFirst

End Function


had been following this brief here: http://support.microsoft.com/kb/188857
Cheers
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Also, have you experimented by using the recordset Open method instead of Execute?
 
Upvote 0
I think that's what I was trying to do... I tried following the parts in the MSN doc that explained how to do it (see 'Command_3_click' below)

Code:
Option Explicit
   Dim cn As New ADODB.Connection
   Dim cmd As New ADODB.Command
   Dim rs As New ADODB.Recordset

   Private Sub Command1_Click()
   With cn
      .CursorLocation = adUseServer
     .ConnectionString = "dsn=yourdatasourcename"uid=yourUserId;pwd=yourPassword
      .Open
   End With  
   rs.CursorType = adOpenKeyset
   rs.LockType = adLockOptimistic
   End Sub

   Private Sub Command2_Click()
      Set rs = cn.Execute("select * from authors")
   End Sub

   Private Sub Command3_Click()
     With cmd
       .ActiveConnection = cn
       .CommandType = adCmdText
       .CommandText = "select * from authors"
     End With
     Set rs = cmd.Execute
   End Sub

   Private Sub Command4_Click()
    rs.Open "select * from authors", cn
   End Sub

   Private Sub Command5_Click()
     Debug.Print "CursorLocation " & rs.CursorLocation; ""
     Debug.Print "cursortype " & rs.CursorType
     Debug.Print "locktype " & rs.LockType
   End Sub

On two occasions, in a row, it uses execute. I tried following this and go no further.

In any case, Trimming the field on entry seems to be working.

For anyone else looking for a solution like this, you need to:

Code:
strsql = "SELECT [Field1NameasinCSVColHed], TRIM([Field1NameasinCSVColHed]) as Field1NameasinCSVColHed From [" & BK & "]"

and pay attention to where there are square brackets and not square brackets
 
Upvote 0
I don't see it in your examples, but have you tried
Code:
   Private Sub Command4_Click()
    rs.Open "select * from authors", cn, adOpenDynamic, adLockOptimistic
   End Sub
 
Upvote 0
I was referring to the recordset open method, not the connection open method. ;)

Code:
Sub Demo(ByVal strBK As String, ByVal strDB As String)
    Dim rec As ADODB.Recordset: Set rec = New ADODB.Recordset
    Const strConnection As String = "Provider=Microsoft.ace.oledb.12.0;" & _
                                     "Data Source=" & strDB & "\;" & _
                                     "Extended Properties=""text;" & _
                                     "HDR=Yes;" & _
                                     "FMT=Delimited(,)"";" & _
                                     "Persist Security Info=False"
    Const strSQL As String = "Select * From [< TABLE >]"
    
    Call rec.Open(Replace$(strSQL, "< TABLE >", strBK), strConnection, adOpenDynamic, adLockOptimistic)
    
    'more code...
End Sub
 
Upvote 0
Ha! Jeez that was stupidly easy. The stupidly part being played by me.

Though it now presents me with a final error to my required end result

Connection is made, Record count is 1022, BOF is false, EOF is false, I apply no filters, I have moved first, but when I move to do this:

Rich (BB code):
If RS Is Nothing Then: MsgBox ("You have either picked the wrong file, or the file is empty!"): Exit Function
With RS
        If Not .BOF And Not .EOF Then
                Do Until .EOF = True
                    .Fields(12).Value = VBA.Replace(.Fields(12).Value, "/", "", 1, Len(.Fields(12).Value), vbTextCompare)
                    If Not chans.Exists(.Fields(12).Value) Then chans.Add .Fields(12).Value, I
                    .MoveNext
                Loop
        End If
End With

The result is 'Multi-Step operation generated error. Check each status value'

The only thing I can think, is that the datatype isn't correct for the column. (I recognise this from when you try and pass say a string into an adInteger type field.

Do I have to have 12 lines stipulating what data type each column is? That would make sense, but it seems to connect without it... and I can read from it, so it clearly knows it's a string. Has it also defaulted to some sort of OLE Static or variant type?
 
Upvote 0
All the more reason to make the changes prior to constructing the recordset, or after it has been unloaded. :biggrin:

So perhaps that replacement is what results in say a number, when it expect a string. So try using string type declaration character with Replace, i.e.:

Code:
.Fields(12).Value = Replace[highlight]$[/highlight](.Fields(12).Value, "/", "", 1, Len(.Fields(12).Value), vbTextCompare)

or...

Code:
.Fields(12).Value = [highlight]Cstr([/highlight]Replace(.Fields(12).Value, "/", "", 1, Len(.Fields(12).Value), vbTextCompare)[highlight])[/highlight]
 
Upvote 0
All the more reason to make the changes prior to constructing the recordset, or after it has been unloaded. :biggrin:

So perhaps that replacement is what results in say a number, when it expect a string. So try using string type declaration character with Replace, i.e.:

Code:
.Fields(12).Value = Replace[highlight]$[/highlight](.Fields(12).Value, "/", "", 1, Len(.Fields(12).Value), vbTextCompare)

or...

Code:
.Fields(12).Value = [highlight]Cstr([/highlight]Replace(.Fields(12).Value, "/", "", 1, Len(.Fields(12).Value), vbTextCompare)[highlight])[/highlight]


Still the multi-step generation issue

have tried both

Code:
                    .Fields(12).Value = CStr(VBA.Replace$(CStr(.Fields(12).Value), "/", "", 1, Len(CStr(.Fields(12).Value)), vbTextCompare))
and
Code:
.Fields(12).Value = VBA.Replace$(.Fields(12).Value, "/", "", 1, Len(.Fields(12).Value), vbTextCompare)

using the immediate window, I've done a ?Rs.Fields(12).type, which returns 203. That's 'adVarChar' isn't it?
 
Upvote 0
All the more reason to make the changes prior to constructing the recordset, or after it has been unloaded. :biggrin:

And Whilst I do appreciate the learning to be had, as I said there're sheet names to be had about nyah... If the string in field(12) is clean, I don't have to do any other work later on. If it's not, it's a lot more hassle. And this has to run on lots and lots of rows :)
 
Upvote 0
Im curious as to why you've opted to use ADO for this, I always found it really slow for large csvs (which i think you're querying).

I've always found that opening them, reading them to the end, splitting into arrays (by cr then commas) and then working on them is much faster.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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