Em cam on anh nhe.
dưới đây la toàn bộ code em đã chỉnh sửa lại
'----------------------------------------------------------------------------------------------------
'---・CREATE : 2019/11/06 11:25 Nguyen
'----------------------------------------------------------------------------------------------------
Private Const C_REF_LOCAL_TABLE As Boolean = False
'---・IsTable
Public Function IsTable(ByVal strTable As String) As Boolean
Dim myTDef As DAO.TableDef
IsTable = False
For Each myTDef In CurrentDb.TableDefs
If myTDef.NAME = strTable Then
IsTable = True
Exit Function
End If
Next
End Function
'---・MyDLookup
Public Function MyDLookup(ByVal strITerm As String, ByVal strTable As String, Optional ByVal STRWHERE As String = "") As Variant
On Error GoTo Err_Step
Dim rs As ADODB.Recordset
Dim sSql As String
MyDLookup = Null
If C_REF_LOCAL_TABLE And IsTable(strTable) Then
MyDLookup = DLookup(strITerm, strTable, STRWHERE)
Else
sSql = "SELECT " & strITerm & " AS ITEM FROM " & strTable
If STRWHERE <> "" Then
sSql = sSql & " WHERE " & STRWHERE
End If
Set rs = New ADODB.Recordset
Call SYS_ADO.ExecSQL(sSql, rs:=rs)
If rs.EOF Then
MyDLookup = Null
Else
MyDLookup = rs(0)
End If
Set rs = Nothing
End If
Exit_Step:
Exit Function
Err_Step:
MsgBox Err.Description, vbCritical
Resume Exit_Step
End Function
'---・ExecSQL
Public Function ExecSQL(ByVal pSQL As String, _
Optional ByVal AdoType As Integer = adOpenForwardOnly, _
Optional ByVal AdoReadType As Integer = adLockReadOnly, _
Optional ByRef rs As ADODB.Recordset = Nothing, _
Optional ByRef rtid As Long = -1) As Boolean
'SQLServerConnect
On Error GoTo ERR_TRAP
Dim sInt As Long
Dim blnSelect As Boolean
If InStr(1, pSQL, "INSERT", vbTextCompare) > 0 Then
blnSelect = False
ElseIf InStr(1, pSQL, "DELETE", vbTextCompare) > 0 Then
blnSelect = False
ElseIf InStr(1, pSQL, "UPDATE", vbTextCompare) > 0 Then
blnSelect = False
ElseIf InStr(1, pSQL, "SELECT", vbTextCompare) > 0 Then
blnSelect = True
End If
If Not SYS_ADO.CheckConnection Then
GetConnection:
If Not Connect_Cur Then
Exit Function
End If
End If
On Error GoTo ERR_TRAP
If blnSelect = True Then
'SELECT
If Not rs Is Nothing Then
If rs.State <> 0 Then rs.Close
End If
rs.Open pSQL, cn, AdoType, AdoReadType
Else
'DELETE,INSERT,UPDATE
Dim raf As Long
cn.Execute pSQL, raf
If raf = 0 Then
'do nothing
End If
End If
ExecSQL = True
Exit_Section:
Exit Function
ERR_TRAP:
If Err.Number = -2147467259 Then
If InStr(1, Err.Description, "Communication link failure", vbTextCompare) > 0 Then
cn.Close
GoTo GetConnection
End If
End If
MsgBox Err.Number & Space(1) & Err.Description, vbCritical, SYSTEM_NAME
Resume Exit_Section
ConnectionError:
End Function
'---・CheckConnection
Public Function CheckConnection()
If cn Is Nothing Then
Exit Function
End If
If cn.State = 0 Then
Exit Function
End If
CheckConnection = True
End Function