Advertisement
nandordudas

Test databse

May 17th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Sub a()
  5.  
  6.     On Error GoTo Catch
  7.  
  8.     Dim d As New Database, r As Object, i As Variant
  9.  
  10.     DoCmd.SetWarnings False
  11.  
  12.     Application.SetOption "ANSI Query Mode", True
  13.  
  14.     DoCmd.RunSQL "CREATE TABLE tblSample (" _
  15.         & "SampleId AUTOINCREMENT(1, 1) CONSTRAINT PrimaryKey PRIMARY KEY, " _
  16.         & "LotNumber VARCHAR NOT NULL, " _
  17.         & "ItemCode INTEGER NOT NULL, " _
  18.         & "ItemNumber INTEGER NOT NULL, " _
  19.         & "ProductName VARCHAR NOT NULL, " _
  20.         & "SampleQuantity DECIMAL(8, 3) NOT NULL, " _
  21.         & "SampleUnit VARCHAR NOT NULL, " _
  22.         & "SamplingDate DATETIME NOT NULL, " _
  23.         & "ExpiryDate DATETIME, " _
  24.         & "LocationIdFk INTEGER, " _
  25.         & "CONSTRAINT ItemNumberKey UNIQUE (ItemNumber), " _
  26.         & "CONSTRAINT LotNumberItemCodeKey UNIQUE (LotNumber, ItemCode))"
  27. '   tblErrorLog
  28. '   Id|3010|A következő tábla már létezik: 'tblSample'.|Sub a|TestDatabase
  29. '   NOW|Environ$("USERNAME")|Environ$("COMPUTERNAME")
  30.  
  31.     DoCmd.RunSQL "CREATE TABLE tblRole (" & _
  32.         "RoleId AUTOINCREMENT(1, 1) CONSTRAINT PrimaryKey PRIMARY KEY, " & _
  33.         "RoleName VARCHAR NOT NULL, " & _
  34.         "Description VARCHAR, " & _
  35.         "CONSTRAINT RoleNamekey UNIQUE (RoleName))"
  36. '   tblErrorLog
  37. '   Id|3010|A következő tábla már létezik: 'tblRole'.|Sub a|TestDatabase
  38. '   NOW|Environ$("USERNAME")|Environ$("COMPUTERNAME")
  39.  
  40.     With d
  41.         .Query = "INSERT INTO tblRole (RoleName) VALUES (:RoleName)"
  42.  
  43.         .Bind ":RoleName", Format$(Now, "yyyymmddHhNnSs")
  44.         .Bind ":RoleName", Format$(Now, "yyyymmddHhNnSs")
  45. '       tblErrorLog
  46. '       Id|-2147220991|Parameter exists(:RoleName)|Sub Bind|TestDatabase
  47. '       NOW|Environ$("USERNAME")|Environ$("COMPUTERNAME")
  48.  
  49.         Set r = .ResultSet
  50.  
  51.         If CBool(.AffectedRows) Then _
  52.             Debug.Print .AffectedRows & " row inserted" & vbCrLf
  53.  
  54. '   Query:
  55. '       INSERT INTO tblRole (RoleName) VALUES (:RoleName)
  56. '   Parameters:
  57. '       : RoleName = 20160510211028
  58. '   Transaction initiated
  59. '   Executed: True
  60. '   RecordCount: 0
  61. '   AffectedRows: 1
  62. '   Transaction committed
  63. '
  64. '   1 row inserted
  65.  
  66.         .Query = "SELECT s.LotNumber FROM " & _
  67.             "tblSample s " & _
  68.             "WHERE s.SampleId = :SampleId"
  69.  
  70.         .Bind ":SampleId", 147
  71.  
  72.         Set r = .ResultSet
  73.  
  74.         If CBool(.RecordCount) Then Debug.Print r("LotNumber") & vbCrLf
  75.  
  76. '   Query:
  77. '       SELECT s.LotNumber FROM tblSample s WHERE s.SampleId = :SampleId
  78. '   Parameters:
  79. '       : SampleId = 147
  80. '   Transaction initiated
  81. '   Executed: True
  82. '   RecordCount: 1
  83. '   AffectedRows: 0
  84. '   Transaction committed
  85. '
  86. '   LotNumber
  87.  
  88.         .Query = "SELECT s.LotNumber FROM " & _
  89.             "tblSample s " & _
  90.             "WHERE s.ItemCode = :ItemCode AND s.SamplingDate > :SamplingDate"
  91.  
  92.         .Bind ":ItemCode", 10002430
  93.         .Bind ":SamplingDate", CDate("2015/1/1")
  94.  
  95.         Set r = .ResultSet
  96.  
  97.         If Not CBool(.RecordCount) Then GoTo Finally
  98.  
  99.         For Each i In r
  100.  
  101.             Debug.Print r(i)("LotNumber")
  102.  
  103.             DoEvents
  104.         Next i
  105.  
  106.     End With
  107.  
  108. '   Query:
  109. '       SELECT s.LotNumber FROM tblSample s WHERE s.ItemCode = :ItemCode
  110. '       AND s.SamplingDate > :SamplingDate
  111. '   Parameters:
  112. '       : ItemCode = 10002430
  113. '       :SamplingDate = 2015. 01. 01.
  114. '   Transaction initiated
  115. '   Executed: True
  116. '   RecordCount: 3
  117. '   AffectedRows: 0
  118. '   Transaction committed
  119. '
  120. '   LotNumber
  121. '   ...
  122.  
  123.     GoTo Finally
  124.  
  125. Catch:
  126.     Handle Err.Number, Err.Description, "", "Sub a", _
  127.         VBE.ActiveCodePane.CodeModule.Name
  128.     Resume Next
  129.  
  130. Finally:
  131.     Set d = Nothing
  132.     Set r = Nothing
  133.     Application.SetOption "ANSI Query Mode", False
  134.     DoCmd.SetWarnings True
  135.  
  136. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement