1

How do I get the name of an ADODB recordset in VBA? DAO has a .Name property but ADODB does not seem to have that.

At first I assumed there is no such property but now, I think there has to be.

If you set the recordset of a form programmatically, and then close and save the form WITHOUT setting the recordset property to nothing, the forms recordsource property will be filled out with the name of the recordset that you opened.

Example:

I set the recordset like this

Set Me.Recordset = oDal.OpenRecordset("tblOptionList")

If I do not do

Set Me.Recordset = Nothing

tblOptionList will be in the forms recordsource property

Here is my function that opens Recordsets

Public Function OpenRecordset(ByVal Tablename As String) As         ADODB.Recordset
    Dim FilePath As String
    Dim rs As ADODB.Recordset

    Set rs = New ADODB.Recordset

    rs.CursorLocation = adUseClient

    FilePath = GetFilePath(Tablename)

    'Try to get a connection
    Connection

    If ConnOpen Then
        Set rs.ActiveConnection = pDbConn
        rs.CursorType = adOpenKeyset
        rs.LockType = adLockOptimistic
        rs.Open Tablename

        If pEnableOffline Then
            SaveOffline rs, Tablename
        End If
    Else
        'No connection, check for offline availablility

        If Dir(FilePath) <> "" And pEnableOffline Then
            rs.Open FilePath
        Else
            MsgBox "No Offline data and Server is Unavailable"
            Exit Function
        End If
    End If

    Set OpenRecordset = rs

End Function

1 Answer 1

0

The ADODB.Recordset.Source property is the counterpart to the DAO.Recordset.Name property.

If the recordset's data source is a table or saved query, those properties will give you its name. If the data source is a SELECT statement, those properties will show you the statement text.

Sign up to request clarification or add additional context in comments.

2 Comments

Duh! Why did I not see that property? I thought I Searched every single property.
Well, there may be more to it than I described. Apparently you can set the Source to a Command object. But I don't generally use ADO, so wasn't motivated enough to investigate that one. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.