Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 54.92 KB | None | 0 0
  1.  
  2. ; #INDEX# =======================================================================================================================
  3. ; Title .........: Microsoft Access Automation UDF Library for AutoIt3
  4. ; AutoIt Version : 3.2.0.
  5. ; Language ......: English
  6. ; Description ...: A collection of functions for creating, attaching to, reading from and manipulating Microsoft Access .
  7. ; Author(s) .....: Ayman Henry (aymhenry@gmail.com)
  8. ; Link...........: http://www.autoitscript.com/forum/topic/140277-ms-access-udf/#entry985303
  9. ; ===============================================================================================================================
  10.  
  11. ; ------------------------------------------------------------------------------
  12. ; Version: V2.1
  13. ; Last Update: 18/May/2012
  14. ; Requirements: AutoIt v3.2.0.1 or higher
  15. ; Notes: Errors associated with incorrect objects will be common user errors.
  16. ; Creating it agine after this UDF is now out of development and is no longer supported.
  17. ; http://www.autoitscript.com/forum/topic/40397-msaccess-udf-updated/page__p__318703__hl__msaccess%20phone__fromsearch__1#entry318703
  18. ; Update History:
  19. ; --- Rev 02.0--------------
  20. ; Fix bug in _AccessRecordList, bad return value
  21. ; Bug in Error Tape for _AccessRecordsCount
  22. ; --- Rev 02.1--------------
  23. ; Fix by (stealthf117jm) problem while using the function _AccessRecordAdd can not add NULL value.
  24. ; Add new functions
  25. ; ------------------------------------------------------------------------------
  26.  
  27. #include-once
  28. #include <Array.au3>
  29.  
  30. ; #VARIABLES# ===================================================================================================================
  31.  
  32. Global Enum _; Error Status Types
  33. $_AccessStatus_Success = 0, _
  34. $_AccessStatus_GeneralError, _
  35. $_AccessStatus_ComError, _
  36. $_AccessStatus_InvalidDataType, _
  37. $_AccessStatus_InvalidObjectType, _
  38. $_AccessStatus_InvalidValue, _
  39. $_AccessStatus_ReadOnly, _
  40. $_AccessStatus_NoMatch, _
  41. $_AccessStatus_DataTypeMisMatch, _
  42. $_AccessStatus_TableExists
  43.  
  44. Global $oAccessErrorHandler, $sAccessUserErrorHandler
  45. Global $_AccessErrorNotify = True
  46. Global $__AccessAU3Debug = False
  47. Global _; Com Error Handler Status Strings
  48. $AccessComErrorNumber, _
  49. $AccessComErrorNumberHex, _
  50. $AccessComErrorDescription, _
  51. $AccessComErrorScriptline, _
  52. $AccessComErrorWinDescription, _
  53. $AccessComErrorSource, _
  54. $AccessComErrorHelpFile, _
  55. $AccessComErrorHelpContext, _
  56. $AccessComErrorLastDllError, _
  57. $AccessComErrorComObj, _
  58. $AccessComErrorOutput
  59.  
  60.  
  61. ; #=====================================================================================================================
  62.  
  63. ; Not in TODO list _AccessCreateTable()
  64. ; NOT in TODO list _AccessUpdateTable($oNewDB, $oTable)
  65. ; Not in TODO list _AccessTabelDelete()
  66.  
  67.  
  68. ; #CURRENT# =====================================================================================================================
  69. ; New in Rev 2.1
  70. ; _AccessErrCode()
  71. ; _AccessErrMsg()
  72. ;_AccessSelectQuery
  73. ;_AccessActionQuery
  74. ;_AccessRecordMove
  75.  
  76. ; in Rev 2.0 and before -------------------
  77. ; _AccessOpen()
  78. ; _AccessClose()
  79.  
  80. ; _AccessTableExists()
  81. ; _AccessTablesCount()
  82. ; _AccessTablesList()
  83.  
  84. ; _AccessFieldExists
  85. ; _AccessFieldsList
  86. ; _AccessFieldsCount()
  87.  
  88. ; _AccessRecordsCount()
  89. ; _AccessRecordList ()
  90. ; _AccessRecordAdd()
  91. ; _AccessRecordEdit()
  92. ; _AccessRecordDelete ()
  93.  
  94. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  95. ; __AccessIsObjType
  96. ; __AccessCreateDB()
  97. ;__AccessInternalErrorHandlerRegister
  98. ;__AccessInternalErrorHandlerDeRegister
  99. ;__AccessInternalErrorHandler
  100. ; __AccessOpenDB
  101. ;__AccessRecordAddEdit
  102.  
  103. ; #FUNCTION# ====================================================================================================================
  104. ; Name...........: _AccessOpen
  105. ; Description ...: Open Microsoft Office Access File
  106. ; Parameters ....: $s_FilePath - File path and name
  107. ; $Options - Sets various options for the database, as specified in Remarks
  108. ; True Opens the database in exclusive (exclusive: A type of access to data in a database that is shared over a network.
  109. ; When you open a database in exclusive mode, you prevent others from opening the database.) mode.
  110. ; False (Default) Opens the database in shared mode.
  111. ; $ReadOnly - True if you want to open the database with read-only access, or False (default) if you want to open the database with read/write access.
  112. ; $Connect - Specifies various connection information, including passwords.
  113. ; Return values .: On Success - Returns database object linked to the opened file
  114. ; On Failure - Returns 0 and sets @ERROR
  115. ; @ERROR - 0 ($_AccessStatus_Success) = No Error
  116. ; - 1 ($_AccessStatus_GeneralError) = General Error
  117. ; - 3 ($_AccessStatus_InvalidDataType) = Invalid Data Type
  118. ; - 4 ($_AccessStatus_InvalidObjectType) = Invalid Object Type
  119. ; Author ........: Ayman Henry
  120. ; ADO Command ...: expression.OpenDatabase(Name, Options, ReadOnly, Connect)
  121. ; ===============================================================================================================================
  122. Func _AccessOpen($s_FilePath = "", $Options = False, $ReadOnly = False, $Connect = ";pwd=")
  123. Local $o_object, $o_MDBObj
  124.  
  125. If $s_FilePath = "" Then Return 0 ; There is currently no way of read non existing db.
  126. If FileExists($s_FilePath) = 0 Then Return 0
  127.  
  128. Local $result, $f_mustUnlock = 0, $i_ErrorStatusCode = $_AccessStatus_Success
  129.  
  130. ; Setup internal error handler to Trap COM errors, turn off error notification
  131. Local $status = __AccessInternalErrorHandlerRegister()
  132. If Not $status Then __AccessErrorNotify("Warning", "_AccessOpen", _
  133. "Cannot register internal error handler, cannot trap COM errors", _
  134. "Use _AccessErrorHandlerRegister() to register a user error handler")
  135. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  136.  
  137. _AccessErrorNotify(False)
  138. $o_object = ObjCreate("DAO.DBEngine.36")
  139.  
  140. If Not IsObj($o_object) Or @error = $_AccessStatus_ComError Then
  141. $i_ErrorStatusCode = $_AccessStatus_NoMatch
  142. EndIf
  143.  
  144. ; restore error notify and error handler status
  145. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  146. __AccessInternalErrorHandlerDeRegister()
  147.  
  148. If Not $i_ErrorStatusCode = $_AccessStatus_Success Then
  149. ;$o_object = ObjCreate("Access.Application")
  150. ;$o_object = ObjCreate("DAO.DBEngine.36")
  151.  
  152. If Not IsObj($o_object) Then
  153. __AccessErrorNotify("Error", "_AccessOpen", "", "Access Object Creation Failed")
  154. Return SetError($_AccessStatus_GeneralError, 0, 0)
  155. EndIf
  156. EndIf
  157.  
  158. $o_MDBObj = __AccessOpenDB($o_object, $s_FilePath, $Options, $ReadOnly, $Connect)
  159.  
  160. Return SetError(@error, 0, $o_MDBObj)
  161. EndFunc ;==>_AccessOpen
  162.  
  163. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  164. ; Name...........: __AccessInternalErrorHandlerRegister
  165. ; Description ...: Register and enable the internal Access COM error handler
  166. ; Parameters ....: None
  167. ; Return values .: Success - 1
  168. ; Failure - 0
  169. ; Author ........: Ayman Henry (based on Code written on Word.au3)
  170. ; Modified.......:
  171. ; Remarks .......:
  172. ; ===============================================================================================================================
  173. Func __AccessInternalErrorHandlerRegister()
  174. Local $sCurrentErrorHandler = ObjEvent("AutoIt.Error")
  175. If $sCurrentErrorHandler <> "" And Not IsObj($oAccessErrorHandler) Then
  176. ; We've got trouble... User COM Error handler assigned without using _AccessErrorHandlerRegister
  177. Return SetError($_AccessStatus_GeneralError, 0, 0)
  178. EndIf
  179. $oAccessErrorHandler = ""
  180. $oAccessErrorHandler = ObjEvent("AutoIt.Error", "__AccessInternalErrorHandler")
  181. If IsObj($oAccessErrorHandler) Then Return SetError($_AccessStatus_Success, 0, 1)
  182. Return SetError($_AccessStatus_GeneralError, 0, 0)
  183. EndFunc ;==>__AccessInternalErrorHandlerRegister
  184.  
  185. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  186. ; Name...........: __AccessInternalErrorHandlerDeRegister
  187. ; Description ...: DeRegister the internal Access COM error handler and register User Access COM error handler if defined
  188. ; Parameters ....: None
  189. ; Return values .: 1
  190. ; Author ........: Ayman Henry (based on Code written on Word.au3)
  191. ; Modified.......:
  192. ; Remarks .......:
  193. ; ===============================================================================================================================
  194. Func __AccessInternalErrorHandlerDeRegister()
  195. $oAccessErrorHandler = ""
  196. If $sAccessUserErrorHandler <> "" Then
  197. $oAccessErrorHandler = ObjEvent("AutoIt.Error", $sAccessUserErrorHandler)
  198. EndIf
  199. Return SetError($_AccessStatus_Success, 0, 1)
  200. EndFunc ;==>__AccessInternalErrorHandlerDeRegister
  201.  
  202. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  203. ; Name...........: __AccessInternalErrorHandler
  204. ; Description ...: Update $AccessComError... global variables
  205. ; Parameters ....: None
  206. ; Return values .: @error = $_AccessStatus_ComError
  207. ; Author ........: Ayman Henry (based on Code written on Word.au3)
  208. ; Modified.......:
  209. ; Remarks .......:
  210. ; ===============================================================================================================================
  211. Func __AccessInternalErrorHandler()
  212. $AccessComErrorScriptline = $oAccessErrorHandler.scriptline
  213. $AccessComErrorNumber = $oAccessErrorHandler.number
  214. $AccessComErrorNumberHex = Hex($oAccessErrorHandler.number, 8)
  215. $AccessComErrorDescription = StringStripWS($oAccessErrorHandler.description, 2)
  216. $AccessComErrorWinDescription = StringStripWS($oAccessErrorHandler.WinDescription, 2)
  217. $AccessComErrorSource = $oAccessErrorHandler.Source
  218. $AccessComErrorHelpFile = $oAccessErrorHandler.HelpFile
  219. $AccessComErrorHelpContext = $oAccessErrorHandler.HelpContext
  220. $AccessComErrorLastDllError = $oAccessErrorHandler.LastDllError
  221. $AccessComErrorOutput = ""
  222. $AccessComErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CRLF
  223. $AccessComErrorOutput &= "----> $AccessComErrorScriptline = " & $AccessComErrorScriptline & @CRLF
  224. $AccessComErrorOutput &= "----> $AccessComErrorNumberHex = " & $AccessComErrorNumberHex & @CRLF
  225. $AccessComErrorOutput &= "----> $AccessComErrorNumber = " & $AccessComErrorNumber & @CRLF
  226. $AccessComErrorOutput &= "----> $AccessComErrorWinDescription = " & $AccessComErrorWinDescription & @CRLF
  227. $AccessComErrorOutput &= "----> $AccessComErrorDescription = " & $AccessComErrorDescription & @CR
  228. $AccessComErrorOutput &= "----> $AccessComErrorSource = " & $AccessComErrorSource & @CRLF
  229. $AccessComErrorOutput &= "----> $AccessComErrorHelpFile = " & $AccessComErrorHelpFile & @CRLF
  230. $AccessComErrorOutput &= "----> $AccessComErrorHelpContext = " & $AccessComErrorHelpContext & @CRLF
  231. $AccessComErrorOutput &= "----> $AccessComErrorLastDllError = " & $AccessComErrorLastDllError & @CRLF
  232. If $_AccessErrorNotify Or $__AccessAU3Debug Then ConsoleWrite($AccessComErrorOutput & @CRLF)
  233. Return SetError($_AccessStatus_ComError)
  234. EndFunc ;==>__AccessInternalErrorHandler
  235.  
  236. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  237. ; Name ..........: __AccessOpenDB
  238. ; Description ...:
  239. ; Syntax ........: __AccessOpenDB(Byref $o_object, $s_FilePath[, $Options = False[, $ReadOnly = False[, $Connect = ";pwd="]]])
  240. ; Parameters ....: $o_object - [in/out] Object variable of a Access.Application object.
  241. ; $s_FilePath - A string value. Full path of the document to open
  242. ; $Options - [optional] Default is False.
  243. ; Sets various options for the database, as specified in Remarks
  244. ; True Opens the database in exclusive (exclusive: A type of access to data in a database that is shared over a network.
  245. ; When you open a database in exclusive mode, you prevent others from opening the database.) mode.
  246. ; $ReadOnly - [optional] Default is False.True if you want to open the database with read-only access, or False (default) if you want to open the database with read/write access.
  247. ; $Connect - [optional] Default is ";pwd=". Specifies various connection information, including passwords.
  248. ; Return values .: None
  249. ; Author ........: Ayman Henry
  250. ; Modified ......:
  251. ; Remarks .......: Under modification - not used
  252. ; Related .......:
  253. ; Link ..........:
  254. ; Example .......: No
  255. ; ===============================================================================================================================
  256. Func __AccessOpenDB(ByRef $o_object, $s_FilePath, $Options = False, $ReadOnly = False, $Connect = ";pwd=")
  257. If Not FileExists($s_FilePath) Then Return 0
  258. ;--------
  259. Local $o_doc
  260.  
  261. If Not IsObj($o_object) Then
  262. __AccessErrorNotify("Error", "_AccessOpenDB", "$_AccessStatus_InvalidDataType")
  263. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  264. EndIf
  265. ;
  266. If Not __AccessIsObjType($o_object, "application") Then
  267. __AccessErrorNotify("Error", "_AccessOpenDB", "$_AccessStatus_InvalidObjectType")
  268. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  269. EndIf
  270. ;
  271. ;$o_doc = $o_object.OpenDatabase ( $s_FilePath); ,True, True, ";pwd=" )
  272. $o_doc = $o_object.OpenDatabase($s_FilePath, $Options, $ReadOnly, $Connect)
  273.  
  274. If Not IsObj($o_doc) Then
  275. __AccessErrorNotify("Error", "_AccessOpenDB", "", "Document Object Creation Failed")
  276. Return SetError($_AccessStatus_GeneralError, 0, 0)
  277. EndIf
  278.  
  279. Return SetError($_AccessStatus_Success, 0, $o_doc)
  280. EndFunc ;==>__AccessOpenDB
  281.  
  282. ; #FUNCTION# ====================================================================================================================
  283. ; Name ..........: _AccessClose
  284. ; Description ...:
  285. ; Syntax ........: _AccessClose(Byref $o_object)
  286. ; Parameters ....: $o_object - [in/out] database object to close.
  287. ; Return values .: On Success - Returns 1
  288. ; On Failure - Returns 0
  289. ; Author ........: Ayman Henry
  290. ; Modified ......:
  291. ; Remarks .......:
  292. ; Related .......:
  293. ; Link ..........:
  294. ; Example .......: No
  295. ; ===============================================================================================================================
  296. Func _AccessClose(ByRef $o_object)
  297.  
  298. If Not IsObj($o_object) Then
  299. __AccessErrorNotify("Error", "_AccessClose", "$_AccessStatus_InvalidDataType")
  300. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  301. EndIf
  302. ;
  303. If Not __AccessIsObjType($o_object, "database") Then
  304. __AccessErrorNotify("Error", "_AccessClose", "$_AccessStatus_InvalidObjectType")
  305. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  306. EndIf
  307. ;
  308. $o_object.Close
  309.  
  310. Return SetError($_AccessStatus_Success, 1, 0)
  311. EndFunc ;==>_AccessClose
  312.  
  313. ; #FUNCTION# ====================================================================================================================
  314. ; Function Name: _AccessErrorNotify
  315. ; Description ...: Specifies whether Access.au3 automatically notifies of Warnings and Errors (to the console)
  316. ; Parameters ....: $f_notify - Optional: specifies whether notification should be on or off
  317. ; - -1 = (Default) return current setting
  318. ; - True = Turn On
  319. ; - False = Turn Off
  320. ; Return values .: On Success - If $f_notify = -1, returns the current notification setting, else returns 1
  321. ; On Failure - Returns 0
  322. ; Author ........: Ayman Henry (based on Code written on Word.au3)
  323. ; ===============================================================================================================================
  324. Func _AccessErrorNotify($f_notify = -1)
  325. Switch Number($f_notify)
  326. Case -1
  327. Return $_AccessErrorNotify
  328. Case 0
  329. $_AccessErrorNotify = False
  330. Return 1
  331. Case 1
  332. $_AccessErrorNotify = True
  333. Return 1
  334. Case Else
  335. __AccessErrorNotify("Error", "_AccessErrorNotify", "$_AccessStatus_InvalidValue")
  336. Return 0
  337. EndSwitch
  338. EndFunc ;==>_AccessErrorNotify
  339.  
  340. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  341. ; Name ..........: __AccessErrorNotify
  342. ; Description ...:
  343. ; Syntax ........: __AccessErrorNotify($s_severity, $s_func[, $s_status = ""[, $s_message = ""]])
  344. ; Parameters ....: $s_severity - A string value.
  345. ; $s_func - A string value.
  346. ; $s_status - [optional] A string value. Default is "".
  347. ; $s_message - [optional] A string value. Default is "".
  348. ; Return values .: None
  349. ; Author ........: Ayman Henry (based on Code written on Word.au3)
  350. ; Modified ......:
  351. ; Remarks .......:
  352. ; Related .......:
  353. ; Link ..........:
  354. ; Example .......: No
  355. ; ===============================================================================================================================
  356. Func __AccessErrorNotify($s_severity, $s_func, $s_status = "", $s_message = "")
  357. If $_AccessErrorNotify Or $__AccessAU3Debug Then
  358. Local $sStr = "--> Access.au3 " & $s_severity & " from function " & $s_func
  359. If Not $s_status = "" Then $sStr &= ", " & $s_status
  360. If Not $s_message = "" Then $sStr &= " (" & $s_message & ")"
  361. ConsoleWrite($sStr & @CRLF)
  362. EndIf
  363. Return 1
  364. EndFunc ;==>__AccessErrorNotify
  365.  
  366. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  367. ; Name ..........: __AccessIsObjType
  368. ; Description ...: Check to see if an object variable is of a specific type
  369. ; Syntax ........: __AccessIsObjType(Byref $o_object, $s_type)
  370. ; Parameters ....: $o_object - [in/out] an object to be checked.
  371. ; $s_type - Type Name, that the object has to be of its type.
  372. ; Return values .: True / False the check result
  373. ; Author ........: Ayman Henry (based on code in word.au3)
  374. ; Modified ......:
  375. ; Remarks .......:
  376. ; Related .......:
  377. ; Link ..........:
  378. ; Example .......: No
  379. ; ===============================================================================================================================
  380. Func __AccessIsObjType(ByRef $o_object, $s_type)
  381. If Not IsObj($o_object) Then Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  382.  
  383. ; Setup internal error handler to Trap COM errors, turn off error notification
  384. Local $status = __AccessInternalErrorHandlerRegister()
  385. If Not $status Then __AccessErrorNotify("Warning", "internal function __AccessIsObjType", _
  386. "Cannot register internal error handler, cannot trap COM errors", _
  387. "Use _AccessErrorHandlerRegister() to register a user error handler")
  388. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  389. _AccessErrorNotify(False)
  390. ;
  391. Local $s_Name = StringLower(ObjName($o_object)), $objectOK = False
  392.  
  393. Switch StringLower($s_type)
  394. Case "application"
  395. If $s_Name = "_dbengine" Then $objectOK = True
  396. Case "database"
  397. If $s_Name = "database" Then $objectOK = True
  398. Case "_tabledef"
  399. If $s_Name = "_tabledef" Then $objectOK = True
  400. Case "_field"
  401. If $s_Name = "_field" Then $objectOK = True
  402. Case "RecordSet"
  403. If $s_Name = "RecordSet" Then $objectOK = True
  404.  
  405. Case Else
  406. ; Unsupported ObjType specified
  407. Return SetError($_AccessStatus_InvalidValue, 2, 0)
  408. EndSwitch
  409.  
  410. ; restore error notify and error handler status
  411. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  412. __AccessInternalErrorHandlerDeRegister()
  413.  
  414. If $objectOK Then
  415. Return SetError($_AccessStatus_Success, 0, 1)
  416. Else
  417. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  418. EndIf
  419. EndFunc ;==>__AccessIsObjType
  420.  
  421. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  422. ; Name ..........: __AccessCreateDB
  423. ; Description ...: Returns an object variable representing a new empty document
  424. ; Syntax ........: __AccessCreateDB(Byref $o_object, $sFileLocation[, $Options = ""[, $ReadOnly = False[, $Connect = ""]]])
  425. ; Parameters ....: $o_object - [in/out] Object variable of a Access.Application object.
  426. ; $s_FilePath - A string value. Full path of the document to open
  427. ; $Options - [optional] Default is False.
  428. ; Sets various options for the database, as specified in Remarks
  429. ; True Opens the database in exclusive (exclusive: A type of access to data in a database that is shared over a network.
  430. ; When you open a database in exclusive mode, you prevent others from opening the database.) mode.
  431. ; $ReadOnly - [optional] Default is False.True if you want to open the database with read-only access, or False (default) if you want to open the database with read/write access.
  432. ; $Connect - [optional] Default is ";pwd=". Specifies various connection information, including passwords.
  433. ; Return values .: On Success - Returns an object variable pointing to a Access.Application, document object
  434. ; On Failure - Returns 0 and sets @ERROR
  435. ; @ERROR - 0 ($_AccessStatus_Success) = No Error
  436. ; - 1 ($_AccessStatus_GeneralError) = General Error
  437. ; - 3 ($_AccessStatus_InvalidDataType) = Invalid Data Type
  438. ; - 4 ($_AccessStatus_InvalidObjectType) = Invalid Object Type
  439. ; Author ........: Ayman Henry
  440. ; Modified ......:
  441. ; Remarks .......: Under modification - not used
  442. ; Related .......:
  443. ; Link ..........:
  444. ; Example .......: No
  445. ; ===============================================================================================================================
  446. Func __AccessCreateDB(ByRef $o_object, $sFileLocation, $Options = "", $ReadOnly = False, $Connect = "")
  447.  
  448. If Not IsObj($o_object) Then
  449. __AccessErrorNotify("Error", "__AccessCreateDB", "$_AccessStatus_InvalidDataType")
  450. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  451. EndIf
  452.  
  453. If Not __AccessIsObjType($o_object, "application") Then
  454. __AccessErrorNotify("Error", "__AccessCreateDB", "$_AccessStatus_InvalidObjectType")
  455. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  456. EndIf
  457.  
  458. Local $o_doc = $o_object.CreateDatabase($sFileLocation, ";LANGID=0x0409;CP=1252;COUNTRY=0") ; dbLangGeneral
  459.  
  460. If Not IsObj($o_doc) Then
  461. __AccessErrorNotify("Error", "__AccessCreateDB", "", "Document Object Creation Failed")
  462. Return SetError($_AccessStatus_GeneralError, 0, 0)
  463. EndIf
  464.  
  465. Return SetError($_AccessStatus_Success, 0, $o_doc)
  466. EndFunc ;==>__AccessCreateDB
  467.  
  468.  
  469. ; #FUNCTION# ====================================================================================================================
  470. ; Name ..........: _AccessTablesCount
  471. ; Description ...: Return number of tables in a database
  472. ; Syntax ........: _AccessTablesCount(Byref $o_DataBaseObject[, $b_OnlyNonSysTable = True])
  473. ; Parameters ....: $o_DataBaseObject - [in/out] database object.
  474. ; $b_OnlyNonSysTable - [optional] A binary value. Default is True. if True: do not Count also system tables
  475. ; Return values .: Number of tables on the given database
  476. ; Author ........: Ayman Henry
  477. ; Modified ......:
  478. ; Remarks .......:
  479. ; Related .......:
  480. ; Link ..........:
  481. ; Example .......: No
  482. ; ===============================================================================================================================
  483. Func _AccessTablesCount(ByRef $o_DataBaseObject, $b_OnlyNonSysTable = True)
  484. Local $n_Tables, $nCnt, $n_TablesCount = 0
  485.  
  486. If Not IsObj($o_DataBaseObject) Then
  487. __AccessErrorNotify("Error", "_AccessTablesCount", "$_AccessStatus_InvalidDataType")
  488. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  489. EndIf
  490.  
  491. If Not __AccessIsObjType($o_DataBaseObject, "database") Then
  492. __AccessErrorNotify("Error", "_AccessTablesCount", "$_AccessStatus_InvalidObjectType")
  493. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  494. EndIf
  495. ;if BitAND ($o_DataBaseObject.TableDefs($nCnt).Attributes,-2147483646) =0 Then ; dbSystemObject -2147483646
  496.  
  497. $n_Tables = $o_DataBaseObject.TableDefs.Count
  498.  
  499. If $b_OnlyNonSysTable = False Then
  500. Return $n_Tables
  501. EndIf
  502.  
  503. For $nCnt = 0 To $n_Tables - 1
  504. If BitAND($o_DataBaseObject.TableDefs($nCnt).Attributes, -2147483646) = 0 Then ; dbSystemObject -2147483646
  505. $n_TablesCount = $n_TablesCount + 1
  506. EndIf
  507. Next
  508. Return $n_TablesCount
  509.  
  510. EndFunc ;==>_AccessTablesCount
  511.  
  512. ; #FUNCTION# ====================================================================================================================
  513. ; Name ..........: _AccessTableExists
  514. ; Description ...: Checks if a table is exisits on a database
  515. ; Syntax ........: _AccessTableExists($o_Database[, $s_TableName = ""])
  516. ; Parameters ....: $o_Database - database object.
  517. ; $s_TableName - [optional] A string value. Default is "". table name
  518. ; Return values .: Sucess - The table object.,
  519. ; Fail - Return 0
  520. ; Author ........: Ayman Henry
  521. ; Modified ......:
  522. ; Remarks .......:
  523. ; Related .......:
  524. ; Link ..........:
  525. ; Example .......: No
  526. ; ===============================================================================================================================
  527. Func _AccessTableExists($o_Database, $s_TableName = "")
  528. Local $n_TableNo = _AccessTablesCount($o_Database, False)
  529. If $s_TableName = "" Or $n_TableNo = 0 Then Return 0
  530.  
  531. For $nCnt = 0 To $n_TableNo - 1
  532. If $o_Database.Tabledefs($nCnt).Name = $s_TableName Then
  533. Return $o_Database.Tabledefs($s_TableName)
  534. EndIf
  535. Next
  536.  
  537. Return 0
  538.  
  539. EndFunc ;==>_AccessTableExists
  540.  
  541. ; #FUNCTION# ====================================================================================================================
  542. ; Name ..........: _AccessFieldsCount
  543. ; Description ...: Return number of Fields on a table
  544. ; Syntax ........: _AccessFieldsCount(Byref $o_DataBaseObject, $s_Table)
  545. ; Parameters ....: $o_DataBaseObject - [in/out] Database object.
  546. ; $s_Table - A string value.Table name
  547. ; Return values .: Sucess - No. of fields in table
  548. ; Fail - Retrn 0
  549. ; Author ........: Ayman Henry
  550. ; Modified ......:
  551. ; Remarks .......:
  552. ; Related .......:
  553. ; Link ..........:
  554. ; Example .......: No
  555. ; ===============================================================================================================================
  556. Func _AccessFieldsCount(ByRef $o_DataBaseObject, $s_Table)
  557. Local $o_TableObject, $n_doc
  558.  
  559. If $s_Table = "" Then Return 0
  560. $o_TableObject = _AccessTableExists($o_DataBaseObject, $s_Table)
  561.  
  562. If $o_TableObject = 0 Then Return 0
  563.  
  564. If Not IsObj($o_DataBaseObject) Then
  565. __AccessErrorNotify("Error", "_AccessFieldsCount", "$_AccessStatus_InvalidDataType")
  566. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  567. EndIf
  568.  
  569. If Not __AccessIsObjType($o_DataBaseObject, "database") Then
  570. __AccessErrorNotify("Error", "_AccessFieldsCount", "$_AccessStatus_InvalidObjectType")
  571. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  572. EndIf
  573.  
  574. ;$n_doc = $o_DataBaseObject.TableDefs($s_Table).Fields.Count ; db.TableDefs(0).Fields.Count
  575. $n_doc = $o_TableObject.Fields.Count ; db.TableDefs(0).Fields.Count
  576.  
  577. Return $n_doc
  578. EndFunc ;==>_AccessFieldsCount
  579.  
  580. ; #FUNCTION# ====================================================================================================================
  581. ; Name ..........: _AccessFieldExists
  582. ; Description ...: Checks if a Field is Exisys in a table
  583. ; Syntax ........: _AccessFieldExists(Byref $o_Table[, $s_Field = ""])
  584. ; Parameters ....: $o_Table - [in/out] Table Object.
  585. ; $s_Field - [optional] A string value. Default is "". Field Name
  586. ; Return values .: Sucess 1, Fail 0
  587. ; Author ........: Ayman Henry
  588. ; Modified ......:
  589. ; Remarks .......:
  590. ; Related .......:
  591. ; Link ..........:
  592. ; Example .......: No
  593. ; ===============================================================================================================================
  594. Func _AccessFieldExists(ByRef $o_Table, $s_Field = "")
  595.  
  596. Local $n_FieldsNo
  597.  
  598. If $s_Field = "" Then Return 0
  599.  
  600. If Not IsObj($o_Table) Then
  601. __AccessErrorNotify("Error", "_AccessFieldExists", "$_AccessStatus_InvalidDataType")
  602. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  603. EndIf
  604.  
  605. If Not __AccessIsObjType($o_Table, "_TableDef") Then
  606. __AccessErrorNotify("Error", "_AccessFieldExists", "$_AccessStatus_InvalidObjectType")
  607. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  608. EndIf
  609.  
  610.  
  611. $n_FieldsNo = $o_Table.Fields.Count
  612.  
  613. For $nCnt = 0 To $n_FieldsNo - 1
  614. ;if $o_Database.TableDefs($s_TableName).Fields($nCnt).Name = $s_Field then Return True
  615. If $o_Table.Fields($nCnt).Name = $s_Field Then
  616. Return $o_Table.Fields($nCnt)
  617. EndIf
  618. Next
  619.  
  620. Return False
  621.  
  622. EndFunc ;==>_AccessFieldExists
  623.  
  624. ; #FUNCTION# ====================================================================================================================
  625. ; Name ..........: _AccessRecordsCount
  626. ; Description ...: Count the number of Records on a table
  627. ; Syntax ........: _AccessRecordsCount(Byref $o_Database[, $s_TableName = ""])
  628. ; Parameters ....: $o_Database - [in/out] database object.
  629. ; $s_TableName - [optional] A string value. Default is "".Table Name
  630. ; Return values .: Sucess - No of recordes
  631. ; Fail Return 0
  632. ; Author ........: Ayman Henry
  633. ; Modified ......:
  634. ; Remarks .......:
  635. ; Related .......:
  636. ; Link ..........:
  637. ; Example .......: No
  638. ; ===============================================================================================================================
  639. Func _AccessRecordsCount(ByRef $o_Database, $s_TableName = "")
  640. Local $nErr
  641. If $s_TableName = "" Then Return 0
  642. If _AccessTableExists($o_Database, $s_TableName) = 0 Then Return 0
  643. ;-------
  644. Local $n_RecCount, $o_Rec
  645. $o_Rec = $o_Database.OpenrecordSet($s_TableName)
  646.  
  647. If Not IsObj($o_Rec) Then
  648. __AccessErrorNotify("Error", "_AccessRecordsCount", "$_AccessStatus_InvalidDataType")
  649. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  650. EndIf
  651.  
  652. If Not __AccessIsObjType($o_Rec, "RecordSet") Then
  653. __AccessErrorNotify("Error", "_AccessRecordsCount", "$_AccessStatus_InvalidObjectType")
  654. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  655. EndIf
  656.  
  657.  
  658.  
  659. ; Setup internal error handler to Trap COM errors, turn off error notification
  660. Local $status = __AccessInternalErrorHandlerRegister()
  661. If Not $status Then __AccessErrorNotify("Warning", "_AccessRecordAdd", _
  662. "Cannot register internal error handler, cannot trap COM errors", _
  663. "Use _AccessErrorHandlerRegister() to register a user error handler")
  664. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  665. $_AccessErrorNotify = False
  666.  
  667. $o_Rec.MoveFirst
  668. $nErr = @error
  669. $n_RecCount = $o_Rec.RecordCount
  670. $nErr = @error + $nErr
  671.  
  672. $_AccessErrorNotify = True
  673.  
  674. ; restore error notify and error handler status
  675. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  676. __AccessInternalErrorHandlerDeRegister()
  677.  
  678. If $nErr = 0 Then
  679. Return $n_RecCount
  680. Else
  681. Return 0
  682. EndIf
  683. EndFunc ;==>_AccessRecordsCount
  684.  
  685. ; #FUNCTION# ====================================================================================================================
  686. ; Name ..........: _AccessTablesList
  687. ; Description ...: Returns an array has a list of tables on a database
  688. ; Syntax ........: _AccessTablesList(Byref $o_DataBaseObject[, $b_OnlyNonSysTable = True])
  689. ; Parameters ....: $o_DataBaseObject - [in/out] Database Object.
  690. ; $b_OnlyNonSysTable - [optional] A binary value. Default is True. if true, ignores the system tables
  691. ; Return values .: Array with tables name, the first array item has the number of tables.
  692. ; Author ........: Ayman Henry
  693. ; Modified ......:
  694. ; Remarks .......:
  695. ; Related .......:
  696. ; Link ..........:
  697. ; Example .......: No
  698. ; ===============================================================================================================================
  699. Func _AccessTablesList(ByRef $o_DataBaseObject, $b_OnlyNonSysTable = True)
  700. Local $avArray[1], $n_Count = 0
  701. Local $n_TablesCount = _AccessTablesCount($o_DataBaseObject, False)
  702. $avArray[0] = $n_TablesCount
  703. If $n_TablesCount = 0 Then Return $avArray
  704.  
  705. For $nCnt = 0 To $n_TablesCount - 1
  706. If $b_OnlyNonSysTable = True Then
  707. If BitAND($o_DataBaseObject.TableDefs($nCnt).Attributes, -2147483646) = 0 Then ; dbSystemObject -2147483646
  708. $n_Count = $n_Count + 1
  709. _ArrayAdd($avArray, $o_DataBaseObject.TableDefs($nCnt).Name)
  710. EndIf
  711. Else
  712. $n_Count = $n_Count + 1
  713. _ArrayAdd($avArray, $o_DataBaseObject.TableDefs($nCnt).Name)
  714. EndIf
  715. Next
  716. $avArray[0] = $n_Count
  717. Return $avArray
  718.  
  719. EndFunc ;==>_AccessTablesList
  720.  
  721. ; #FUNCTION# ====================================================================================================================
  722. ; Name ..........: _AccessFieldsList
  723. ; Description ...: Returns an array has a list of fields on a database
  724. ; Syntax ........: _AccessFieldsList(Byref $o_DataBaseObject, $s_Table)
  725. ; Parameters ....: $o_DataBaseObject - [in/out] database name.
  726. ; $s_Table - A string value. table object.
  727. ; Return values .: Array with files name, the first array item has the number of fields.
  728. ; Author ........: Ayman Henry
  729. ; Modified ......:
  730. ; Remarks .......:
  731. ; Related .......:
  732. ; Link ..........:
  733. ; Example .......: No
  734. ; ===============================================================================================================================
  735. Func _AccessFieldsList(ByRef $o_DataBaseObject, $s_Table)
  736. Local $avArray[1]
  737. Local $n_FieldsCount = _AccessFieldsCount($o_DataBaseObject, $s_Table)
  738. $avArray[0] = $n_FieldsCount
  739.  
  740. If $n_FieldsCount = 0 Then Return $avArray
  741.  
  742. For $nCnt = 0 To $n_FieldsCount - 1
  743. ;_ArrayAdd ($avArray, $o_Table.Fields($nCnt).Name )
  744. _ArrayAdd($avArray, $o_DataBaseObject.TableDefs($s_Table).Fields($nCnt).Name)
  745. Next
  746.  
  747. Return $avArray
  748.  
  749. EndFunc ;==>_AccessFieldsList
  750.  
  751. ; #FUNCTION# ====================================================================================================================
  752. ; Name ..........: _AccessRecordList
  753. ; Description ...: Returns an array has a list of record fields value.
  754. ; Syntax ........: _AccessRecordList(Byref $o_DataBaseObject, $s_Table, $nRecNo)
  755. ; Parameters ....: $o_DataBaseObject - [in/out] Database object.
  756. ; $s_Table - A string value. Table Name
  757. ; $nRecNo - An integer number value. The abslout position for the required fields.
  758. ; Return values .: Array with one record data, the first array item has the number of fields.
  759. ; Author ........: Ayman Henry
  760. ; Modified ......:
  761. ; Remarks .......:
  762. ; Related .......:
  763. ; Link ..........:
  764. ; Example .......: No
  765. ; ===============================================================================================================================
  766. Func _AccessRecordList(ByRef $o_DataBaseObject, $s_Table, $nRecNo)
  767. Local $avArray[1], $o_Rec
  768. Local $n_FieldsCount = _AccessFieldsCount($o_DataBaseObject, $s_Table)
  769. $avArray[0] = 0
  770. If $n_FieldsCount = 0 Then Return $avArray
  771. If Not IsInt($nRecNo) Then Return $avArray
  772. If $nRecNo < 1 Then Return $avArray
  773.  
  774. $o_Rec = $o_DataBaseObject.OpenRecordSet($s_Table)
  775.  
  776. If Not IsObj($o_Rec) Then
  777. __AccessErrorNotify("Error", "_AccessRecordList", "$_AccessStatus_InvalidDataType")
  778. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  779. EndIf
  780.  
  781. If Not __AccessIsObjType($o_Rec, "RecordSet") Then
  782. __AccessErrorNotify("Error", "_AccessRecordList", "$_AccessStatus_InvalidObjectType")
  783. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  784. EndIf
  785. ;-------
  786. $o_Rec.MoveFirst
  787. $o_Rec.Move($nRecNo - 1)
  788.  
  789. If $o_Rec.Eof = -1 Or $o_Rec.Bof = -1 Then
  790. $o_Rec.close
  791. Return $avArray
  792. EndIf
  793.  
  794. $avArray[0] = $n_FieldsCount
  795. For $nCnt = 0 To $n_FieldsCount - 1
  796. ;_ArrayAdd ($avArray, $o_Table.Fields($nCnt).Name )
  797. _ArrayAdd($avArray, $o_Rec.Fields($nCnt).Value)
  798. Next
  799.  
  800. $o_Rec.close
  801. Return $avArray
  802.  
  803. EndFunc ;==>_AccessRecordList
  804.  
  805. ; #FUNCTION# ====================================================================================================================
  806. ; Name ..........: _AccessRecordAdd
  807. ; Description ...: Add a record to a table
  808. ; Syntax ........: _AccessRecordAdd(Byref $o_DataBaseObject, $s_Table, $av_Fields)
  809. ; Parameters ....: $o_DataBaseObject - [in/out] Database object.
  810. ; $s_Table - A string value. table name to add a record to
  811. ; $av_Fields - An array of variants. Two dim. array, col.1 has the field name col.2 the value.
  812. ; Return values .: Success 1
  813. ; Falis any other value.
  814. ; Author ........: Ayman Henry
  815. ; Modified ......:
  816. ; Remarks .......:
  817. ; Related .......:
  818. ; Link ..........:
  819. ; Example .......: No
  820. ; ===============================================================================================================================
  821. Func _AccessRecordAdd(ByRef $o_DataBaseObject, $s_Table, $av_Fields)
  822. Return __AccessRecordAddEdit($o_DataBaseObject, $s_Table, $av_Fields)
  823. EndFunc ;==>_AccessRecordAdd
  824.  
  825. ; #FUNCTION# ====================================================================================================================
  826. ; Name ..........: _AccessRecordEdit
  827. ; Description ...: Edit a record to a table
  828. ; Syntax ........: _AccessRecordEdit(Byref $o_DataBaseObject, $s_Table, $av_Fields, $nRecNo)
  829. ; Parameters ....: $o_DataBaseObject - [in/out] Database object.
  830. ; $s_Table - A string value. table name to edit.
  831. ; $av_Fields - An array of variants.An array of variants. Two dim. array, col.1 has the field name col.2 the value.
  832. ; $nRecNo - An integer number value. the absoulte position of the record to be edit
  833. ; Return values .: Success 1
  834. ; Falis any other value.
  835. ; Author ........: Ayman Henry
  836. ; Modified ......:
  837. ; Remarks .......:
  838. ; Related .......:
  839. ; Link ..........:
  840. ; Example .......: No
  841. ; ===============================================================================================================================
  842. Func _AccessRecordEdit(ByRef $o_DataBaseObject, $s_Table, $av_Fields, $nRecNo)
  843. Return __AccessRecordAddEdit($o_DataBaseObject, $s_Table, $av_Fields, $nRecNo)
  844. EndFunc ;==>_AccessRecordEdit
  845.  
  846. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  847. ; Name ..........: __AccessRecordAddEdit
  848. ; Description ...: Add or Edit a record if $nRecNo is given i.e it is in Edit mode
  849. ; Syntax ........: __AccessRecordAddEdit(Byref $o_DataBaseObject, $s_Table, $av_Fields[, $nRecNo = Default])
  850. ; Parameters ....: $o_DataBaseObject - [in/out] Database object.
  851. ; $s_Table - A string value. table name to edit.
  852. ; $av_Fields - An array of variants.An array of variants. Two dim. array, col.1 has the field name col.2 the value.
  853. ; $nRecNo - An integer number value. the absoulte position of the record to be edit
  854. ; Return values .: Success 1
  855. ; Falis any other value.
  856. ; Author ........: Ayaman Henry
  857. ; Modified ......: stealthf117jm
  858. ; Remarks .......:
  859. ; Related .......:
  860. ; Link ..........:
  861. ; Example .......: No
  862. ; ===============================================================================================================================
  863. Func __AccessRecordAddEdit(ByRef $o_DataBaseObject, $s_Table, $av_Fields, $nRecNo = Default)
  864.  
  865. If $s_Table = "" Then Return -1
  866. If Not IsArray($av_Fields) Then Return -2
  867. If UBound($av_Fields, 0) <> 2 Then Return -3
  868.  
  869. If $nRecNo <> Default Then
  870. If Not IsInt($nRecNo) Then Return 0
  871. If $nRecNo < 1 Then Return 0
  872. EndIf
  873. ;------------
  874. Local $o_Rec, $b_Err = False
  875. Local $o_Table = _AccessTableExists($o_DataBaseObject, $s_Table)
  876. If $o_Table = 0 Then Return -4
  877.  
  878. ;--------
  879. For $nCnt = 0 To UBound($av_Fields, 0) - 1
  880. If 0 = _AccessFieldExists($o_Table, $av_Fields[$nCnt][0]) Then Return -5
  881. Next
  882. ;--------
  883. $o_Rec = $o_DataBaseObject.OpenRecordSet($s_Table)
  884.  
  885. If Not IsObj($o_Rec) Then
  886. __AccessErrorNotify("Error", "_AccessRecordList", "$_AccessStatus_InvalidDataType")
  887. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  888. EndIf
  889.  
  890. If Not __AccessIsObjType($o_Rec, "RecordSet") Then
  891. __AccessErrorNotify("Error", "_AccessRecordList", "$_AccessStatus_InvalidObjectType")
  892. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  893. EndIf
  894. ;-------
  895.  
  896. ; Setup internal error handler to Trap COM errors, turn off error notification
  897. Local $status = __AccessInternalErrorHandlerRegister()
  898. If Not $status Then __AccessErrorNotify("Warning", "_AccessRecordAdd", _
  899. "Cannot register internal error handler, cannot trap COM errors", _
  900. "Use _AccessErrorHandlerRegister() to register a user error handler")
  901. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  902. _AccessErrorNotify(False)
  903.  
  904. If $nRecNo = Default Then
  905. $o_Rec.AddNew
  906. Else
  907.  
  908. $o_Rec.MoveFirst
  909. If @error <> 0 Then $b_Err = True
  910.  
  911. $o_Rec.Move($nRecNo - 1)
  912. If @error <> 0 Then $b_Err = True
  913.  
  914. $o_Rec.Edit
  915. If @error <> 0 Then $b_Err = True
  916. EndIf
  917.  
  918. If $b_Err = False Then
  919.  
  920. For $nCnt = 0 To UBound($av_Fields, 1) - 1
  921. ;$o_Rec.Fields($av_Fields[$nCnt][0]).Value = $av_Fields[$nCnt][1]
  922. ;Fixed by: By stealthf117jm
  923. ;If $av_Fields[$nCnt][1] <> "" ;this will detect 0 as "" and not allow you to add 0 to a field
  924. If Not ($av_Fields[$nCnt][1] == "") Then $o_Rec.Fields($av_Fields[$nCnt][0]).Value = $av_Fields[$nCnt][1]
  925. ;----
  926. If @error <> 0 Then ;$_AccessStatus_ComError
  927. $b_Err = True
  928. ExitLoop
  929. EndIf
  930. Next
  931. EndIf
  932.  
  933. ; restore error notify and error handler status
  934. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  935. __AccessInternalErrorHandlerDeRegister()
  936.  
  937. If $b_Err = True Then
  938. $o_Rec.CancelUpdate
  939. __AccessErrorNotify("Error", "_AccessRecordAdd", "$_AccessStatus_DataTypeMisMatch, Field =" & $o_Rec.Fields($av_Fields[$nCnt][0]).Name)
  940. Return SetError($_AccessStatus_DataTypeMisMatch, 1, 0)
  941. Else
  942. $o_Rec.Update
  943. EndIf
  944.  
  945. $o_Rec.close
  946. Return 1
  947.  
  948. EndFunc ;==>__AccessRecordAddEdit
  949.  
  950. ; #FUNCTION# ====================================================================================================================
  951. ; Name ..........: _AccessRecordDelete
  952. ; Description ...: Delete a record
  953. ; Syntax ........: _AccessRecordDelete(Byref $o_DataBaseObject, $s_Table, $nRecNo)
  954. ; Parameters ....: $o_DataBaseObject - [in/out] database object.
  955. ; $s_Table - A string value. table name
  956. ; $nRecNo - An integer number value. Absoult position of record.
  957. ; Return values .: None
  958. ; Author ........: Ayman Henry
  959. ; Modified ......:
  960. ; Remarks .......:
  961. ; Related .......:
  962. ; Link ..........:
  963. ; Example .......: No
  964. ; ===============================================================================================================================
  965. Func _AccessRecordDelete(ByRef $o_DataBaseObject, $s_Table, $nRecNo)
  966. Local $o_Rec, $b_Err = 0
  967. Local $n_FieldsCount = _AccessFieldsCount($o_DataBaseObject, $s_Table)
  968.  
  969. If $n_FieldsCount = 0 Then Return 0
  970. If Not IsInt($nRecNo) Then Return 0
  971. If $nRecNo < 1 Then Return 0
  972.  
  973. $o_Rec = $o_DataBaseObject.OpenRecordSet($s_Table)
  974.  
  975. If Not IsObj($o_Rec) Then
  976. __AccessErrorNotify("Error", "_AccessRecordList", "$_AccessStatus_InvalidDataType")
  977. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  978. EndIf
  979.  
  980. If Not __AccessIsObjType($o_Rec, "RecordSet") Then
  981. __AccessErrorNotify("Error", "_AccessRecordList", "$_AccessStatus_InvalidObjectType")
  982. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  983. EndIf
  984. ;-------
  985. $o_Rec.MoveFirst
  986. $o_Rec.Move($nRecNo - 1)
  987.  
  988. If $o_Rec.Eof = -1 Or $o_Rec.Bof = -1 Then
  989. $o_Rec.close
  990. Return 0
  991. EndIf
  992.  
  993. ; Setup internal error handler to Trap COM errors, turn off error notification
  994. Local $status = __AccessInternalErrorHandlerRegister()
  995. If Not $status Then __AccessErrorNotify("Warning", "_AccessRecordAdd", _
  996. "Cannot register internal error handler, cannot trap COM errors", _
  997. "Use _AccessErrorHandlerRegister() to register a user error handler")
  998. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  999.  
  1000. $o_Rec.Delete
  1001. If @error <> 0 Then ;$_AccessStatus_ComError
  1002. $b_Err = True
  1003. EndIf
  1004.  
  1005. ; restore error notify and error handler status
  1006. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  1007. __AccessInternalErrorHandlerDeRegister()
  1008. $o_Rec.close
  1009.  
  1010. If $b_Err = 1 Then Return 0
  1011. Return 1
  1012. EndFunc ;==>_AccessRecordDelete
  1013. ; #FUNCTION# ====================================================================================================================
  1014. ; Name ..........: _AccessErrCode
  1015. ; Description ...: Error Code Notification
  1016. ; Syntax ........: _AccessErrCode()
  1017. ; Parameters ....:
  1018. ; Return values .: Error Code
  1019. ; Author ........: Ayman Henry
  1020. ; Modified ......:
  1021. ; Remarks .......:
  1022. ; Related .......:
  1023. ; Link ..........:
  1024. ; Example .......: No
  1025. ; ===============================================================================================================================
  1026. Func _AccessErrCode()
  1027. Return $AccessComErrorNumber
  1028. EndFunc ;==>_AccessErrCode
  1029.  
  1030. ; #FUNCTION# ====================================================================================================================
  1031. ; Name ..........: _AccessErrMsg
  1032. ; Description ...: Error Message Nofification
  1033. ; Syntax ........: _AccessErrMsg()
  1034. ; Parameters ....:
  1035. ; Return values .: Error message
  1036. ; Author ........: Ayman Henry
  1037. ; Modified ......:
  1038. ; Remarks .......:
  1039. ; Related .......:
  1040. ; Link ..........:
  1041. ; Example .......: No
  1042. ; ===============================================================================================================================
  1043. Func _AccessErrMsg()
  1044. Return $AccessComErrorDescription
  1045. EndFunc ;==>_AccessErrMsg
  1046. ; #FUNCTION# ====================================================================================================================
  1047. ; Name ..........: _AccessActionQuery
  1048. ; Description ...: Exceute an Action Query
  1049. ; Syntax ........: _AccessActionQuery(Byref $o_DataBaseObject, $sQuery)
  1050. ; Parameters ....: $o_DataBaseObject - [in/out] An unknown value.
  1051. ; $sQuery - A string value. Query in SQL.
  1052. ; Return values .: Sucess Retrns 1, On Failure - Returns 0
  1053. ; Author ........: Ayman Henry
  1054. ; Modified ......:
  1055. ; Remarks .......: Action query do not starts with Select
  1056. ; Related .......:
  1057. ; Link ..........:
  1058. ; Example .......: No
  1059. ; ===============================================================================================================================
  1060. Func _AccessActionQuery(ByRef $o_DataBaseObject, $sQuery)
  1061. Local $n_Err = 0
  1062. $sQuery = StringStripWS($sQuery, 3)
  1063.  
  1064. If StringLower((StringLeft($sQuery, 4))) = "sele" Then
  1065. Return 0
  1066. EndIf
  1067.  
  1068. If Not IsObj($o_DataBaseObject) Then
  1069. __AccessErrorNotify("Error", "_AccessActionQuery", "$_AccessQuery_InvalidDataType")
  1070. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  1071. EndIf
  1072.  
  1073. If Not __AccessIsObjType($o_DataBaseObject, "database") Then
  1074. __AccessErrorNotify("Error", "_AccessActionQuery", "$_AccessQuery_InvalidObjectType")
  1075. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  1076. EndIf
  1077. $n_Err = 1
  1078. ; Setup internal error handler to Trap COM errors, turn off error notification
  1079. Local $status = __AccessInternalErrorHandlerRegister()
  1080. If Not $status Then __AccessErrorNotify("Warning", "_AccessRecordAdd", _
  1081. "Cannot register internal error handler, cannot trap COM errors", _
  1082. "Use _AccessErrorHandlerRegister() to register a user error handler")
  1083. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  1084.  
  1085. $o_DataBaseObject.Execute($sQuery) ; dbSeeChanges Generates a run-time error if another user is changing data you are editing (Microsoft Access workspaces only).
  1086.  
  1087. If @error <> 0 Then ;$_AccessStatus_ComError
  1088. $n_Err = 0
  1089. EndIf
  1090.  
  1091. ; restore error notify and error handler status
  1092. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  1093. __AccessInternalErrorHandlerDeRegister()
  1094.  
  1095. Return $n_Err
  1096. EndFunc ;==>_AccessActionQuery
  1097. ; #FUNCTION# ====================================================================================================================
  1098. ; Name ..........: _AccessFetchData
  1099. ; Description ...: Read Data From a Query
  1100. ; Syntax ........: _AccessFetchData (Byref $h_Query, Byref $aRow)
  1101. ; Parameters ....: $h_Query - [in/out] A handle value.
  1102. ; $aRow - [in/out] An array. Array[0], the number of Fields, Rest: data in Recored
  1103. ; Return values .: Sucess Retrns 1, On Failure - Returns 0
  1104. ; Author ........: Ayman Henry
  1105. ; Modified ......:
  1106. ; Remarks .......:
  1107. ; Related .......:
  1108. ; Link ..........:
  1109. ; Example .......: No
  1110. ; ===============================================================================================================================
  1111. Func _AccessFetchData(ByRef $h_Query, ByRef $aRow)
  1112. Local $n_Err = 0
  1113.  
  1114. If Not IsObj($h_Query) Then
  1115. __AccessErrorNotify("Error", "_AccessFetchData", "$_AccessQuery_InvalidDataType")
  1116. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  1117. EndIf
  1118.  
  1119. If Not __AccessIsObjType($h_Query, "RecordSet") Then
  1120. __AccessErrorNotify("Error", "_AccessFetchData", "$_AccessQuery_InvalidObjectType")
  1121. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  1122. EndIf
  1123.  
  1124.  
  1125. ; Setup internal error handler to Trap COM errors, turn off error notification
  1126. Local $status = __AccessInternalErrorHandlerRegister()
  1127. If Not $status Then __AccessErrorNotify("Warning", "_AccessRecordAdd", _
  1128. "Cannot register internal error handler, cannot trap COM errors", _
  1129. "Use _AccessErrorHandlerRegister() to register a user error handler")
  1130. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  1131.  
  1132. Local $nField = $h_Query.Fields.Count
  1133. Local $nCnt
  1134. ReDim $aRow[1]
  1135.  
  1136. $aRow[0] = $nField
  1137. $n_Err = 1
  1138. For $nCnt = 1 To $nField - 1
  1139. _ArrayAdd($aRow, $h_Query.Fields($nCnt - 1).Value)
  1140. If @error <> 0 Then ;$_AccessStatus_ComError
  1141. $n_Err = 0
  1142. If $AccessComErrorNumber = -2147352567 Then ; No current record.
  1143. ExitLoop
  1144. EndIf
  1145. EndIf
  1146. Next
  1147.  
  1148. ; restore error notify and error handler status
  1149. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  1150. __AccessInternalErrorHandlerDeRegister()
  1151.  
  1152. Return $n_Err
  1153. EndFunc ;==>_AccessFetchData
  1154. ; #FUNCTION# ====================================================================================================================
  1155. ; Name ..........: _AccessSelectQuery
  1156. ; Description ...: Open a query
  1157. ; Syntax ........: _AccessSelectQuery(Byref $o_DataBaseObject, $sQuery, Byref $h_Query)
  1158. ; Parameters ....: $o_DataBaseObject - [in/out] An unknown value.
  1159. ; $sQuery - A string value.
  1160. ; $h_Query - [in/out] A handle value to Query
  1161. ; Return values .: Sucess Retrns 1, On Failure - Returns 0
  1162. ; Author ........: Ayman Henry
  1163. ; Modified ......:
  1164. ; Remarks .......:
  1165. ; Related .......:
  1166. ; Link ..........:
  1167. ; Example .......: No
  1168. ; ===============================================================================================================================
  1169. Func _AccessSelectQuery(ByRef $o_DataBaseObject, $sQuery, ByRef $h_Query)
  1170. Local $n_Err = 0
  1171. $sQuery = StringStripWS($sQuery, 3)
  1172.  
  1173. If StringLower((StringLeft($sQuery, 4))) <> "sele" Then
  1174. Return 0
  1175. EndIf
  1176.  
  1177. If Not IsObj($o_DataBaseObject) Then
  1178. __AccessErrorNotify("Error", "_AccessSelectQuery", "$_AccessQuery_InvalidDataType")
  1179. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  1180. EndIf
  1181.  
  1182. If Not __AccessIsObjType($o_DataBaseObject, "database") Then
  1183. __AccessErrorNotify("Error", "_AccessSelectQuery", "$_AccessQuery_InvalidObjectType")
  1184. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  1185. EndIf
  1186. $n_Err = 1
  1187. ; Setup internal error handler to Trap COM errors, turn off error notification
  1188. Local $status = __AccessInternalErrorHandlerRegister()
  1189. If Not $status Then __AccessErrorNotify("Warning", "_AccessRecordAdd", _
  1190. "Cannot register internal error handler, cannot trap COM errors", _
  1191. "Use _AccessErrorHandlerRegister() to register a user error handler")
  1192. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  1193.  
  1194. $h_Query = $o_DataBaseObject.OpenRecordSet($sQuery) ; dbSeeChanges Generates a run-time error if another user is changing data you are editing (Microsoft Access workspaces only).
  1195. If @error <> 0 Then ;$_AccessStatus_ComError
  1196. $n_Err = 0
  1197. EndIf
  1198.  
  1199. ; restore error notify and error handler status
  1200. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  1201. __AccessInternalErrorHandlerDeRegister()
  1202.  
  1203. Return $n_Err
  1204. EndFunc ;==>_AccessSelectQuery
  1205. ; #FUNCTION# ====================================================================================================================
  1206. ; Name ..........: _AccessRecordMove
  1207. ; Description ...: Move Record Pointer Forward, Backward, First or last
  1208. ; Syntax ........: _AccessRecordMove (Byref $h_Query, $nNum[, $b_GoFirst = True ])
  1209. ; Parameters ....: $h_Query - [in/out] A handle value.
  1210. ; $nNum - A floating point number value. If positive Number, mover forward, negative to backward, if 0 (see $b_GoFirst)
  1211. ; $b_GoFirst - [optional] A binary value. Default is True. used only if $nNum is Zero, then go to first Record. otherwise go to last record
  1212. ; Return values .: Sucess Retrns 1, On Failure - Returns 0
  1213. ; Author ........: Ayman Henry
  1214. ; Modified ......:
  1215. ; Remarks .......:
  1216. ; Related .......:
  1217. ; Link ..........:
  1218. ; Example .......: No
  1219. ; ===============================================================================================================================
  1220. Func _AccessRecordMove(ByRef $h_Query, $nNum, $b_GoFirst = True)
  1221. Local $n_Err = 0
  1222. If Not IsObj($h_Query) Then
  1223. __AccessErrorNotify("Error", "_AccessFetchData", "$_AccessQuery_InvalidDataType")
  1224. Return SetError($_AccessStatus_InvalidDataType, 1, 0)
  1225. EndIf
  1226.  
  1227. If Not __AccessIsObjType($h_Query, "RecordSet") Then
  1228. __AccessErrorNotify("Error", "_AccessFetchData", "$_AccessQuery_InvalidObjectType")
  1229. Return SetError($_AccessStatus_InvalidObjectType, 1, 0)
  1230. EndIf
  1231.  
  1232. $n_Err = 1
  1233. ; Setup internal error handler to Trap COM errors, turn off error notification
  1234. Local $status = __AccessInternalErrorHandlerRegister()
  1235. If Not $status Then __AccessErrorNotify("Warning", "_AccessRecordAdd", _
  1236. "Cannot register internal error handler, cannot trap COM errors", _
  1237. "Use _AccessErrorHandlerRegister() to register a user error handler")
  1238. Local $f_NotifyStatus = _AccessErrorNotify() ; save current error notify status
  1239. $nNum = Number($nNum)
  1240.  
  1241. If $nNum = 0 Then
  1242. If $b_GoFirst = True Then
  1243. $h_Query.MoveFirst
  1244. Else
  1245. $h_Query.MoveLast
  1246. EndIf
  1247. Else
  1248. $h_Query.Move($nNum)
  1249. EndIf
  1250.  
  1251. If @error <> 0 Then ;$_AccessStatus_ComError
  1252. $n_Err = 0
  1253. EndIf
  1254.  
  1255. If $n_Err <> 0 Then
  1256. $h_Query.Move(0) ; check if there is current rec.
  1257. If @error <> 0 Then $n_Err = 0
  1258. EndIf
  1259.  
  1260. ; restore error notify and error handler status
  1261. _AccessErrorNotify($f_NotifyStatus) ; restore notification status
  1262. __AccessInternalErrorHandlerDeRegister()
  1263. Return $n_Err
  1264. EndFunc ;==>_AccessRecordMove
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement