I tried two diffent ways of opening a recordset using the same strSQL and got two different answers.
This routine captures the Name of a subform and it's recordsource (see below) for use in this onclick event.
The highlighted debug.print rs.recordcount returns different answers. If I take out the DISTINCT word then I get a -1 (ADO) and 1 (DAO)
Code:
Private Sub cmd_UseCasingDefaults_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld3 As DAO.Field
Dim rst1 As ADODB.Recordset 'The default table
Dim fld1 As ADODB.Field
Dim rst2 As ADODB.Recordset 'The data_??? table
Dim fld2 As ADODB.Field
Dim frmName As String
Dim frmRecSource As String
Dim strSQL As String
Dim strAPI As String
Dim strOperationsGroup As String
' MsgBox getSubFrmName()
' MsgBox getSubFrmRecordsource
'Capture API number of well
strAPI = Me.cbo_SelectWell
' 'Capture recordsource name from subform of interest
frmRecSource = getSubFrmRecordsource()
Debug.Print frmRecSource
'
'set SQL statement
strSQL = "Select DISTINCT * FROM (default_Pinedale) WHERE default_Pinedale.TableName = '" & frmRecSource & "' "
Debug.Print strSQL
'Use ADO recordset
Set rst1 = New ADODB.Recordset
Set rst1 = CurrentProject.Connection.Execute(strSQL)
Debug.Print rst1.RecordCount
[highlight]'returns a count of -1 [/highlight]
'Use DAO recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
Debug.Print rs.RecordCount
[highlight]'returns a count of 3 and is correct[/highlight]
'List fields for ADO recordset
For Each fld1 In rst1.Fields
Debug.Print fld1.name
Next fld1
rst1.Close
Set rst1 = Nothing
For Each fld3 In rs.Fields
Debug.Print fld3.name
Next fld3
rs.Close
Set rs = Nothing
End Sub
Three Functions go with this: (credits to MajP and PHV for their help)
Code:
'captures the name of the subform on the current tabbed page
Public Function getSubFrm() As Access.Form
Dim pg As Access.Page
Dim ctl As Access.Control
Set pg = Me.TabCtl60.Pages(TabCtl60.Value)
For Each ctl In pg.Controls
If ctl.ControlType = acSubform Then
Set getSubFrm = ctl.Form
Exit Function
End If
Next ctl
End Function
'Retrieves the Subform name
Public Function getSubFrmName()
getSubFrmName = getSubFrm.name
End Function
'Retrieves the Subform Recordsource
Public Function getSubFrmRecordsource()
getSubFrmRecordsource = getSubFrm.RecordSource
End Function