Rubrik: Datenbanken · ADO | VB-Versionen: VB5, VB6 | 04.07.07 |
ADO-Recordset einfacher öffnen Mit dieser Funktion öffnen Sie ein ADO-Recordset zukünftig mit einem Einzeiler. | ||
Autor: Dieter Otter | Bewertung: | Views: 31.042 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | kein Beispielprojekt |
Die Vorgehensweise zum Erstellen eines ADO-Recordsets ist wie folgt:
Dim oRs As ADODB.Recordset
Set oRs = New ADODB.Recordset With oRs .CursorLocation = ... .Open "SQL-Abfragestring", oConn, adOpenDynamic, adLockOptimistic End With
Das sind also jedesmal mindestens 5 Zeilen Code.
Warum eigentlich nicht diese Zeilen in einer gesonderte öffentliche Funktion kapseln? Dann benötigt man zukünftig nur noch eine einzige Codezeile zum Erstellen des Recordsets!
' ADO-Recordset erstellen Public Function dbOpenRecordset(oConn As ADODB.Connection, _ ByVal sSQL As String, _ Optional ByVal CursorType As ADODB.CursorTypeEnum = adOpenDynamic, _ Optional ByVal LockType As ADODB.LockTypeEnum = adLockOptimistic, _ Optional ByVal CursorLocation As ADODB.CursorLocationEnum = adUseClient) _ As ADODB.Recordset Set dbOpenRecordset = New ADODB.Recordset With dbOpenRecordset .CursorLocation = CursorLocation .Open sSQL, oConn, CursorType, LockType End With End Function
Aufrufbeispiel
Dim oRs As ADODB.Recordset Set oRs = dbOpenRecordset(oConn, "SELECT * FROM Tabelle")
17.07.07: Erweiterung (von Carl-Rainer Zeiss)
Durch nachfolgende Abfrage lässt sich eine komplette Tabelle / Sicht nur mit Angabe des Tabellennamens öffnen:
' ADO-Recordset erstellen Public Function dbOpenRecordset(oConn As ADODB.Connection, _ ByVal sSQL As String, _ Optional ByVal CursorType As ADODB.CursorTypeEnum = adOpenDynamic, _ Optional ByVal LockType As ADODB.LockTypeEnum = adLockOptimistic, _ Optional ByVal CursorLocation As ADODB.CursorLocationEnum = adUseClient) _ As ADODB.Recordset ' Falls kein "SELECT..." im SQL-String angegeben, ' autom. ergänzen If Instr(1, sSQL, "select", vbTextCompare) = 0 Then sSQL = "SELECT * FROM " & sSQL End If Set dbOpenRecordset = New ADODB.Recordset With dbOpenRecordset .CursorLocation = CursorLocation .Open sSQL, oConn, CursorType, LockType End With End Function
Aufrufbeispiel:
Dim oRs As ADODB.Recordset Set oRs = dbOpenRecordset(oConn, "Tabelle")