Public Function getRecs(sql As String) As DAO.Recordset
' Khai bao bien
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
' Bay loi
On Error GoTo ErrorHandler
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sql , dbOpenDynaset)
Set getRecs = rst
Exit Function
''''''''''''''''''''''''''''''''
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function
'Su dung ham
Private Sub Command1_Click()
Dim getRS As DAO.Recordset
Set getRS = getRecs("Select * From tbTest")
getRS.MoveFirst
MsgBox getRS!ID
' Trong Function neu Close thi ham su dung se bi LOI khi lay bang ghi
getRS.Close
Set getRS = Nothing
End Sub
Khi viết 1 dự án lớn, bạn cần biết cách chuyển hàm lấy giá trị bảng ghi ra Recordset để khi gọi cho nhanh