Difference between revisions of "Microsoft Access modSql"

From database24
Jump to navigation Jump to search
Line 7: Line 7:
 
=== executeSql ===
 
=== executeSql ===
 
<syntaxhighlight lang="vb">
 
<syntaxhighlight lang="vb">
Public Function executeSql(strSql As String) As Long
+
Public Function executeSql(strSql As String, Optional strDatabase As String = "") As Long
 
     Dim lngResult As Long
 
     Dim lngResult As Long
   
+
 
     Const strFormatHms As String = "hh:mm:ss"
 
     Const strFormatHms As String = "hh:mm:ss"
 
      
 
      
 +
    Dim db As Database
 
     Dim datStart As Date
 
     Dim datStart As Date
 
     Dim strErr As String
 
     Dim strErr As String
 
     Dim strTable As String
 
     Dim strTable As String
 
     Dim strAffected As String
 
     Dim strAffected As String
   
+
 
     lngResult = 0
 
     lngResult = 0
   
+
 
     If isDebug Then
 
     If isDebug Then
 
         Debug.Print "----------------------------------------"
 
         Debug.Print "----------------------------------------"
 
         Debug.Print strSql
 
         Debug.Print strSql
        Debug.Print
 
 
         datStart = Now
 
         datStart = Now
 +
        Debug.Print "------------"
 
         Debug.Print "Start      : " & Format(datStart, strFormatHms)
 
         Debug.Print "Start      : " & Format(datStart, strFormatHms)
 
     End If
 
     End If
 
+
   
     With CurrentDb
+
     If strDatabase = "" Then
         'DoCmd.SetWarnings False
+
        Set db = CurrentDb
 +
    Else
 +
         Set db = OpenDatabase(strDatabase)
 +
    End If
 +
   
 +
    With db
 
         On Error GoTo handleExecuteError
 
         On Error GoTo handleExecuteError
 
         If isDebug Then
 
         If isDebug Then
Line 36: Line 42:
 
         End If
 
         End If
 
         On Error GoTo 0
 
         On Error GoTo 0
        'DoCmd.SetWarnings True
 
 
         DoEvents
 
         DoEvents
       
+
 
         strAffected = "Records    "
 
         strAffected = "Records    "
 
         Select Case .RecordsAffected
 
         Select Case .RecordsAffected
Line 54: Line 59:
 
         End Select
 
         End Select
 
     End With
 
     End With
   
+
 
     If isDebug Then
 
     If isDebug Then
 
         Debug.Print "End        : " & Format(Now, strFormatHms)
 
         Debug.Print "End        : " & Format(Now, strFormatHms)
Line 60: Line 65:
 
         Debug.Print "Duration  : " & Format(Now - datStart, strFormatHms)
 
         Debug.Print "Duration  : " & Format(Now - datStart, strFormatHms)
 
         Debug.Print strAffected & ": " & Format(lngResult, "#,##0")
 
         Debug.Print strAffected & ": " & Format(lngResult, "#,##0")
 +
        Debug.Print "------------"
 
         Debug.Print "----------------------------------------"
 
         Debug.Print "----------------------------------------"
 
         Debug.Print
 
         Debug.Print
 
     End If
 
     End If
 
+
 
     executeSql = lngResult
 
     executeSql = lngResult
 
     Exit Function
 
     Exit Function
   
+
 
handleExecuteError:
 
handleExecuteError:
    'On Error GoTo handleExecuteSqlErrorFinal
 
    'DoCmd.RunSQL strSql
 
    'On Error GoTo 0
 
    'resume next
 
    'handleExecuteSqlErrorFinal:
 
 
 
     Select Case Err.Number
 
     Select Case Err.Number
   
+
 
     Case 3010:
 
     Case 3010:
 
         'Table already exists
 
         'Table already exists
Line 88: Line 88:
 
         DoCmd.DeleteObject acTable, strTable
 
         DoCmd.DeleteObject acTable, strTable
 
         Resume
 
         Resume
   
+
 
     Case 2008:
 
     Case 2008:
 
         'Table is open
 
         'Table is open
 
         DoCmd.Close acTable, strTable, acSaveNo
 
         DoCmd.Close acTable, strTable, acSaveNo
 
         Resume
 
         Resume
   
+
    Case 3022:
 
        'LockOut
 
        Debug.Print Now & " - trying again ..."
 
        DoEvents
 
        Resume
 
   
 
 
     Case Else
 
     Case Else
 
         strErr = Err.Number & " : " & Err.Description
 
         strErr = Err.Number & " : " & Err.Description
Line 110: Line 104:
 
             Stop
 
             Stop
 
         End If
 
         End If
   
+
 
     End Select
 
     End Select
 
End Function
 
End Function

Revision as of 12:09, 15 July 2010

Constants

Public Const strFormatSqlDate As String = "yyyy\-mm\-dd"
Public Const strFormatSqlDateCriterion As String = "\#yyyy\-mm\-dd\#"

executeSql

Public Function executeSql(strSql As String, Optional strDatabase As String = "") As Long
    Dim lngResult As Long
 
    Const strFormatHms As String = "hh:mm:ss"
    
    Dim db As Database
    Dim datStart As Date
    Dim strErr As String
    Dim strTable As String
    Dim strAffected As String
 
    lngResult = 0
 
    If isDebug Then
        Debug.Print "----------------------------------------"
        Debug.Print strSql
        datStart = Now
        Debug.Print "------------"
        Debug.Print "Start      : " & Format(datStart, strFormatHms)
    End If
    
    If strDatabase = "" Then
        Set db = CurrentDb
    Else
        Set db = OpenDatabase(strDatabase)
    End If
    
    With db
        On Error GoTo handleExecuteError
        If isDebug Then
            .Execute strSql, dbFailOnError
        Else
            .Execute strSql
        End If
        On Error GoTo 0
        DoEvents
 
        strAffected = "Records    "
        Select Case .RecordsAffected
        Case 1
            'Last inserted id
            lngResult = .OpenRecordset("SELECT @@IDENTITY")(0)
            If lngResult = 0 Then
                lngResult = 1
            Else
                strAffected = "Last Id    "
            End If
        Case Else
            'Number of affected records
            lngResult = .RecordsAffected
        End Select
    End With
 
    If isDebug Then
        Debug.Print "End        : " & Format(Now, strFormatHms)
        Debug.Print "             " & "--------"
        Debug.Print "Duration   : " & Format(Now - datStart, strFormatHms)
        Debug.Print strAffected & ": " & Format(lngResult, "#,##0")
        Debug.Print "------------"
        Debug.Print "----------------------------------------"
        Debug.Print
    End If
 
    executeSql = lngResult
    Exit Function
 
handleExecuteError:
    Select Case Err.Number
 
    Case 3010:
        'Table already exists
        strErr = Err.Description
        strTable = Mid( _
            Err.Description, _
            InStr(1, strErr, "'", vbTextCompare) + 1, _
            InStrRev(strErr, "'", -1, vbTextCompare) - _
                InStr(1, strErr, "'", vbTextCompare) - 1 _
            )
        DoCmd.Close acTable, strTable, acSaveNo
        DoCmd.DeleteObject acTable, strTable
        Resume
 
    Case 2008:
        'Table is open
        DoCmd.Close acTable, strTable, acSaveNo
        Resume
 
    Case Else
        strErr = Err.Number & " : " & Err.Description
        Debug.Print "########################################"
        Debug.Print "# ERR " & Err.Number & " : " & Err.Description
        Debug.Print "# ABORT"
        Debug.Print "########################################"
        Debug.Print
        If isDebug Then
            Stop
        End If
 
    End Select
End Function

showSqlResult

Public Sub showSqlResult(strSql As String, Optional strQuery As String = "")
    Dim dbs As Database
    Dim qdf As QueryDef
    
    If strQuery = "" Then
        strQuery = "SQL Result"
    End If
    If existsQuery(strQuery) Then
        DoCmd.DeleteObject acQuery, strQuery
    End If
    Set dbs = CurrentDb
    Set qdf = dbs.CreateQueryDef(Name:=strQuery, SQLText:=strSql)
    DoCmd.OpenQuery strQuery
    Do While CurrentData.AllQueries(strQuery).IsLoaded
        DoEvents
    Loop
    DoCmd.DeleteObject acQuery, strQuery
End Sub

getSqlAmount

Public Function getSqlAmount(cur As Currency) As String
    Dim strResult As String
    
    strResult = CStr(cur)
    strResult = Replace( _
        Expression:=strResult, _
        Find:=",", _
        Replace:=".", _
        Compare:=vbTextCompare _
        )
    
    getSqlAmount = strResult
End Function

getSqlDate

Public Function getSqlDate(dat As Date) As String
    Dim strResult As String
    
    strResult = Format(dat, strFormatSqlDate)
    
    getSqlDate = strResult
End Function

getSqlDateCriterion

Public Function getSqlDateCriterion(dat As Date) As String
    Dim strResult As String
    
    strResult = Format(dat, strFormatSqlDateCriterion)
    
    getSqlDateCriterion = strResult
End Function