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"
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?
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?