home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / TreeList212239812008.psc / TreeList / clsADO.cls next >
Text File  |  2008-07-30  |  29KB  |  859 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsADO"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. Public adoCn As ADODB.Connection
  16. Private m_DatabaseName As String
  17. Private m_CommandTimeOut As Long
  18. Private m_ConnectionTimeout As Long
  19. Private m_CursorLocation As Long
  20. Private mvarKeepLog As Boolean
  21. Private mvarLogFile As String
  22. Private m_Table_FieldPrimaryKeyName As String
  23. Private m_QueryString As String
  24. Private intLog As Integer
  25. Public Property Get QueryString() As String
  26.     On Error Resume Next
  27.     QueryString = m_QueryString
  28.     Err.Clear
  29. End Property
  30. Public Property Let QueryString(ByVal Value As String)
  31.     On Error Resume Next
  32.     m_QueryString = Value
  33.     Err.Clear
  34. End Property
  35. Public Property Get Table_FieldPrimaryKeyName() As String
  36.     On Error Resume Next
  37.     Table_FieldPrimaryKeyName = m_Table_FieldPrimaryKeyName
  38.     Err.Clear
  39. End Property
  40. Public Property Get CursorLocation() As CursorLocationEnum
  41.     On Error Resume Next
  42.     CursorLocation = m_CursorLocation
  43.     Err.Clear
  44. End Property
  45. Public Property Let CursorLocation(ByVal Value As CursorLocationEnum)
  46.     On Error Resume Next
  47.     m_CursorLocation = Value
  48.     Err.Clear
  49. End Property
  50. Public Property Get ConnectionTimeout() As Long
  51.     On Error Resume Next
  52.     ConnectionTimeout = m_ConnectionTimeout
  53.     Err.Clear
  54. End Property
  55. Public Property Let ConnectionTimeout(ByVal Value As Long)
  56.     On Error Resume Next
  57.     m_ConnectionTimeout = Value
  58.     Err.Clear
  59. End Property
  60. Public Property Get CommandTimeOut() As Long
  61.     On Error Resume Next
  62.     CommandTimeOut = m_CommandTimeOut
  63.     Err.Clear
  64. End Property
  65. Public Property Let CommandTimeOut(ByVal Value As Long)
  66.     On Error Resume Next
  67.     m_CommandTimeOut = Value
  68.     Err.Clear
  69. End Property
  70. Public Property Get DatabaseName() As String
  71.     On Error Resume Next
  72.     DatabaseName = m_DatabaseName
  73.     Err.Clear
  74. End Property
  75. Public Property Let DatabaseName(ByVal Value As String)
  76.     On Error Resume Next
  77.     m_DatabaseName = Value
  78.     Err.Clear
  79. End Property
  80. Public Function OpenConnection(Optional ByVal DbName As String) As ADODB.ObjectStateEnum
  81.     On Error Resume Next
  82.     If Len(DbName) > 0 Then DatabaseName = DbName
  83.     Set adoCn = New ADODB.Connection
  84.     adoCn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName
  85.     adoCn.CursorLocation = Me.CursorLocation
  86.     adoCn.CommandTimeOut = Me.CommandTimeOut
  87.     adoCn.ConnectionTimeout = Me.ConnectionTimeout
  88.     adoCn.Mode = adModeReadWrite
  89.     adoCn.Open
  90.     OpenConnection = adoCn.State
  91.     If Me.KeepLog = True Then
  92.         intLog = FreeFile
  93.         Open Me.LogFile For Append As #intLog
  94.             Print #intLog, Me.DateTimeDone & ", Open Connection To Server " & DatabaseName
  95.         End If
  96.         Err.Clear
  97. End Function
  98. Public Function OpenRs(RsSource As String, Optional RsCursorType As CursorTypeEnum = adOpenStatic, Optional RsLockType As LockTypeEnum = adLockOptimistic, Optional RsCommandType As CommandTypeEnum = adCmdTable) As ADODB.Recordset
  99.     On Error Resume Next
  100.     Dim IsQuery As Boolean
  101.     If Left$(LCase$(RsSource), 6) = "select" Then
  102.         IsQuery = True
  103.     Else
  104.         RsSource = Replace$(RsSource, "[", "")
  105.         RsSource = Replace$(RsSource, "]", "")
  106.         RsSource = "[" & RsSource & "]"
  107.         IsQuery = False
  108.     End If
  109.     Set OpenRs = New ADODB.Recordset
  110.     OpenRs.CursorLocation = adoCn.CursorLocation
  111.     OpenRs.Open RsSource, adoCn, RsCursorType, RsLockType, RsCommandType
  112.     If IsQuery = True Then
  113.         OpenRs.MoveLast
  114.         OpenRs.MoveFirst
  115.     End If
  116.     Err.Clear
  117. End Function
  118. Public Function Table_NameFromSelect(ByVal strQuery As String) As String
  119.     On Error Resume Next
  120.     Dim fromPos As Long
  121.     Dim lenStr As Long
  122.     Dim lenCnt As Long
  123.     Dim restStr As String
  124.     Dim delimPos As Long
  125.     Dim TbName As String
  126.     fromPos = InStr(1, strQuery, " from ", vbTextCompare)
  127.     If fromPos > 0 Then
  128.         restStr = Trim$(Mid$(strQuery, fromPos + 5))
  129.         ' check using the table Quote
  130.         delimPos = InStr(1, restStr, "[", vbTextCompare)
  131.         lenStr = Len(restStr)
  132.         TbName = ""
  133.         If delimPos > 0 Then
  134.             For lenCnt = 2 To lenStr
  135.                 If Mid$(restStr, lenCnt, 1) = "]" Then
  136.                     Exit For
  137.                 Else
  138.                     TbName = TbName & Mid$(restStr, lenCnt, 1)
  139.                 End If
  140.                 Err.Clear
  141.             Next
  142.         Else
  143.             For lenCnt = 1 To lenStr
  144.                 If Mid$(restStr, lenCnt, 1) = " " Then
  145.                     Exit For
  146.                 Else
  147.                     TbName = TbName & Mid$(restStr, lenCnt, 1)
  148.                 End If
  149.                 Err.Clear
  150.             Next
  151.         End If
  152.         Table_NameFromSelect = Replace$(TbName, ";", "", , , vbTextCompare)
  153.     Else
  154.         Table_NameFromSelect = strQuery
  155.     End If
  156.     Err.Clear
  157. End Function
  158. Public Function RemNull(rsField As Variant) As String
  159.     On Error Resume Next
  160.     RemNull = rsField.Value & ""
  161.     Err.Clear
  162. End Function
  163. Public Function OpenRsFieldNames(Rs As ADODB.Recordset, Optional QuoteEach As Boolean = False) As String
  164.     On Error Resume Next
  165.     Dim fldCnt As Long
  166.     Dim fldTot As Long
  167.     Dim fldHed As String
  168.     Dim FldName As String
  169.     fldHed = ""
  170.     fldTot = Rs.Fields.Count - 1
  171.     For fldCnt = 0 To fldTot
  172.         FldName = Rs.Fields(fldCnt).Name
  173.         If QuoteEach = True Then
  174.             FldName = Chr$(34) & FldName & Chr$(34)
  175.         End If
  176.         If fldCnt = fldTot Then
  177.             fldHed = fldHed & FldName
  178.         Else
  179.             fldHed = fldHed & FldName & ","
  180.         End If
  181.         Err.Clear
  182.     Next
  183.     OpenRsFieldNames = fldHed
  184.     Err.Clear
  185. End Function
  186. Public Sub CloseConnection()
  187.     On Error Resume Next
  188.     adoCn.Close
  189.     Err.Clear
  190. End Sub
  191. Public Property Get IsConnected() As Boolean
  192.     On Error Resume Next
  193.     Select Case adoCn.State
  194.     Case 0
  195.         IsConnected = False
  196.     Case Else
  197.         IsConnected = True
  198.     End Select
  199.     Err.Clear
  200. End Property
  201. Public Property Get State() As String
  202.     On Error Resume Next
  203.     Select Case adoCn.State
  204.     Case adStateClosed
  205.         State = "Closed"
  206.     Case adStateOpen
  207.         State = "Open"
  208.     Case adStateExecuting
  209.         State = "Executing"
  210.     Case adStateFetching
  211.         State = "Fetching"
  212.     Case adStateConnecting
  213.         State = "Connecting"
  214.     End Select
  215.     Err.Clear
  216. End Property
  217. Public Property Let KeepLog(ByVal vData As Boolean)
  218.     On Error Resume Next
  219.     ' if true a log file is kept by each execute statement
  220.     mvarKeepLog = vData
  221.     Err.Clear
  222. End Property
  223. Public Property Get KeepLog() As Boolean
  224.     On Error Resume Next
  225.     KeepLog = mvarKeepLog
  226.     Err.Clear
  227. End Property
  228. Public Property Let LogFile(ByVal vData As String)
  229.     On Error Resume Next
  230.     ' establish the name of the log file
  231.     mvarLogFile = vData
  232.     Err.Clear
  233. End Property
  234. Public Property Get LogFile() As String
  235.     On Error Resume Next
  236.     LogFile = mvarLogFile
  237.     Err.Clear
  238. End Property
  239. Private Sub Class_Initialize()
  240.     On Error Resume Next
  241.     Me.CommandTimeOut = 0
  242.     Me.ConnectionTimeout = 15
  243.     Me.CursorLocation = adUseClient
  244.     Err.Clear
  245. End Sub
  246. Public Function Table_FieldNames(ByVal TbName As String) As String
  247.     On Error Resume Next
  248.     ' returns the names of the fields of the table
  249.     Dim myRS As ADODB.Recordset
  250.     Set myRS = New ADODB.Recordset
  251.     Dim strResults As String
  252.     If LCase$(Left$(TbName, 6)) = "select" Then
  253.         TbName = Table_NameFromSelect(TbName)
  254.     End If
  255.     strResults = ""
  256.     Set myRS = adoCn.OpenSchema(ADODB.adSchemaColumns, Array(Empty, Empty, TbName))
  257.     Do Until myRS.EOF
  258.         strResults = strResults & myRS.Fields("column_name").Value & ","
  259.         myRS.MoveNext
  260.         Err.Clear
  261.     Loop
  262.     Table_FieldNames = RemDelim(strResults, ",")
  263.     myRS.Close
  264.     Err.Clear
  265. End Function
  266. Public Function Table_FieldIndexes(ByVal TbName As String) As String
  267.     On Error Resume Next
  268.     ' returns the names of the indexes of the table
  269.     Dim myRS As ADODB.Recordset
  270.     Set myRS = New ADODB.Recordset
  271.     Dim strResults As String
  272.     If LCase$(Left$(TbName, 6)) = "select" Then
  273.         TbName = Table_NameFromSelect(TbName)
  274.     End If
  275.     strResults = ""
  276.     Set myRS = adoCn.OpenSchema(ADODB.adSchemaIndexes, Array(Empty, Empty, Empty, Empty, TbName))
  277.     Do Until myRS.EOF
  278.         strResults = strResults & myRS.Fields("column_name").Value & ","
  279.         myRS.MoveNext
  280.         Err.Clear
  281.     Loop
  282.     Table_FieldIndexes = MvRemoveDuplicates(strResults, ",")
  283.     myRS.Close
  284.     Err.Clear
  285. End Function
  286. Public Function Table_FieldSizes(ByVal TbName As String) As String
  287.     On Error Resume Next
  288.     ' returns the sizes of the fields of the table
  289.     Dim myRS As ADODB.Recordset
  290.     Set myRS = New ADODB.Recordset
  291.     Dim strResults As String
  292.     If LCase$(Left$(TbName, 6)) = "select" Then
  293.         TbName = Table_NameFromSelect(TbName)
  294.     End If
  295.     strResults = ""
  296.     Set myRS = adoCn.OpenSchema(ADODB.adSchemaColumns, Array(Empty, Empty, TbName))
  297.     Do Until myRS.EOF
  298.         strResults = strResults & myRS.Fields("character_maximum_length").Value & ","
  299.         myRS.MoveNext
  300.         Err.Clear
  301.     Loop
  302.     Table_FieldSizes = RemDelim(strResults, ",")
  303.     myRS.Close
  304.     Err.Clear
  305. End Function
  306. Private Function RemDelim(ByVal Dataobj As String, ByVal Delimiter As String) As String
  307.     On Error Resume Next
  308.     Dim intDataSize As Long
  309.     Dim intDelimSize As Long
  310.     Dim strLast As String
  311.     intDataSize = Len(Dataobj)
  312.     intDelimSize = Len(Delimiter)
  313.     strLast = Right$(Dataobj, intDelimSize)
  314.     Select Case strLast
  315.     Case Delimiter
  316.         RemDelim = Left$(Dataobj, (intDataSize - intDelimSize))
  317.     Case Else
  318.         RemDelim = Dataobj
  319.     End Select
  320.     Err.Clear
  321. End Function
  322. Public Function MvRemoveDuplicates(ByVal StrMvString As String, Optional ByVal Delim As String = ";") As String
  323.     On Error Resume Next
  324.     ' returns a string from a string after removing all duplicated sub strings of a delimited string
  325.     Dim spData() As String
  326.     Dim spTot As Long
  327.     Dim spCnt As Long
  328.     Dim xCol As Collection
  329.     Set xCol = New Collection
  330.     spData = Split(StrMvString, Delim)
  331.     spTot = UBound(spData)
  332.     For spCnt = 0 To spTot
  333.         spData(spCnt) = Trim$(spData(spCnt))
  334.         If Len(spData(spCnt)) > 0 Then
  335.             xCol.Add spData(spCnt), spData(spCnt)
  336.         End If
  337.         Err.Clear
  338.     Next
  339.     MvRemoveDuplicates = MvFromCollection(xCol, Delim)
  340.     Err.Clear
  341. End Function
  342. Public Function MvFromCollection(objCollection As Collection, ByVal Delimiter As String) As String
  343.     On Error Resume Next
  344.     ' returns a delimited string based on a collection
  345.     Dim xTot As Long
  346.     Dim xCnt As Long
  347.     Dim sRet As String
  348.     sRet = ""
  349.     xTot = objCollection.Count
  350.     For xCnt = 1 To xTot
  351.         If xCnt = xTot Then
  352.             sRet = sRet & objCollection.Item(xCnt)
  353.         Else
  354.             sRet = sRet & objCollection.Item(xCnt) & Delimiter
  355.         End If
  356.         Err.Clear
  357.     Next
  358.     MvFromCollection = sRet
  359.     Err.Clear
  360. End Function
  361. Public Function Table_FieldTypes(ByVal TbName As String) As String
  362.     On Error Resume Next
  363.     ' returns the field types of the fields of the table
  364.     Dim myRS As ADODB.Recordset
  365.     Set myRS = New ADODB.Recordset
  366.     Dim strResults As String
  367.     If LCase$(Left$(TbName, 6)) = "select" Then
  368.         TbName = Table_NameFromSelect(TbName)
  369.     End If
  370.     strResults = ""
  371.     Set myRS = adoCn.OpenSchema(ADODB.adSchemaColumns, Array(Empty, Empty, TbName))
  372.     Do Until myRS.EOF
  373.         strResults = strResults & myRS.Fields("data_type").Value & ","
  374.         myRS.MoveNext
  375.         Err.Clear
  376.     Loop
  377.     Table_FieldTypes = RemDelim(strResults, ",")
  378.     myRS.Close
  379.     Err.Clear
  380. End Function
  381. Public Function Table_FieldPrimaryKey(ByVal TbName As String) As String
  382.     On Error Resume Next
  383.     ' returns the names of the fields of the table
  384.     Dim myRS As ADODB.Recordset
  385.     Set myRS = New ADODB.Recordset
  386.     Dim strResults As String
  387.     Dim strNames As String
  388.     If LCase$(Left$(TbName, 6)) = "select" Then
  389.         TbName = Table_NameFromSelect(TbName)
  390.     End If
  391.     strResults = ""
  392.     strNames = ""
  393.     Set myRS = adoCn.OpenSchema(ADODB.adSchemaPrimaryKeys, Array(Empty, Empty, TbName))
  394.     Do Until myRS.EOF
  395.         strResults = strResults & RemNull(myRS!column_name) & ","
  396.         strNames = strNames & RemNull(myRS!pk_name) & ","
  397.         myRS.MoveNext
  398.         Err.Clear
  399.     Loop
  400.     strResults = MvRemoveDuplicates(strResults, ",")
  401.     strNames = MvRemoveDuplicates(strNames, ",")
  402.     m_Table_FieldPrimaryKeyName = strNames
  403.     Table_FieldPrimaryKey = strResults
  404.     myRS.Close
  405.     Err.Clear
  406. End Function
  407. Public Function Table_FieldAutoIncrement(ByVal TbName As String) As String
  408.     On Error Resume Next
  409.     ' returns the names of the fields of the table that is autoincrement
  410.     Dim myRS As ADODB.Recordset
  411.     Set myRS = New ADODB.Recordset
  412.     Dim strResults As String
  413.     Dim strDefault As String
  414.     Dim strColumnName As String
  415.     If LCase$(Left$(TbName, 6)) = "select" Then
  416.         TbName = Table_NameFromSelect(TbName)
  417.     End If
  418.     Table_FieldAutoIncrement = ""
  419.     strResults = ""
  420.     Set myRS = adoCn.OpenSchema(ADODB.adSchemaColumns, Array(Empty, Empty, TbName))
  421.     Do Until myRS.EOF
  422.         strDefault = LCase$(RemNull(myRS!column_default))
  423.         strColumnName = RemNull(myRS!column_name)
  424.         If strDefault = "('newsequentialid()')" Then
  425.             strResults = strResults & strColumnName & ","
  426.         End If
  427.         myRS.MoveNext
  428.         Err.Clear
  429.     Loop
  430.     myRS.Close
  431.     Table_FieldAutoIncrement = RemDelim(strResults, ",")
  432.     Err.Clear
  433. End Function
  434. Public Function Table_FieldIndexExists(ByVal TbName As String, ByVal strIdxName As String) As Boolean
  435.     On Error Resume Next
  436.     ' returns true / false if an index exists in a table
  437.     Dim colPos As Long
  438.     Dim colNames As String
  439.     colNames = Table_FieldIndexes(TbName)
  440.     colPos = MvSearch(colNames, strIdxName, ",")
  441.     If colPos = 0 Then
  442.         Table_FieldIndexExists = False
  443.     Else
  444.         Table_FieldIndexExists = True
  445.     End If
  446.     Err.Clear
  447. End Function
  448. Private Function MvSearch(ByVal StrMv As String, ByVal StrSearch As String, Delimiter As String) As Long
  449.     On Error Resume Next
  450.     Dim xValues() As String
  451.     Dim xPos As Long
  452.     xValues = Split(StrMv, Delimiter)
  453.     xPos = ArraySearch(xValues, StrSearch)
  454.     MvSearch = IIf((xPos = -1), 0, xPos + 1)
  455.     Err.Clear
  456. End Function
  457. Private Function ArraySearch(varArray() As String, ByVal StrSearch As String) As Long
  458.     On Error Resume Next
  459.     Dim ArrayTot As Long
  460.     Dim arrayCnt As Long
  461.     Dim strCur As String
  462.     Dim arrayLow As Long
  463.     ArrayTot = UBound(varArray)
  464.     arrayLow = LBound(varArray)
  465.     StrSearch = LCase$(Trim$(StrSearch))
  466.     ArraySearch = -1
  467.     For arrayCnt = arrayLow To ArrayTot
  468.         strCur = LCase$(varArray(arrayCnt))
  469.         Select Case strCur
  470.         Case StrSearch
  471.             ArraySearch = arrayCnt
  472.             Exit For
  473.         End Select
  474.         Err.Clear
  475.     Next
  476.     Err.Clear
  477. End Function
  478. Public Function Table_FieldExists(ByVal TbName As String, ByVal strColumnName As String) As Boolean
  479.     On Error Resume Next
  480.     ' returns true or false if a field exists in a table
  481.     Dim colPos As Long
  482.     Dim colNames As String
  483.     colNames = Table_FieldNames(TbName)
  484.     colPos = MvSearch(colNames, strColumnName, ",")
  485.     If colPos = 0 Then
  486.         Table_FieldExists = False
  487.     Else
  488.         Table_FieldExists = True
  489.     End If
  490.     Err.Clear
  491. End Function
  492. Public Function Execute(ByVal mvarQuery As String) As ADODB.Recordset
  493.     On Error GoTo ErrorHandler
  494.     ' run an execute statement against the connection and
  495.     ' log the entry of the log is being kept
  496.     Set Execute = New ADODB.Recordset
  497.     QueryString = mvarQuery
  498.     If Me.KeepLog = True Then
  499.         Print #intLog, Me.DateTimeDone & ", " & QueryString
  500.     End If
  501.     Set Execute = adoCn.Execute(mvarQuery)
  502.     Err.Clear
  503.     Exit Function
  504. ErrorHandler:
  505.     'Call Me.MyMsgBox
  506.     Err.Clear
  507. End Function
  508. Public Sub Table_Create(ByVal dbTable As String, ByVal FldName As String, Optional ByVal fldType As String = "", Optional ByVal FldSize As String = "", Optional ByVal Fldidx As String = "", Optional ByVal FldAutoIncrement As String = "", Optional ByVal PrimaryFld As String = "")
  509.     On Error Resume Next
  510.     ' create a table
  511.     Dim spFlds() As String
  512.     Dim spType() As String
  513.     Dim spSize() As String
  514.     Dim spIndx() As String
  515.     Dim spAuto() As String
  516.     Dim totFld As Integer
  517.     Dim totIdx As Integer
  518.     Dim newCnt As Integer
  519.     Dim tbQuery As String
  520.     Dim typeStr As String
  521.     Dim idxStr As String
  522.     Fldidx = MvRemoveDuplicates(Fldidx, "," & PrimaryFld)
  523.     FldAutoIncrement = MvRemoveDuplicates(FldAutoIncrement, ",")
  524.     Call StrParse(spFlds, FldName, ",")
  525.     Call StrParse(spType, fldType, ",")
  526.     Call StrParse(spSize, FldSize, ",")
  527.     Call StrParse(spIndx, Fldidx, ",")
  528.     Call StrParse(spAuto, FldAutoIncrement, ",")
  529.     ArrayTrimItems spFlds
  530.     ArrayTrimItems spType
  531.     ArrayTrimItems spSize
  532.     ArrayTrimItems spIndx
  533.     ArrayTrimItems spAuto
  534.     totFld = UBound(spFlds)
  535.     totIdx = UBound(spIndx)
  536.     ReDim Preserve spType(totFld)
  537.     ReDim Preserve spSize(totFld)
  538.     dbTable = Iconv(dbTable, "t")
  539.     tbQuery = "CREATE TABLE [" & dbTable & "] ("
  540.     idxStr = ""
  541.     For newCnt = 1 To totFld
  542.         spType(newCnt) = Trim$(spType(newCnt))
  543.         spFlds(newCnt) = Trim$(spFlds(newCnt))
  544.         spSize(newCnt) = Trim$(spSize(newCnt))
  545.         If Len(spType(newCnt)) = 0 Then
  546.             spType(newCnt) = "Text"
  547.         End If
  548.         If Len(spSize(newCnt)) = 0 Then
  549.             spSize(newCnt) = "255"
  550.         End If
  551.         Select Case LCase$(spType(newCnt))
  552.         Case "image", "picture", "general": typeStr = "varbinary(max)"
  553.         Case "nvarchar": typeStr = "nvarchar(" & spSize(newCnt) & ")"
  554.         Case "boolean", "logical":          typeStr = "bit"
  555.         Case "varbinary": typeStr = "varbinary(" & spSize(newCnt) & ")"
  556.         Case "char": typeStr = "char(" & spSize(newCnt) & ")"
  557.         Case "varchar": typeStr = "varchar(" & spSize(newCnt) & ")"
  558.         Case "currency", "smallmoney":      typeStr = "money"
  559.         Case "date":        typeStr = "datetime"
  560.         Case "time":        typeStr = "datetime"
  561.         Case "timestamp": typeStr = "datetime"
  562.         Case "smalldatetime":        typeStr = "datetime"
  563.         Case "double":         typeStr = "float"
  564.         Case "real":        typeStr = "real"
  565.         Case "integer", "int":        typeStr = "smallint"
  566.         Case "long": typeStr = "int"
  567.         Case "memo", "longtext", "long text":         typeStr = "nvarchar(max)"
  568.         Case "single":             typeStr = "tinyint"
  569.         Case "text":          typeStr = "varchar(" & spSize(newCnt) & ")"
  570.         Case "ntext": typeStr = "nvarchar(max)"
  571.         Case "xml": typeStr = "nvarchar(max)"
  572.         Case ""
  573.             typeStr = "varchar(255)"
  574.         Case Else
  575.             typeStr = LCase$(spType(newCnt))
  576.         End Select
  577.         If MvSearch(FldAutoIncrement, spFlds(newCnt), ",") > 0 Then
  578.             typeStr = "uniqueidentifier DEFAULT NEWSEQUENTIALID()"
  579.         End If
  580.         If MvSearch(PrimaryFld, spFlds(newCnt), ",") > 0 Then
  581.             typeStr = typeStr & " NOT NULL"
  582.         End If
  583.         tbQuery = tbQuery & "[" & spFlds(newCnt) & "] " & typeStr & ","
  584.         Err.Clear
  585.     Next
  586.     tbQuery = RemDelim(tbQuery, ",") & ")"
  587.     Me.Table_Delete dbTable
  588.     Call Me.Execute(tbQuery)
  589.     For newCnt = 1 To totIdx
  590.         Fldidx = "create index [" & spIndx(newCnt) & "] on [" & dbTable & "] (" & spIndx(newCnt) & ");"
  591.         Call Me.Execute(Fldidx)
  592.         Err.Clear
  593.     Next
  594.     ' create the primary key
  595.     If Len(PrimaryFld) > 0 Then
  596.         PrimaryFld = MvField(PrimaryFld, 1, ",")
  597.         QueryString = "ALTER TABLE [" & dbTable & "] ADD CONSTRAINT [PK_" & dbTable & "_" & PrimaryFld & "] PRIMARY KEY (" & PrimaryFld & ");"
  598.         Call Me.Execute(QueryString)
  599.     End If
  600.     Err.Clear
  601. End Sub
  602. Public Function Table_Delete(ByVal TbName As String) As Boolean
  603.     On Error Resume Next
  604.     ' delete specified table
  605.     If Len(TbName) = 0 Then
  606.         Table_Delete = False
  607.     Else
  608.         If Table_Exists(TbName) = True Then Me.Execute "DROP TABLE [" & TbName & "];"
  609.         If Table_Exists(TbName) = True Then
  610.             Table_Delete = False
  611.         Else
  612.             Table_Delete = True
  613.         End If
  614.     End If
  615.     Err.Clear
  616. End Function
  617. Public Sub Tables_Delete(ParamArray TablesToDelete())
  618.     On Error Resume Next
  619.     ' delete a collection of tables
  620.     Dim Item As Variant
  621.     For Each Item In TablesToDelete
  622.         Call Table_Delete(CStr(Item))
  623.         DoEvents
  624.         Err.Clear
  625.     Next
  626.     Err.Clear
  627. End Sub
  628. Public Function Table_Exists(ByVal strTbName As String) As Boolean
  629.     On Error Resume Next
  630.     ' returns existence of a table
  631.     Dim strCurrent As String
  632.     Dim dPos As Long
  633.     strCurrent = Me.Table_Names
  634.     dPos = MvSearch(strCurrent, strTbName, ",")
  635.     If dPos = 0 Then
  636.         Table_Exists = False
  637.     Else
  638.         Table_Exists = True
  639.     End If
  640.     Err.Clear
  641. End Function
  642. Public Property Get DateTimeDone() As String
  643.     On Error Resume Next
  644.     ' exact time now
  645.     DateTimeDone = Format$(Now, "dd/mm/yyyy hh:mm:ss ampm")
  646.     Err.Clear
  647. End Property
  648. Public Function ComputerName() As String
  649.     On Error Resume Next
  650.     ' return computer name
  651.     ComputerName = VBA.Environ$("COMPUTERNAME")
  652.     Err.Clear
  653. End Function
  654. Public Function Table_Names(Optional ByVal bShowSchema As Boolean = False, Optional ByVal bShowSystem As Boolean = False) As String
  655.     On Error Resume Next
  656.     ' returns all the names of the table types in the current database
  657.     Dim myRS As ADODB.Recordset
  658.     Set myRS = New ADODB.Recordset
  659.     Dim mStr As String
  660.     Dim mSource As String
  661.     Dim mType As String
  662.     Dim mName As String
  663.     Dim tbType As String
  664.     mStr = ""
  665.     tbType = ""
  666.     Set myRS = adoCn.OpenSchema(ADODB.adSchemaTables)
  667.     Do Until myRS.EOF
  668.         mSource = LCase$(RemNull(myRS!table_schema))
  669.         mType = LCase$(RemNull(myRS!table_type))
  670.         mName = RemNull(myRS!table_name)
  671.         Select Case LCase$(mType)
  672.         Case "system table"
  673.             If bShowSystem = True Then
  674.                 tbType = tbType & mName & ","
  675.             End If
  676.         Case "access table"
  677.             If bShowSchema = True Then
  678.                 tbType = tbType & mName & ","
  679.             End If
  680.         Case "table", "view"
  681.             tbType = tbType & mName & ","
  682.         End Select
  683.         myRS.MoveNext
  684.         Err.Clear
  685.     Loop
  686.     Table_Names = RemDelim(tbType, ",")
  687.     myRS.Close
  688.     Err.Clear
  689. End Function
  690. Public Function Table_FieldCreateIndex(ByVal TbName As String, ByVal IdxName As String) As Boolean
  691.     On Error Resume Next
  692.     Dim fExists As Boolean
  693.     fExists = Table_FieldIndexExists(TbName, IdxName)
  694.     If fExists = False Then
  695.         ' create an index in a table
  696.         QueryString = "create index [" & IdxName & "] on [" & TbName & "] (" & IdxName & ");"
  697.         Call Execute(QueryString)
  698.         DoEvents
  699.     End If
  700.     Table_FieldCreateIndex = Table_FieldIndexExists(TbName, IdxName)
  701.     Err.Clear
  702. End Function
  703. Private Function StrParse(retarray() As String, ByVal strText As String, ByVal Delimiter As String) As Long
  704.     On Error Resume Next
  705.     Dim varArray() As String
  706.     Dim varCnt As Long
  707.     Dim VarS As Long
  708.     Dim VarE As Long
  709.     Dim varA As Long
  710.     varArray = Split(strText, Delimiter)
  711.     VarS = LBound(varArray)
  712.     VarE = UBound(varArray)
  713.     varA = VarE + 1
  714.     ReDim retarray(varA)
  715.     For varCnt = VarS To VarE
  716.         varA = varCnt + 1
  717.         retarray(varA) = varArray(varCnt)
  718.         Err.Clear
  719.     Next
  720.     StrParse = UBound(retarray)
  721.     Err.Clear
  722. End Function
  723. Public Sub ArrayTrimItems(varArray() As String)
  724.     On Error Resume Next
  725.     ' trim all array elements
  726.     Dim uArray As Long
  727.     Dim cArray As Long
  728.     Dim lArray As Long
  729.     uArray = UBound(varArray)
  730.     lArray = LBound(varArray)
  731.     For cArray = lArray To uArray
  732.         varArray(cArray) = Trim$(varArray(cArray))
  733.         Err.Clear
  734.     Next
  735.     Err.Clear
  736. End Sub
  737. Public Function Iconv(ByVal sValue As String, Optional ByVal sFormat As String = "") As String
  738.     On Error Resume Next
  739.     ' remove characters specified below from a string
  740.     Dim sRslt As String
  741.     Dim I As Long
  742.     Dim Ch As String
  743.     Dim L As Long
  744.     Dim sN As String
  745.     sRslt = sValue
  746.     Select Case UCase$(sFormat)
  747.     Case ""
  748.         sRslt = Replace$(sRslt, ",", "")
  749.         sRslt = Replace$(sRslt, "/", "")
  750.         sRslt = Replace$(sRslt, ".", "")
  751.         sRslt = Replace$(sRslt, "(", "")
  752.         sRslt = Replace$(sRslt, ")", "")
  753.         sRslt = Replace$(sRslt, "~", "")
  754.         sRslt = Replace$(sRslt, ".", "")
  755.         sRslt = Replace$(sRslt, "@", "")
  756.         sRslt = Replace$(sRslt, "#", "")
  757.         sRslt = Replace$(sRslt, "$", "")
  758.         sRslt = Replace$(sRslt, "%", "")
  759.         sRslt = Replace$(sRslt, "^", "")
  760.         sRslt = Replace$(sRslt, "&", "")
  761.         sRslt = Replace$(sRslt, "*", "")
  762.         sRslt = Replace$(sRslt, "_", "")
  763.         sRslt = Replace$(sRslt, "-", "")
  764.         sRslt = Replace$(sRslt, "=", "")
  765.         sRslt = Replace$(sRslt, "|", "")
  766.         sRslt = Replace$(sRslt, "\", "")
  767.         sRslt = Replace$(sRslt, ":", "")
  768.         sRslt = Replace$(sRslt, ";", "")
  769.         sRslt = Replace$(sRslt, "<", "")
  770.         sRslt = Replace$(sRslt, ">", "")
  771.         sRslt = Replace$(sRslt, "?", "")
  772.         sRslt = Replace$(sRslt, "/", "")
  773.         sRslt = Replace$(sRslt, "'", "")
  774.         sRslt = Replace$(sRslt, "`", "")
  775.         sRslt = Replace$(sRslt, "+", "")
  776.         sRslt = Replace$(sRslt, "{", "")
  777.         sRslt = Replace$(sRslt, "}", "")
  778.         sRslt = Replace$(sRslt, "[", "")
  779.         sRslt = Replace$(sRslt, "]", "")
  780.         sRslt = Replace$(sRslt, Chr$(34), "")
  781.     Case "Q"
  782.         sRslt = Replace$(sRslt, "''", "")
  783.         sRslt = Replace$(sRslt, "'", "")
  784.     Case "F"
  785.         sRslt = Replace$(sRslt, "/", "%")
  786.         sRslt = Replace$(sRslt, "\", "%")
  787.         sRslt = Replace$(sRslt, "|", "%")
  788.     Case "C"
  789.         sRslt = Replace$(sRslt, ",", "")
  790.     Case "M"
  791.         sRslt = Replace$(sRslt, ",", "")
  792.         sRslt = Replace$(sRslt, ".", "")
  793.     Case "S"
  794.         L = Len(sRslt)
  795.         sRslt = sRslt
  796.         If L = 0 Then
  797.             Err.Clear
  798.             Exit Function
  799.         End If
  800.         sN = ""
  801.         For I = 1 To L
  802.             Ch = Mid$(sRslt, I, 1)
  803.             If Ch = " " Then
  804.                 sN = sN & Ch
  805.             End If
  806.             If Ch >= "a" Then
  807.                 If Ch <= "z" Then
  808.                     sN = sN & Ch
  809.                 End If
  810.             End If
  811.             If Ch >= "A" Then
  812.                 If Ch <= "Z" Then
  813.                     sN = sN & Ch
  814.                 End If
  815.             End If
  816.             Err.Clear
  817.         Next
  818.         sRslt = sN
  819.     Case "T"
  820.         sRslt = Replace$(sRslt, ".", "")
  821.         sRslt = Replace$(sRslt, "[", "")
  822.         sRslt = Replace$(sRslt, "]", "")
  823.         sRslt = Replace$(sRslt, ".", "")
  824.         sRslt = Replace$(sRslt, Chr$(34), "")
  825.         sRslt = Replace$(sRslt, "`", "")
  826.         sRslt = Replace$(sRslt, "'", "")
  827.         sRslt = Replace$(sRslt, ",", "")
  828.     End Select
  829.     Iconv = sRslt
  830.     Err.Clear
  831. End Function
  832. Public Function MvField(ByVal strData As String, Optional ByVal fldPos As Long = 1, Optional ByVal Delim As String = ";") As String
  833.     On Error Resume Next
  834.     ' returns a substring from a delimted string
  835.     Dim spData() As String
  836.     Dim spCnt As Long
  837.     MvField = ""
  838.     If Len(Delim) = 0 Then
  839.         Delim = Chr$(253)
  840.     End If
  841.     If Len(strData) = 0 Then
  842.         Err.Clear
  843.         Exit Function
  844.     End If
  845.     Call StrParse(spData, strData, Delim)
  846.     spCnt = UBound(spData)
  847.     Select Case fldPos
  848.     Case -1
  849.         MvField = Trim$(spData(spCnt))
  850.     Case -2
  851.         MvField = Trim$(spData(spCnt - 1))
  852.     Case Else
  853.         If fldPos <= spCnt Then
  854.             MvField = Trim$(spData(fldPos))
  855.         End If
  856.     End Select
  857.     Err.Clear
  858. End Function
  859.