Difference between revisions of "Microsoft Access modSql"

From database24
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Microsoft Access]]
 +
[[Category:VBA]]
 +
[[Category:SQL]]
 
=== Constants ===
 
=== Constants ===
 
<syntaxhighlight lang="vb">
 
<syntaxhighlight lang="vb">
Line 6: Line 9:
  
 
=== executeSql ===
 
=== executeSql ===
 +
SQL statements with references to Access objects would work with DoCmd.RunSQL but the use of references to form fields and other Access objects is not advisable, therefore this option for handling possible errors has deliberately been omitted.
 +
 
<syntaxhighlight lang="vb">
 
<syntaxhighlight lang="vb">
Public Function executeSql(strSql As String, Optional strDatabase As String = "") As Long
+
Public Function executeSql( _
 +
    strSql As String, _
 +
    Optional strDatabase As String = "", _
 +
    Optional blnReturnId As Boolean = True _
 +
    ) As Long
 
     Dim lngResult As Long
 
     Dim lngResult As Long
 
   
 
   
    Const strFormatHms As String = "hh:mm:ss"
 
   
 
 
     Dim db As Database
 
     Dim db As Database
 
     Dim datStart As Date
 
     Dim datStart As Date
Line 17: Line 24:
 
     Dim strTable As String
 
     Dim strTable As String
 
     Dim strAffected As String
 
     Dim strAffected As String
 +
    Dim strResult As String
 
   
 
   
 
     lngResult = 0
 
     lngResult = 0
Line 24: Line 32:
 
         Debug.Print strSql
 
         Debug.Print strSql
 
         datStart = Now
 
         datStart = Now
         Debug.Print "------------"
+
         Debug.Print "+---------------------+"
         Debug.Print "Start     : " & Format(datStart, strFormatHms)
+
         Debug.Print "|    Start : " & Format(datStart, strFormatTime) & " |"
 
     End If
 
     End If
 
      
 
      
Line 44: Line 52:
 
         DoEvents
 
         DoEvents
 
   
 
   
         strAffected = "Records   "
+
         strAffected = "Records "
         Select Case .RecordsAffected
+
         lngResult = .RecordsAffected
         Case 1
+
         If lngResult = 1 Then
             'Last inserted id
+
             If blnReturnId Then
            lngResult = .OpenRecordset("SELECT @@IDENTITY")(0)
+
                'Last inserted id
            If lngResult = 0 Then
+
                lngResult = .OpenRecordset("SELECT @@IDENTITY")(0)
                lngResult = 1
+
                If lngResult = 0 Then
            Else
+
                    lngResult = 1
                strAffected = "Last Id   "
+
                Else
 +
                    strAffected = "Last Id "
 +
                End If
 
             End If
 
             End If
        Case Else
+
         End If
            'Number of affected records
 
            lngResult = .RecordsAffected
 
         End Select
 
 
     End With
 
     End With
 
   
 
   
 
     If isDebug Then
 
     If isDebug Then
         Debug.Print "End       : " & Format(Now, strFormatHms)
+
        strResult = Format(lngResult, "#,##0")
        Debug.Print "            " & "--------"
+
        If Len(strResult) < 8 Then
         Debug.Print "Duration   : " & Format(Now - datStart, strFormatHms)
+
            strResult = strResult & Space(8 - Len(strResult) + 1) & "|"
         Debug.Print strAffected & ": " & Format(lngResult, "#,##0")
+
        End If
         Debug.Print "------------"
+
         Debug.Print "|      End : " & Format(Now, strFormatTime) & " |"
 +
         Debug.Print "| Duration : " & Format(Now - datStart, strFormatTime) & " |"
 +
         Debug.Print strAffected & ": " & strResult
 +
         Debug.Print "+---------------------+"
 
         Debug.Print "----------------------------------------"
 
         Debug.Print "----------------------------------------"
 
         Debug.Print
 
         Debug.Print
Line 76: Line 86:
 
     Select Case Err.Number
 
     Select Case Err.Number
 
   
 
   
     Case 3010:
+
     Case 2008: 'Table is open
         'Table already exists
+
        DoCmd.Close acTable, strTable, acSaveNo
 +
        Resume
 +
          
 +
    Case 3010: 'Table already exists
 
         strErr = Err.Description
 
         strErr = Err.Description
 
         strTable = Mid( _
 
         strTable = Mid( _
Line 89: Line 102:
 
         Resume
 
         Resume
 
   
 
   
     Case 2008:
+
     Case 3022: 'Duplicate key, duplicate values in index
         'Table is open
+
        If isDebug Then
         DoCmd.Close acTable, strTable, acSaveNo
+
            Stop
 +
        End If
 +
        db.Execute strSql
 +
         Resume Next
 +
       
 +
    Case 3051: 'Table is currently in use / locked
 +
         Debug.Print "|  Restart : " & Format(datStart, strFormatTime) & " |"
 +
        DoEvents
 
         Resume
 
         Resume
 
   
 
   
Line 103: Line 123:
 
         If isDebug Then
 
         If isDebug Then
 
             Stop
 
             Stop
 +
            Resume
 
         End If
 
         End If
 
   
 
   

Latest revision as of 14:53, 28 September 2010

Constants

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

executeSql

SQL statements with references to Access objects would work with DoCmd.RunSQL but the use of references to form fields and other Access objects is not advisable, therefore this option for handling possible errors has deliberately been omitted.

Public Function executeSql( _
    strSql As String, _
    Optional strDatabase As String = "", _
    Optional blnReturnId As Boolean = True _
    ) As Long
    Dim lngResult As Long
 
    Dim db As Database
    Dim datStart As Date
    Dim strErr As String
    Dim strTable As String
    Dim strAffected As String
    Dim strResult As String
 
    lngResult = 0
 
    If isDebug Then
        Debug.Print "----------------------------------------"
        Debug.Print strSql
        datStart = Now
        Debug.Print "+---------------------+"
        Debug.Print "|    Start : " & Format(datStart, strFormatTime) & " |"
    End If
    
    If strDatabase = "" Or strDatabase = CurrentProject.FullName 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 "
        lngResult = .RecordsAffected
        If lngResult = 1 Then
            If blnReturnId Then
                'Last inserted id
                lngResult = .OpenRecordset("SELECT @@IDENTITY")(0)
                If lngResult = 0 Then
                    lngResult = 1
                Else
                    strAffected = "|  Last Id "
                End If
            End If
        End If
    End With
 
    If isDebug Then
        strResult = Format(lngResult, "#,##0")
        If Len(strResult) < 8 Then
            strResult = strResult & Space(8 - Len(strResult) + 1) & "|"
        End If
        Debug.Print "|      End : " & Format(Now, strFormatTime) & " |"
        Debug.Print "| Duration : " & Format(Now - datStart, strFormatTime) & " |"
        Debug.Print strAffected & ": " & strResult
        Debug.Print "+---------------------+"
        Debug.Print "----------------------------------------"
        Debug.Print
    End If
 
    executeSql = lngResult
    Exit Function
 
handleExecuteError:
    Select Case Err.Number
 
    Case 2008: 'Table is open
        DoCmd.Close acTable, strTable, acSaveNo
        Resume
        
    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 3022: 'Duplicate key, duplicate values in index
        If isDebug Then
            Stop
        End If
        db.Execute strSql
        Resume Next
        
    Case 3051: 'Table is currently in use / locked
        Debug.Print "|  Restart : " & Format(datStart, strFormatTime) & " |"
        DoEvents
        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
            Resume
        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