home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD15746332001.psc / DBofADO.bas < prev    next >
Encoding:
BASIC Source File  |  2001-02-28  |  13.8 KB  |  418 lines

  1. Attribute VB_Name = "basDBofADO"
  2. ' DBofADO.bas
  3. '
  4. Option Explicit
  5.  
  6. Public Declare Function GetShortPathName Lib "KERNEL32" Alias "GetShortPathNameA" _
  7.     (ByVal lpszLongPath As String, ByVal lpszShortPath As String, _
  8.     ByVal cchBuffer As Long) As Long
  9.     
  10. Public gFileSpec As String               ' Filespec of MDB
  11. Public gTableName As String              ' Table name of selected MDB
  12. Public gstrFields() As String
  13. Public gstrFieldsOrig() As String
  14.  
  15. Public gcdg As Object
  16.  
  17. Public gAcnn As adodb.Connection
  18. Public gstrCNN As String
  19.  
  20.  
  21.  
  22. Sub Main()
  23.     Set gcdg = frmFrame.CommonDialog1
  24.     gFileSpec = ""
  25.     gTableName = ""
  26.     
  27.     frmFrame.Show
  28. End Sub
  29.  
  30.  
  31.  
  32. Sub DBFilesMDBproc()
  33.     On Error GoTo errhandler
  34.     
  35.     ' Obtain gFileSpec
  36.     Dim i As Integer
  37.     If GetFileSpec("(*.mdb)|*.mdb") = True Then
  38.          If UCase(Right(gFileSpec, 4)) <> ".MDB" Then
  39.              MsgBox "Please select a .MDB file"
  40.              Exit Sub
  41.          End If
  42.          
  43.          Set gAcnn = New adodb.Connection
  44.          gAcnn.CursorLocation = adUseClient
  45.          gstrCNN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
  46.             "Data Source=" & gFileSpec & ";"
  47.  
  48.             ' Only gAcnn, not gRcnn
  49.          If (gAcnn.Errors.Count > 0) Then
  50.              ' Just Display The First Error In The Collection
  51.             MsgBox "Error: " & gAcnn.Errors(0).Description, _
  52.                  0, "Connect Error!"
  53.             Exit Sub
  54.          End If
  55.          
  56.          frmDBofADO.Show
  57.     End If
  58.     Exit Sub
  59.     
  60.   ' Provided a way to exit, if error occurred in called form
  61.   ' forcing it to be closed
  62. errhandler:
  63.     ErrMsgProc "basMain DBFilesMDBProc"
  64. End Sub
  65.  
  66.  
  67.  
  68.  
  69. Function GetFileSpec(ByVal strFilter As String) As Boolean
  70.     On Error GoTo errhandler
  71.  
  72.     Dim tmpfile As String
  73.     tmpfile = gFileSpec
  74.    
  75.     Do
  76.         frmFrame.CommonDialog1.CancelError = True
  77.         frmFrame.CommonDialog1.FileName = tmpfile
  78.         frmFrame.CommonDialog1.Filter = strFilter
  79.         frmFrame.CommonDialog1.ShowOpen
  80.         
  81.         If frmFrame.CommonDialog1.FileName = "" Then
  82.             Exit Do
  83.         End If
  84.     
  85.         tmpfile = frmFrame.CommonDialog1.FileName
  86.         
  87.         If IsFileThere(tmpfile) Then
  88.             Exit Do
  89.         End If
  90.         
  91.         MsgBox "File specification not found.  Please re-try"
  92.     Loop
  93.     
  94.     If tmpfile <> "" Then
  95.         gFileSpec = tmpfile
  96.         GetFileSpec = True
  97.     Else
  98.         GetFileSpec = False
  99.     End If
  100.     
  101.     Exit Function
  102.     
  103. errhandler:
  104.    GetFileSpec = False
  105.    If Err.Number <> 32755 Then
  106.        ErrMsgProc "basMain GetFileSpec"
  107.    End If
  108. End Function
  109.  
  110.  
  111.  
  112. Sub ErrMsgProc(mMsg As String)
  113.     MsgBox mMsg & vbCrLf & Err.Number & Space(5) & Err.Description
  114. End Sub
  115.  
  116.  
  117.  
  118. ' Convert the numeric value returned by DB to Enum, so
  119. ' that at least the user could have a guess of what it is.
  120. Function ConvType(ByVal TypeVal As Long) As String
  121.   Select Case TypeVal
  122.       Case adBigInt                    ' 20
  123.          ConvType = "adBigInt"
  124.       Case adBinary                    ' 128
  125.          ConvType = "adBinary"
  126.       Case adBoolean                   ' 11
  127.          ConvType = "adBoolean"
  128.       Case adBSTR                      ' 8 i.e. null terminated string
  129.          ConvType = "adBSTR"
  130.       Case adChar                      ' 129
  131.          ConvType = "adChar"
  132.       Case adCurrency                  ' 6
  133.          ConvType = "adCurrency"
  134.       Case adDate                      ' 7
  135.          ConvType = "adDate"
  136.       Case adDBDate                    ' 133
  137.          ConvType = "adDBDate"
  138.       Case adDBTime                    ' 134
  139.          ConvType = "adDBTime"
  140.       Case adDBTimeStamp               ' 135
  141.          ConvType = "adDBTimeStamp"
  142.       Case adDecimal                   ' 14
  143.          ConvType = "adDecimal"
  144.       Case adDouble                    ' 5
  145.          ConvType = "adDouble"
  146.       Case adEmpty                     ' 0
  147.          ConvType = "adEmpty"
  148.       Case adError                     ' 10
  149.          ConvType = "adError"
  150.       Case adGUID                      ' 72
  151.          ConvType = "adGUID"
  152.       Case adIDispatch                 ' 9
  153.          ConvType = "adIDispatch"
  154.       Case adInteger                   ' 3
  155.          ConvType = "adInteger"
  156.       Case adIUnknown                  ' 13
  157.          ConvType = "adIUnknown"
  158.       Case adLongVarBinary             ' 205
  159.          ConvType = "adLongVarBinary"
  160.       Case adLongVarChar               ' 201
  161.          ConvType = "adLongVarChar"
  162.       Case adLongVarWChar              ' 203
  163.          ConvType = "adLongVarWChar"
  164.       Case adNumeric                  ' 131
  165.          ConvType = "adNumeric"
  166.       Case adSingle                    ' 4
  167.          ConvType = "adSingle"
  168.       Case adSmallInt                  ' 2
  169.          ConvType = "adSmallInt"
  170.       Case adTinyInt                   ' 16
  171.          ConvType = "adTinyInt"
  172.       Case adUnsignedBigInt            ' 21
  173.          ConvType = "adUnsignedBigInt"
  174.       Case adUnsignedInt               ' 19
  175.          ConvType = "adUnsignedInt"
  176.       Case adUnsignedSmallInt          ' 18
  177.          ConvType = "adUnsignedSmallInt"
  178.       Case adUnsignedTinyInt           ' 17
  179.          ConvType = "adUnsignedTinyInt"
  180.       Case adUserDefined               ' 132
  181.          ConvType = "adUserDefined"
  182.       Case adVarBinary                 ' 204
  183.          ConvType = "adVarBinary"
  184.       Case adVarChar                   ' 200
  185.          ConvType = "adVarChar"
  186.       Case adVariant                   ' 12
  187.          ConvType = "adVariant"
  188.       Case adVarWChar                  ' 202
  189.          ConvType = "adVarWChar"
  190.       Case adWChar                     ' 130
  191.          ConvType = "adWChar"
  192.    End Select
  193. End Function
  194.  
  195.  
  196.  
  197. Function ConvAttr(ByVal mAttr As Long) As String
  198.     Dim tmp As String
  199.     tmp = ""
  200.     If (mAttr And adFldMayDefer) Then
  201.         tmp = tmp & "adFldMayDefer "             '2
  202.     End If
  203.     If (mAttr And adFldUpdatable) Then
  204.         tmp = tmp & "adFldUpdatable "            '4
  205.     End If
  206.     If (mAttr And adFldUnknownUpdatable) Then
  207.         tmp = tmp & "adFldUnknownUpdatable "     '8
  208.     End If
  209.     If (mAttr And adFldFixed) Then
  210.         tmp = tmp & "adFldFixed "           '16
  211.     End If
  212.     If (mAttr And adFldIsNullable) Then
  213.         tmp = tmp & "adFldIsNullable "      '32
  214.     End If
  215.     If (mAttr And adFldMayBeNull) Then
  216.         tmp = tmp & "adFldMayBeNull "       '64
  217.     End If
  218.     If (mAttr And adFldLong) Then
  219.         tmp = tmp & "adFldLong "            '128
  220.     End If
  221.     If (mAttr And adFldRowID) Then
  222.         tmp = tmp & "adFldRowID "           '256
  223.     End If
  224.     If (mAttr And adFldRowVersion) Then
  225.        tmp = tmp & "adFldRowVersion "       '512
  226.     End If
  227.     If (mAttr And adFldCacheDeferred) Then
  228.         tmp = tmp & "adFldCacheDeferred "   '4096
  229.     End If
  230.     If tmp = "" Then
  231.         tmp = "Unknown"
  232.     End If
  233.     ConvAttr = tmp
  234. End Function
  235.  
  236.  
  237.  
  238. Function ConvLockType(ByVal mLockType) As String
  239.     Select Case mLockType
  240.        Case (mLockType And adLockReadOnly)
  241.            ConvLockType = "adLockReadOnly"           ' 1
  242.        Case (mLockType And adLockPessimistic)
  243.            ConvLockType = "adLockPessimistic"        ' 2
  244.        Case (mLockType And adLockOptimistic)
  245.            ConvLockType = "adLockOptimistic"         ' 3
  246.        Case (mLockType And adLockBatchOptimistic)
  247.            ConvLockType = "adLockBatchOptimistic"    ' 4
  248.        Case Else
  249.            ConvLockType = "(Unknown)"
  250.     End Select
  251. End Function
  252.  
  253.  
  254.  
  255. Function ConvEditMode(ByVal mEditMode) As String
  256.     Select Case mEditMode
  257.        Case (mEditMode And adEditNone)
  258.            ConvEditMode = "adEditNone"               ' 0
  259.        Case (mEditMode And adEditInProgress)
  260.            ConvEditMode = "adEditInProgress"         ' 1
  261.        Case (mEditMode And adEditAdd)
  262.            ConvEditMode = "adEditAdd"                ' 2
  263.        Case Else
  264.            ConvEditMode = "(Unknown)"
  265.     End Select
  266. End Function
  267.  
  268.  
  269.  
  270.  
  271. Function ConvState(ByVal mState) As String
  272.     Select Case mState
  273.        Case (mState And adStateClosed)
  274.            ConvState = "adStateClosed"           ' 0, default
  275.        Case (mState And adStateOpen)
  276.            ConvState = "adStateOpen"             '
  277.        Case (mState And adStateConnecting)
  278.            ConvState = "adStateConnecting"
  279.        Case (mState And adStateExecuting)
  280.            ConvState = "adStateExecuting"
  281.        Case (mState And adStateFetching)
  282.            ConvState = "adStateFetching"
  283.        Case Else
  284.            ConvState = "(Unknown)"
  285.     End Select
  286. End Function
  287.  
  288.  
  289.  
  290. 'Returns a sum of one or more of the RecordStatusEnum values.
  291. 'Use the Status property to see what changes are pending for records
  292. 'modified during batch updating. You can also use the Status property
  293. 'to view the status of records that fail during bulk operations, such
  294. 'as when you call the Resync, UpdateBatch, or CancelBatch methods on
  295. 'a Recordset object, or set the Filter property on a Recordset object
  296. 'to an array of bookmarks. With this property, you can determine how
  297. 'a given record failed and resolve it accordingly.
  298. Function ConvStatus(ByVal mStatus) As String
  299.     ' Because one or more values can be present, accumulate the string
  300.     Dim tmp As String
  301.     tmp = ""
  302.     Select Case mStatus
  303.        Case (mStatus And adRecOK)
  304.           ConvStatus = "adRecOK"           ' 0 Record was successfully update
  305.        Case (mStatus And adRecNew)
  306.           ConvStatus = "adRecNew"          ' 1 Is new
  307.        Case (mStatus And adRecModified)
  308.           ConvStatus = "adRecModified"     ' 2 Was modified.
  309.        Case (mStatus And adRecDeleted)
  310.           ConvStatus = "adRecDeleted"      ' 4 Was deleted.
  311.        Case (mStatus And adRecUnmodified)
  312.           ConvStatus = "adRecUnmodified"   ' 8 Was not modified.
  313.        Case (mStatus And adRecInvalid)
  314.           ConvStatus = "adRecInvalid"      ' 16 Was not saved because its bookmark is invalid.
  315.        Case (mStatus And adRecMultipleChanges)
  316.           ConvStatus = "adRecMultipleChanges"  ' 64 Not saved because it would have affected multiple records.
  317.        Case (mStatus And adRecPendingChanges)
  318.           ConvStatus = "adRecPendingChanges"   ' 128 Was not saved because it refers to a pending insert.
  319.        Case (mStatus And adRecCanceled)
  320.           ConvStatus = "adRecCanceled"         ' 256 Was not saved because the operation was canceled.
  321.        Case (mStatus And adRecCantRelease)
  322.           ConvStatus = "adRecCantRelease"      ' 1024 Was not saved because of existing record locks.
  323.        Case (mStatus And adRecConcurrencyViolation)
  324.           ConvStatus = "adRecConcurrencyViolation"   ' 2048 Was not saved because optimistic concurrency was in use.
  325.        Case (mStatus And adRecIntegrityViolation)
  326.           ConvStatus = "adRecIntegrityViolation"     ' 4096 Was not saved because the user violated integrity constraints.
  327.        Case (mStatus And adRecMaxChangesExceeded)
  328.           ConvStatus = "adRecMaxChangesExceeded"     ' 8192 Was not saved because there were too many pending changes.
  329.        Case (mStatus And adRecObjectOpen)
  330.           ConvStatus = "adRecObjectOpen"             ' 16384 Was not saved because of a conflict with an open storage object.
  331.        Case (mStatus And adRecOutOfMemory)
  332.           ConvStatus = "adRecOutOfMemory"            ' 32768 Was not saved because the computer has run out of memory.
  333.        Case (mStatus And adRecPermissionDenied)
  334.           ConvStatus = "adRecPermissionDenied"       ' 65536 Was not saved because the user has insufficient permissions.
  335.        Case (mStatus And adRecSchemaViolation)
  336.           ConvStatus = "adRecSchemaViolation"        ' 131072 Was not saved because it violates structure of underlying database.
  337.        Case (mStatus And adRecDBDeleted)
  338.           ConvStatus = "adRecDBDeleted"              ' 262144 The record has already been deleted from the data source.
  339.        Case Else
  340.           ConvStatus = "A combination of serveral status present"
  341.    End Select
  342. End Function
  343.  
  344.  
  345.  
  346. Function ConvCursorType(ByVal mCursorType) As String
  347.     Select Case mCursorType
  348.        Case (mCursorType And adOpenForwardOnly)
  349.            ConvCursorType = "adOpenForwardOnly"      ' 0
  350.        Case (mCursorType And adOpenKeyset)
  351.            ConvCursorType = "adOpenKeyset"           ' 1
  352.        Case (mCursorType And adOpenDynamic)
  353.            ConvCursorType = "adOpenKynamic"          ' 2
  354.        Case (mCursorType And adOpenStatic)
  355.            ConvCursorType = "adOpenStatic"           ' 3
  356.        Case Else
  357.            ConvCursorType = "(Unknown)"
  358.     End Select
  359. End Function
  360.  
  361.  
  362.  
  363.  
  364. Function IsFldTypeUnAllowedForSort(ByVal inType As Long) As Boolean
  365.      ' We disallow these types of fields, adBSTR (null-terminated string), adBinary,
  366.      ' adVarBinary, adLongBinary(205) for OLE object
  367.      ' (adLongBinary 6 is to be allowed, e.g. for currency.  adLongVarChar 201 should
  368.      ' be allowed when for memo type)
  369.     Const ExclFldTypes = "XX8/128/204/205"
  370.     Dim s As String
  371.     Dim inS As String
  372.     inS = LTrim(Trim(CStr(inType)))
  373.     If Len(inS) = 1 Then
  374.          s = "XX" & inS
  375.     ElseIf Len(inS) = 2 Then
  376.          s = "X" & inS
  377.     Else
  378.          s = inS
  379.     End If
  380.     IsFldTypeUnAllowedForSort = (InStr(ExclFldTypes, s) = 0)
  381. End Function
  382.  
  383.  
  384.  
  385.  
  386.  
  387. Function IsFileThere(inFileSpec As String) As Boolean
  388.     On Error Resume Next
  389.     Dim i
  390.     Dim mFile As String
  391.     mFile = LongToShort(inFileSpec)
  392.     i = FreeFile
  393.     Open inFileSpec For Input As i
  394.     If Err Then
  395.         IsFileThere = False
  396.     Else
  397.         Close i
  398.         IsFileThere = True
  399.     End If
  400. End Function
  401.  
  402.  
  403.  
  404. Private Function LongToShort(inSpec) As String
  405.     Dim i
  406.     Dim ShortSpec As String
  407.     Dim mBuffer As String
  408.     Dim mBufLen As Long
  409.     mBufLen = 164
  410.     mBuffer = String(mBufLen, 0)
  411.     i = GetShortPathName(inSpec, mBuffer, mBufLen)
  412.     LongToShort = Left$(mBuffer, i)
  413. End Function
  414.  
  415.  
  416.  
  417.  
  418.