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) ' 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") Dieser Tipp wurde bereits 31.048 mal aufgerufen. Voriger Tipp | Zufälliger Tipp | Nächster Tipp
Anzeige
Diesen und auch alle anderen Tipps & Tricks finden Sie auch auf unserer aktuellen vb@rchiv Vol.6 Ein absolutes Muss - Geballtes Wissen aus mehr als 8 Jahren vb@rchiv! - nahezu alle Tipps & Tricks und Workshops mit Beispielprojekten - Symbol-Galerie mit mehr als 3.200 Icons im modernen Look Weitere Infos - 4 Entwickler-Vollversionen (u.a. sevFTP für .NET), Online-Update-Funktion u.v.m. |
Neu! sevDTA 3.0 Pro SEPA mit Kontonummernprüfung Erstellen von SEPA-Dateien mit integriertem BIC-Verzeichnis und Konto- nummern-Prüfverfahren, so dass ungültige Bankdaten bereits im Vorfeld ermittelt werden können. Tipp des Monats April 2024 Skyfloy Chart von Microsoft und dazu noch gratis Tutorial für Microsoft Chart Controls für Microsoft .NET Framework 3.5 Access-Tools Vol.1 Über 400 MByte Inhalt Mehr als 250 Access-Beispiele, 25 Add-Ins und ActiveX-Komponenten, 16 VB-Projekt inkl. Source, mehr als 320 Tipps & Tricks für Access und VB |
||||||||||||||||
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein. |