ADODB Connection to MS ACCESS -- RecordCount returns -1

Sthrncali

Board Regular
Joined
Apr 1, 2011
Messages
226
I am trying to figure out what I need to change in my count to get the recordcount function to work.. I am doing a query (SQL) that will only return either 1 match, or 0 matches. My code is below. The RecordCount returned is "-1", but then it shows me the record value of the query... meaning the record is being returned its just not reflected when I call on recordcount..

When I run this code, I get 2 msgBox displays..

First Prompt "No Records"
Second Prompt "8075118"

Code:
Sub CheckID()

Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String
Dim i As Long, n As Long, lFieldCount As Long
Dim rng As Range

Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection

strDBName = "MyVOCExceptions.accdb"
strMyPath = "\\MOCCSPRI-3203\BTS MyVOC Adjustments"
strDB = strMyPath & "\" & strDBName

connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB

'--------------
'OPEN RECORDSET, ACCESS RECORDS AND FIELDS

Dim ws As Worksheet

Set adoRecSet = New ADODB.Recordset

Dim Survey As String
Survey = "8075118"

strSQL = "SELECT [Survey_ID] FROM SurveyList WHERE [Survey_ID] = " & Survey
adoRecSet.Open Source:=strSQL, ActiveConnection:=connDB, CursorType:=adOpenDynamic, LockType:=adLockOptimistic
If adoRecSet.RecordCount > 0 Then
Else
MsgBox "No Records"
End If

MsgBox adoRecSet.Fields(0).Value
adoRecSet.Close

'close the objects
connDB.Close

'destroy the variables
Set adoRecSet = Nothing
Set connDB = Nothing
End Sub

Clearly the record is being located... Also, I tried changing the surveyID variable to one that I know doesn't exist in the table. When I run that, as expected, vba throws an error because the record doesn't exist: "Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record."

Any ideas how I can get the recordcount to show as 1 when my query is returning 1 record?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Nevermind, I figured it out. For anyone else with this issue, apparently you have to use a different cursortype to get the recordcount.

Code:
adoRecSet.Open Source:=strSQL, ActiveConnection:=connDB, [B]CursorType:=adOpenKeyset[/B], LockType:=adLockOptimistic
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,554
Members
449,237
Latest member
Chase S

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