home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / dbcexe / names.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-03-30  |  17.8 KB  |  461 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.     MultiUse = -1  'True
  4. End
  5. attribute VB_Name = "Names"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
  11. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  12. '-- Created in part by DBClass Class Generator, copyright (c) 1997 Dew Design, LLC.
  13. Option explicit
  14.  
  15. Private Const m_CLASSNAME = "Names.cls"
  16. '-- The class default SQL statement.
  17. Private Const CLS_SQL = "SELECT Names.* FROM Names"
  18.  
  19. Private m_SQL As String
  20. Private m_WHERE As String
  21. Private m_lFieldSize As Long
  22. Private m_Recordset As Recordset
  23. Private PropertyUpdates As New Collection
  24.  
  25. '--------------------------------------------------------------------------------------------------------------------------'
  26. '----------   Class Properties   -------------------------------'
  27. '--------------------------------------------------------------------------------------------------------------------------'
  28. Public Property Get ID() As Long
  29.     On Error Resume Next
  30.     ID = PropertyUpdates.Item("ID").Value
  31.     If Err.Number = 0 Then Exit Property
  32.     Err.Number = 0
  33.     On Error GoTo Error_Get_ID
  34.     If Not IsNull(m_Recordset!ID) Then _
  35.     ID = m_Recordset!ID
  36.     Exit Property
  37. Error_Get_ID:
  38.     If Not oError(Err.Number, m_CLASSNAME & " : Get ID") Then Resume
  39. End Property
  40. '--------------------------------------------------------------------------------------------------------------------------'
  41. Public Property Let IsNew(ByVal vData As Boolean)
  42.     MarkPropertyUpdated "IsNew", "IsNew", vData
  43. End Property
  44. Public Property Get IsNew() As Boolean
  45.     On Error Resume Next
  46.     IsNew = PropertyUpdates.Item("IsNew").Value
  47.     If Err.Number = 0 Then Exit Property
  48.     Err.Number = 0
  49.     On Error GoTo Error_Get_IsNew
  50.     If Not IsNull(m_Recordset!IsNew) Then _
  51.     IsNew = m_Recordset!IsNew
  52.     Exit Property
  53. Error_Get_IsNew:
  54.     If Not oError(Err.Number, m_CLASSNAME & " : Get IsNew") Then Resume
  55. End Property
  56. '--------------------------------------------------------------------------------------------------------------------------'
  57. Public Property Let lname(ByVal vData As String)
  58.     m_lFieldSize = m_Recordset!lname.Size
  59.     vData = Trim(vData)
  60.     If Len(vData) > m_lFieldSize Then
  61.          vData = Left(vData, m_lFieldSize)
  62.          MsgBox "This Field is limited to "& m_lFieldSize & " characters in length."
  63.     End If
  64.     MarkPropertyUpdated "lname", "lname", vData
  65. End Property
  66. Public Property Get lname() As String
  67.     On Error Resume Next
  68.     lname = PropertyUpdates.Item("lname").Value
  69.     If Err.Number = 0 Then Exit Property
  70.     Err.Number = 0
  71.     On Error GoTo Error_Get_lname
  72.     If Not IsNull(m_Recordset!lname) Then _
  73.     lname = m_Recordset!lname
  74.     Exit Property
  75. Error_Get_lname:
  76.     If Not oError(Err.Number, m_CLASSNAME & " : Get lname") Then Resume
  77. End Property
  78. '--------------------------------------------------------------------------------------------------------------------------'
  79. Public Property Let fname(ByVal vData As String)
  80.     m_lFieldSize = m_Recordset!fname.Size
  81.     vData = Trim(vData)
  82.     If Len(vData) > m_lFieldSize Then
  83.          vData = Left(vData, m_lFieldSize)
  84.          MsgBox "This Field is limited to "& m_lFieldSize & " characters in length."
  85.     End If
  86.     MarkPropertyUpdated "fname", "fname", vData
  87. End Property
  88. Public Property Get fname() As String
  89.     On Error Resume Next
  90.     fname = PropertyUpdates.Item("fname").Value
  91.     If Err.Number = 0 Then Exit Property
  92.     Err.Number = 0
  93.     On Error GoTo Error_Get_fname
  94.     If Not IsNull(m_Recordset!fname) Then _
  95.     fname = m_Recordset!fname
  96.     Exit Property
  97. Error_Get_fname:
  98.     If Not oError(Err.Number, m_CLASSNAME & " : Get fname") Then Resume
  99. End Property
  100. '--------------------------------------------------------------------------------------------------------------------------'
  101. Public Property Let address(ByVal vData As String)
  102.     m_lFieldSize = m_Recordset!address.Size
  103.     vData = Trim(vData)
  104.     If Len(vData) > m_lFieldSize Then
  105.          vData = Left(vData, m_lFieldSize)
  106.          MsgBox "This Field is limited to "& m_lFieldSize & " characters in length."
  107.     End If
  108.     MarkPropertyUpdated "address", "address", vData
  109. End Property
  110. Public Property Get address() As String
  111.     On Error Resume Next
  112.     address = PropertyUpdates.Item("address").Value
  113.     If Err.Number = 0 Then Exit Property
  114.     Err.Number = 0
  115.     On Error GoTo Error_Get_address
  116.     If Not IsNull(m_Recordset!address) Then _
  117.     address = m_Recordset!address
  118.     Exit Property
  119. Error_Get_address:
  120.     If Not oError(Err.Number, m_CLASSNAME & " : Get address") Then Resume
  121. End Property
  122. '--------------------------------------------------------------------------------------------------------------------------'
  123. Public Property Let city(ByVal vData As String)
  124.     m_lFieldSize = m_Recordset!city.Size
  125.     vData = Trim(vData)
  126.     If Len(vData) > m_lFieldSize Then
  127.          vData = Left(vData, m_lFieldSize)
  128.          MsgBox "This Field is limited to "& m_lFieldSize & " characters in length."
  129.     End If
  130.     MarkPropertyUpdated "city", "city", vData
  131. End Property
  132. Public Property Get city() As String
  133.     On Error Resume Next
  134.     city = PropertyUpdates.Item("city").Value
  135.     If Err.Number = 0 Then Exit Property
  136.     Err.Number = 0
  137.     On Error GoTo Error_Get_city
  138.     If Not IsNull(m_Recordset!city) Then _
  139.     city = m_Recordset!city
  140.     Exit Property
  141. Error_Get_city:
  142.     If Not oError(Err.Number, m_CLASSNAME & " : Get city") Then Resume
  143. End Property
  144. '--------------------------------------------------------------------------------------------------------------------------'
  145. Public Property Let state(ByVal vData As String)
  146.     m_lFieldSize = m_Recordset!state.Size
  147.     vData = Trim(vData)
  148.     If Len(vData) > m_lFieldSize Then
  149.          vData = Left(vData, m_lFieldSize)
  150.          MsgBox "This Field is limited to "& m_lFieldSize & " characters in length."
  151.     End If
  152.     MarkPropertyUpdated "state", "state", vData
  153. End Property
  154. Public Property Get state() As String
  155.     On Error Resume Next
  156.     state = PropertyUpdates.Item("state").Value
  157.     If Err.Number = 0 Then Exit Property
  158.     Err.Number = 0
  159.     On Error GoTo Error_Get_state
  160.     If Not IsNull(m_Recordset!state) Then _
  161.     state = m_Recordset!state
  162.     Exit Property
  163. Error_Get_state:
  164.     If Not oError(Err.Number, m_CLASSNAME & " : Get state") Then Resume
  165. End Property
  166. '--------------------------------------------------------------------------------------------------------------------------'
  167. Public Property Let Filter(vData As String)
  168.     m_Where = vData
  169. End Property
  170. Public Property Get Filter() As String
  171.     Filter = m_Where
  172. End Property
  173. '--------------------------------------------------------------------------------------------------------------------------'
  174. Public Property Let SQL(vData As String)
  175.     m_SQL = vData
  176. End Property
  177. Public Property Get SQL() As String
  178.     SQL = m_SQL
  179. End Property
  180. '--------------------------------------------------------------------------------------------------------------------------'
  181. '----------   Class Methods   ------------------------------'
  182. '--------------------------------------------------------------------------------------------------------------------------'
  183. Public Function Add(Optional addIsNew As String) As Boolean
  184.     '-- Adjust the function parameters to whichever fields need to be passed
  185.     '-- Be sure to include default record specifics below
  186.     Dim iID As Long
  187.     On Error GoTo Error_Add
  188.     CancelUpdate
  189.     BeginTrans
  190.          With m_Recordset
  191.               .AddNew
  192.               '-- Default record specifics here
  193.               '!IsNew = addIsNew
  194.               !IsNew = True
  195.               iID = !ID
  196.               .Update
  197.          End With
  198.     CommitTrans
  199.     Add = True
  200.     If Not FindFirst("ID = " & iID) Then _
  201.          MsgBox "Unable to find the new record!"
  202.     Exit Function
  203. Error_Add:
  204.     If Not oError(Err.Number, m_CLASSNAME & " : Add") Then Resume
  205.     Add = False
  206. End Function
  207. '--------------------------------------------------------------------------------------------------------------------------'
  208. Public Sub CancelUpdate()
  209.     Do While PropertyUpdates.Count <> 0
  210.          PropertyUpdates.Remove (1)
  211.     Loop
  212. End Sub
  213.  
  214. '--------------------------------------------------------------------------------------------------------------------------'
  215. Public Function Count() As Long
  216.     On Error GoTo Error_Count
  217.     Dim vBookmark as Variant
  218.     With m_Recordset
  219.          If (Not .EOF) And (Not .BOF) then
  220.               vBookmark = .Bookmark
  221.               .MoveFirst
  222.               .MoveLast
  223.               Count = .RecordCount
  224.               .Bookmark = vBookmark
  225.          End If
  226.     End With
  227.     Exit Function
  228. Error_Count:
  229.     If Not oError(Err.Number, m_CLASSNAME & " : Count") Then Resume
  230. End Function
  231. '--------------------------------------------------------------------------------------------------------------------------'
  232. Public Function Delete(delID As Long) As Boolean
  233.     '-- Be sure to include related records checking here
  234.     On Error GoTo Error_Delete
  235.     CancelUpdate
  236.     BeginTrans
  237.     With m_Recordset
  238.          .FindFirst "ID = " & delID
  239.          If Not .NoMatch Then
  240.               .Delete
  241.               Delete = True
  242.          End If
  243.     End With
  244.     CommitTrans
  245.     Exit Function
  246. Error_Delete:
  247.     If Not oError(Err.Number, m_CLASSNAME & " : Delete") Then Resume
  248.     Delete = False
  249. End Function
  250. '--------------------------------------------------------------------------------------------------------------------------'
  251. Public Sub FillList(ByRef cmbObj As Object, Optional sFilter As String, Optional bBlank As Boolean)
  252.     On Error GoTo Error_FillList
  253.     cmbObj.Clear
  254.     If bBlank Then
  255.          cmbObj.AddItem " "
  256.          cmbObj.ItemData(cmbObj.NewIndex) = 0
  257.     End If
  258.     If IsMissing(sFilter) Or (sFilter = "") Then
  259.          With m_Recordset
  260.               .MoveFirst
  261.               While Not .EOF
  262.                    If Not IsNull(!IsNew) Then
  263.                         cmbObj.AddItem !IsNew
  264.                         cmbObj.ItemData(cmbObj.NewIndex) = !ID
  265.                    End If
  266.                    .MoveNext
  267.               Wend
  268.          End With
  269.     Else
  270.          With m_Recordset
  271.               .FindFirst (sFilter)
  272.               While Not .NoMatch
  273.                    If Not IsNull(!IsNew) Then
  274.                         cmbObj.AddItem !IsNew
  275.                         cmbObj.ItemData(cmbObj.NewIndex) = !ID
  276.                    End If
  277.                    .FindNext (sFilter)
  278.               Wend
  279.          End With
  280.     End If
  281.     Exit Sub
  282. Error_FillList:
  283.     If Not oError(Err.Number, m_CLASSNAME & " : FillList") Then Resume
  284. End Sub
  285. '--------------------------------------------------------------------------------------------------------------------------'
  286. Public Function FindFirst(SearchCrit As String) As Boolean
  287.     On Error GoTo Error_FindFirst
  288.     CancelUpdate
  289.     m_Recordset.FindFirst SearchCrit
  290.     If Not (m_Recordset.NoMatch) then FindFirst = True
  291.     Exit Function
  292. Error_FindFirst:
  293.     If Not oError(Err.Number, m_CLASSNAME & " : FindFirst " & SearchCrit) Then Resume
  294.     FindFirst = False
  295. End Function
  296. '--------------------------------------------------------------------------------------------------------------------------'
  297. Public Function FindLast(SearchCrit As String) As Boolean
  298.     On Error GoTo Error_FindLast
  299.     CancelUpdate
  300.     m_Recordset.FindLast SearchCrit
  301.     If Not (m_Recordset.NoMatch) then FindLast = True
  302.     Exit Function
  303. Error_FindLast:
  304.     If Not oError(Err.Number, m_CLASSNAME & " : FindLast" & SearchCrit) Then Resume
  305.     FindLast = False
  306. End Function
  307. '--------------------------------------------------------------------------------------------------------------------------'
  308. Public Function FindNext(SearchCrit As String) As Boolean
  309.     On Error GoTo Error_FindNext
  310.     CancelUpdate
  311.     m_Recordset.FindNext SearchCrit
  312.     If Not (m_Recordset.NoMatch) then FindNext = True
  313.     Exit Function
  314. Error_FindNext:
  315.     If Not oError(Err.Number, m_CLASSNAME & " : FindNext" & SearchCrit) Then Resume
  316.     FindNext = False
  317. End Function
  318. '--------------------------------------------------------------------------------------------------------------------------'
  319. Public Function FindPrevious(SearchCrit As String) As Boolean
  320.     On Error GoTo Error_FindPrevious
  321.     CancelUpdate
  322.     m_Recordset.FindPrevious SearchCrit
  323.     If Not (m_Recordset.NoMatch) then FindPrevious = True
  324.     Exit Function
  325. Error_FindPrevious:
  326.     If Not oError(Err.Number, m_CLASSNAME & " : FindPrevious" & SearchCrit) Then Resume
  327.     FindPrevious = False
  328. End Function
  329. '--------------------------------------------------------------------------------------------------------------------------'
  330. Private Sub Class_Initialize()
  331.     On Error GoTo Error_Class_Initialize
  332.     '-- Open the recordset based on cls_sql from your database object: dbtest
  333.     m_SQL = CLS_SQL
  334.     m_WHERE = vbNullString
  335.     '-- By default, set the recordset to full table when the class is initialized...
  336.     '-- Comment this out if you wish to filter your query to limit network traffic due to large tables...
  337.     '-- Set m_Recordset = dbtest.OpenRecordset(m_SQL,dbOpenDynaset)
  338.     Exit Sub
  339. Error_Class_Initialize:
  340.     '-- Put extra error stuff here
  341.     If Not oError(Err.Number, m_CLASSNAME & " : Class_Initialize") Then Resume
  342. End Sub
  343. '--------------------------------------------------------------------------------------------------------------------------'
  344. Public Function IsFiltered() As Boolean
  345.     IsFiltered = Not (m_WHERE = vbNullString)
  346. End Function
  347. '--------------------------------------------------------------------------------------------------------------------------'
  348. Private Sub MarkPropertyUpdated(PropertyName As String, RSFieldName As String, vData As Variant)
  349.     On Error Resume Next
  350.     PropertyUpdates.Item(PropertyName).Value = vData
  351.     If Err.Number <> 0 Then
  352.          Dim ObjProperty As New PropertyUpdate
  353.          ObjProperty.Fieldname = RSFieldName
  354.          ObjProperty.Value = vData
  355.          PropertyUpdates.Add ObjProperty, PropertyName
  356.          Err.Number = 0
  357.     End If
  358. End Sub
  359. '--------------------------------------------------------------------------------------------------------------------------'
  360. Public Function MoveFirst() As Boolean
  361.     On Error GoTo Error_MoveFirst
  362.     CancelUpdate
  363.     If Not m_Recordset.BOF Then
  364.          m_Recordset.MoveFirst
  365.          If Not m_Recordset.BOF Then MoveFirst = True
  366.     End If
  367.     Exit Function
  368. Error_MoveFirst:
  369.     If Not oError(Err.Number, m_CLASSNAME & " : MoveFirst") Then Resume
  370.     MoveFirst = False
  371. End Function
  372. '--------------------------------------------------------------------------------------------------------------------------'
  373. Public Function MoveLast() As Boolean
  374.     On Error GoTo Error_MoveLast
  375.     CancelUpdate
  376.     If Not m_Recordset.EOF Then
  377.          m_Recordset.MoveLast
  378.          If Not m_Recordset.EOF Then MoveLast = True
  379.     End If
  380.     Exit Function
  381. Error_MoveLast:
  382.     If Not oError(Err.Number, m_CLASSNAME & " : MoveLast") Then Resume
  383.     MoveLast = False
  384. End Function
  385. '--------------------------------------------------------------------------------------------------------------------------'
  386. Public Function MoveNext() As Boolean
  387.     On Error GoTo Error_MoveNext
  388.     CancelUpdate
  389.     If Not m_Recordset.EOF Then
  390.          m_Recordset.MoveNext
  391.          If m_Recordset.EOF Then
  392.               m_Recordset.MoveLast
  393.          Else
  394.               MoveNext = True
  395.          End If
  396.     End If
  397.     Exit Function
  398. Error_MoveNext:
  399.     If Not oError(Err.Number, m_CLASSNAME & " : MoveNext") Then Resume
  400.     MoveNext = False
  401. End Function
  402. '--------------------------------------------------------------------------------------------------------------------------'
  403. Public Function MovePrevious() As Boolean
  404.     On Error GoTo Error_MovePrevious
  405.     CancelUpdate
  406.     If Not m_Recordset.BOF Then
  407.          m_Recordset.MovePrevious
  408.          If m_Recordset.BOF Then
  409.               m_Recordset.MoveFirst
  410.          Else
  411.               MovePrevious = True
  412.          End If
  413.     End If
  414.     Exit Function
  415. Error_MovePrevious:
  416.     If Not oError(Err.Number, m_CLASSNAME & " : MovePrevious") Then Resume
  417.     MovePrevious = False
  418. End Function
  419. '--------------------------------------------------------------------------------------------------------------------------'
  420. Public Sub Requery()
  421.     On Error GoTo Error_Requery
  422.     CancelUpdate
  423.     Set m_Recordset = dbtest.OpenRecordset(m_SQL & " " & m_WHERE,dbOpenDynaset)
  424.     Exit Sub
  425. Error_Requery:
  426.     If Not oError(Err.Number, m_CLASSNAME & " : Requery") Then Resume
  427. End Sub
  428. '--------------------------------------------------------------------------------------------------------------------------'
  429. Public Sub Save()
  430.     On Error GoTo Error_Save
  431.     If PropertyUpdates.Count = 0 Then Exit Sub
  432.     Dim loProperty  'Loop Object
  433.     Dim Bookmark As Variant
  434.     With m_Recordset
  435.          Bookmark = .Bookmark
  436.          .Edit
  437.          For Each loProperty In PropertyUpdates
  438.               .Fields(loProperty.FieldName) = loProperty.Value
  439.          Next
  440.          .Update
  441.          .Bookmark = Bookmark
  442.     End With
  443.     Set loProperty = Nothing
  444.     CancelUpdate
  445.     Exit Sub
  446. Error_Save:
  447.     If Not oError(Err.Number, m_CLASSNAME & " : Save") Then Resume
  448. End Sub
  449. '--------------------------------------------------------------------------------------------------------------------------'
  450. Private Sub Class_Terminate()
  451.     On Error GoTo Error_Class_Terminate
  452.     Save
  453.  
  454.     m_Recordset.Close
  455.     Set m_Recordset = Nothing
  456.     Exit Sub
  457. Error_Class_Terminate:
  458.     Exit Sub
  459. End Sub
  460. '--------------------------------------------------------------------------------------------------------------------------'
  461.