ongke0711 > 01-07-17, 04:35 AM
Public Function getsysvar(id As Integer) As String[/font][/font]
On Error GoTo ErrorsHandler
Dim rs As ADODB.Recordset
getCnn '-->Sub gọi kết nối
Set rs = New ADODB.Recordset
rs.Open ("SELECT sysvariables.varvalue FROM sysvariables WHERE id = " & id), cnn, adOpenForwardOnly, adLockReadOnly
If Not (rs.BOF And rs.EOF) Then
getsysvar = Trim(rs!varvalue)
Else
getsysvar = False
End If
rs.Close
Set rs = Nothing
ex:
On Error Resume Next
If rs.State <> adStateClosed Then rs.Close
Set rs = Nothing
Exit Function
ErrorsHandler:
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
[font=Tahoma][font=Tahoma]End Function
Public Function letsysvar(id As Integer, sValue As String) As Boolean[/font][/font]
On Error GoTo ErrorsHandler
Dim rs As ADODB.Recordset
Dim strSQL As String
Set rs = New ADODB.Recordset
rs.Open ("SELECT sysvariables.varvalue FROM sysvariables WHERE id = " & id), cnn, adOpenForwardOnly, adLockReadOnly
If Not (rs.BOF And rs.EOF) Then
strSQL = "UPDATE sysvariables SET varvalue = '" & sValue & "' "
strSQL = strSQL & "WHERE id = " & id
cnn.Execute strSQL
letsysvar = True
Else
letsysvar = False
End If
rs.Close
Set rs = Nothing
ex:
On Error Resume Next
Exit Function
ErrorsHandler:
letsysvar = False
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
[font=Tahoma][font=Tahoma]End Function
Public Enum SysVariables
[/font]
Company = 1
Address = 2
Director = 3
ChiefAccountant = 4
TaxCode = 5
Tel = 6
IncomeTaxRate = 7
PeriodBegin = 8
PeriodEnd = 9
PrevPeriodBegin = 10
PrevPeriodEnd = 11
AppName = 12
AppVersion = 13
AppOwner = 14
AppVersionDate = 15
PathBEFile = 16
PathBKFile = 17
PathExportFile = 18
UserName = 19
UserLevel = 20
DebugMode = 21
[font=Tahoma]End Enum
Function getsysvarDate(var As String) As Date
Dim crit As String
crit = "varname = '" & variable & "'"
getsysvarDate = DLookup("Dateval", "sysvariables", crit)
End Function
Option Explicit
' |=================================================================================|
' | Các giá tri bien duoc luu trong Table thay vi luu bang cac Control trong Form |
' | hoac Form invisible. |
' | Dung hàm de lay (Get) cac bien dung cho Form và dùng duoc trong Query. Dung hàm |
' | gán giá tri (Let) cho các bien trong table. |
' | Có 3 kieu ham: |
' | 1. Dùng tham so 'id' de lay tri cua bien. Cách này gon hon nhung Dev phai nho |
' | 'id' nào là bien nào. |
' | 2. Dùng kieu Enum làm tham so 'id' cho hàm. Giup Dev de su dung hon. Hàm có tham|
' | so la Enum se không dùng duoc trong Query. |
' | 3. Dung tham so là String (lay field 'varname') có the dùng duoc trong Query. |
' | |
' | Nhuoc diem: Phai dùng các hàm Convert nhu CDate, CLong, CBool de chuyen doi |
' | chuoi tra ve tu ham getsysvar(). |
' |=================================================================================|
Global cnn As ADODB.Connection
Public Const conJetDate = "\#mm\/dd\/yyyy\#"
Public Enum SysVariables
Company = 1
Address = 2
Director = 3
ChiefAccountant = 4
TaxCode = 5
Tel = 6
IncomeTaxRate = 7
PeriodBegin = 8
PeriodEnd = 9
PrevPeriodBegin = 10
PrevPeriodEnd = 11
AppName = 12
AppVersion = 13
AppOwner = 14
AppVersionDate = 15
PathBEFile = 16
PathBKFile = 17
PathExportFile = 18
UserName = 19
UserLevel = 20
DebugMode = 21
End Enum
Public Sub getCnn()
On Error GoTo ErrorsHandler
Select Case True
Case cnn Is Nothing
Set cnn = CurrentProject.Connection
cnn.CursorLocation = adUseClient
Case cnn = ""
Set cnn = CurrentProject.Connection
cnn.CursorLocation = adUseClient
Case Else
'...
End Select
ex:
Exit Sub
ErrorsHandler:
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
End Sub
Public Function getsysvar(id As Integer) As String
On Error GoTo ErrorsHandler
Dim rs As ADODB.Recordset
getCnn
Set rs = New ADODB.Recordset
rs.Open ("SELECT sysvariables.varvalue FROM sysvariables WHERE id = " & id), cnn, adOpenForwardOnly, adLockReadOnly
If Not (rs.BOF And rs.EOF) Then
getsysvar = Trim(rs!varvalue)
Else
getsysvar = False
End If
rs.Close
Set rs = Nothing
ex:
On Error Resume Next
If rs.State <> adStateClosed Then rs.Close
Set rs = Nothing
Exit Function
ErrorsHandler:
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
End Function
Public Function letsysvar(id As Integer, sValue As String) As Boolean
On Error GoTo ErrorsHandler
Dim rs As ADODB.Recordset
Dim strSQL As String
Set rs = New ADODB.Recordset
rs.Open ("SELECT sysvariables.varvalue FROM sysvariables WHERE id = " & id), cnn, adOpenForwardOnly, adLockReadOnly
If Not (rs.BOF And rs.EOF) Then
strSQL = "UPDATE sysvariables SET varvalue = '" & sValue & "' "
strSQL = strSQL & "WHERE id = " & id
cnn.Execute strSQL
letsysvar = True
Else
letsysvar = False
End If
rs.Close
Set rs = Nothing
ex:
On Error Resume Next
Exit Function
ErrorsHandler:
letsysvar = False
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
End Function
' |=================================================================================|
' | Thay the tham so 'id' bang Enum 'SysVariables'. Cách này giúp Dev de nho hon. |
' |=================================================================================|
Public Function getsysvar2(id As SysVariables) As Variant
On Error GoTo ErrorsHandler
Dim rs As ADODB.Recordset
Dim strFld As String
getCnn
Set rs = New ADODB.Recordset
rs.Open ("SELECT sysvariables.varvalue FROM sysvariables WHERE id = " & id), cnn, adOpenForwardOnly, adLockReadOnly
If Not (rs.BOF And rs.EOF) Then
getsysvar2 = Trim(rs!varvalue)
Else
getsysvar2 = False
End If
rs.Close
Set rs = Nothing
ex:
On Error Resume Next
If rs.State <> adStateClosed Then rs.Close
Set rs = Nothing
Exit Function
ErrorsHandler:
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
End Function
' |=================================================================================|
' | Dung tham so 'varvalue' giúp Dev de nho hon.Cánh này phai dung cac ham convert |
' | de chuyen String thành các dang tuong ung nhu: CDate(getsysvar2("PeriodBegin")) | |
' |=================================================================================|
Public Function getsysvar3(vName As String) As String
On Error GoTo ErrorsHandler
Dim rs As ADODB.Recordset
Dim strFld As String
getCnn
Set rs = New ADODB.Recordset
rs.Open ("SELECT sysvariables.varvalue FROM sysvariables WHERE varname = '" & vName & "'"), cnn, adOpenForwardOnly, adLockReadOnly
If Not (rs.BOF And rs.EOF) Then
getsysvar3 = Trim(rs!varvalue)
Else
getsysvar3 = False
End If
rs.Close
Set rs = Nothing
ex:
On Error Resume Next
If rs.State <> adStateClosed Then rs.Close
Set rs = Nothing
Exit Function
ErrorsHandler:
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
End Function
Public Function letsysvar2(id As SysVariables, sValue As String) As Boolean
On Error GoTo ErrorsHandler
Dim rs As ADODB.Recordset
Dim strSQL As String
Set rs = New ADODB.Recordset
rs.Open ("SELECT sysvariables.varvalue FROM sysvariables WHERE id = " & id), cnn, adOpenForwardOnly, adLockReadOnly
If Not (rs.BOF And rs.EOF) Then
strSQL = "UPDATE sysvariables SET varvalue = '" & sValue & "' "
strSQL = strSQL & "WHERE id = " & id
cnn.Execute strSQL
letsysvar2 = True
Else
letsysvar2 = False
End If
rs.Close
Set rs = Nothing
ex:
On Error Resume Next
Exit Function
ErrorsHandler:
letsysvar2 = False
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Dien giai: " & Err.Description
Resume ex
End Function