Results 1 to 14 of 14

Thread: ADO data Control

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    ADO data Control

    Hello, I have an ADODC on my form. I want to populate the RecordSource etc. through coding
    i also want to be able to Add, Update and Delete records to it in code
    This is what I have so far
    VB Code:
    1. Private conn As adoDB.Connection
    2. Private rs As adoDB.Recordset
    3.  
    4. Private Sub cmdBack_Click()
    5. [b]adoDBHorse.Recordset.MovePrevious[/b]
    6. End Sub
    7.  
    8. Private Sub cmdNext_Click()
    9. [b]adoDBHorse.Recordset.MoveNext[/b]
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13.  
    14.     Set conn = New adoDB.Connection
    15.     ' Point this to NorthWind.mdb sample database.
    16.     conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    17.               "Data Source=" & CurDir & "\ST.mdb"
    18.     Set rs = New adoDB.Recordset
    19.     rs.CursorLocation = adUseClient
    20.     Set rs.ActiveConnection = conn
    21.     rs.Properties("IRowsetIdentity").Value = True
    22.     rs.Open "HorseDet", , adOpenKeyset, adLockOptimistic, adCmdTableDirect
    23.     Set adoDBHorse.Recordset = rs
    24.  
    25.  
    26. [b]txtHorseName.DataField = adoDBHorse.Recordset("strHorseName")[/b]
    27. End Sub
    28.  
    29. Private Sub txtHorseName_LostFocus()
    30. adoDBHorse.Recordset.AddNew
    31.  
    32. End Sub
    But I keep getting errors on the bolded lines saying that Either BOF and EOF are true.

    How can I fix this ?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ADO data Control

    I can easily show you how to do what you need to do through VB and SQL code, but my memory of using data controls is, putting it politely, limited.

    What is the purpose of adbDBHorse and why are you using it instead of rs which you have already declared as a recordset?

  3. #3
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: ADO data Control

    GrimmReaper I would urge you to move away from using the ADODC , I consider it leads to bad/poor programms. Instead use ADO only and use a collection class structure to store and iterate through the retrieved data.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: ADO data Control

    Quote Originally Posted by Hack
    What is the purpose of adbDBHorse and why are you using it instead of rs which you have already declared as a recordset?
    Hello Hack!, thanx.
    Well I honestlty can't tell you anymore (i've confused myself soo much with this small app already!)
    So, do you suggest I should use
    rs.MoveNext
    and
    rs.MovePrevious
    What about the textBox's datafield ?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: ADO data Control

    Hello BombDrop, thanx for the links!

    I already have a collection class, but I don't know how to incorporate the database.
    I only have 1 field in 1 table - the reason for this, is to store the names (instead of typing in over again) - I was considering a textfile (but how can I read one line at a time, click next, read another etc.)
    I'm attaching my project with - it's quite small
    I just need a way to store and retrieve the HorseName.
    Attached Files Attached Files

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ADO data Control

    Is there a reason that you want to display them one at a time as opposed to reading them all in and displaying them in a listbox or a listview?

  7. #7
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: ADO data Control

    Below is an example of a collection class getting data in from a recordset via the Load method, notice i have changed the standard Add Method to accept only the object of the class.
    VB Code:
    1. Option Explicit
    2.  
    3. 'local variable to hold collection
    4. Private m_colClients As Collection
    5.  
    6. Private Sub Add(ByVal Client As clsClient)
    7.  
    8.     'Add the object to the collection
    9.     m_colClients.Add Client, Client.clientshort
    10.  
    11. End Sub
    12.  
    13. Public Property Get Item(vntIndexKey As Variant) As clsClient
    14.     'used when referencing an element in the collection
    15.     'vntIndexKey contains either the Index or Key to the collection,
    16.     'this is why it is declared as a Variant
    17.     'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
    18.     Set Item = m_colClients(vntIndexKey)
    19. End Property
    20.  
    21.  
    22.  
    23. Public Property Get Count() As Long
    24.     'used when retrieving the number of elements in the
    25.     'collection. Syntax: Debug.Print x.Count
    26.     Count = m_colClients.Count
    27. End Property
    28.  
    29.  
    30. Public Sub Remove(vntIndexKey As Variant)
    31.     'used when removing an element from the collection
    32.     'vntIndexKey contains either the Index or Key, which is why
    33.     'it is declared as a Variant
    34.     'Syntax: x.Remove(xyz)
    35.  
    36.  
    37.     m_colClients.Remove vntIndexKey
    38. End Sub
    39.  
    40.  
    41. Public Property Get NewEnum() As IUnknown
    42.     'this property allows you to enumerate
    43.     'this collection with the For...Each syntax
    44.     Set NewEnum = m_colClients.[_NewEnum]
    45. End Property
    46.  
    47.  
    48. Private Sub Class_Initialize()
    49.     'creates the collection when this class is created
    50.     Set m_colClients = New Collection
    51. End Sub
    52.  
    53.  
    54. Private Sub Class_Terminate()
    55.     'destroys collection when this class is terminated
    56.     Set m_colClients = Nothing
    57. End Sub
    58. '======================================================================
    59. 'Procedure  :Load (Sub)
    60. 'Date       :28/09/2004
    61. 'InPut      :
    62. 'Returns    :
    63. 'Author     :© BombDrop
    64. 'Purpose    :Will load the collection with Client information
    65. '======================================================================
    66. Public Sub Load()
    67.  
    68.     On Error GoTo Load_Error
    69.  
    70.     Dim conClient As ADODB.Connection
    71.     Dim cmdClient As ADODB.Command
    72.     Dim recClient As ADODB.Recordset
    73.     Dim objClient As clsClient
    74.  
    75.     Dim strConnectionString As String
    76.  
    77.     Set conClient = New ADODB.Connection
    78.     Set cmdClient = New ADODB.Command
    79.  
    80.    
    81.        
    82.  
    83.     'Set conClient properties and open
    84.     conClient.ConnectionString = CONECTIONSTRING
    85.    
    86.     conClient.CursorLocation = adUseClient
    87.     conClient.Open
    88.  
    89.  
    90.     'set and Execute the command
    91.     cmdClient.CommandText = "procDemoClient"
    92.     Set cmdClient.ActiveConnection = conClient
    93.     Set recClient = cmdClient.Execute
    94.  
    95. DoEvents
    96.     Do While Not recClient.EOF
    97.         Set objClient = New clsClient
    98.  
    99.         objClient.addressref = recClient!addressref
    100.         objClient.analysisarea = recClient!analysisarea
    101.         objClient.clientgroupref = recClient!clientgroupref
    102.         objClient.clientname = recClient!clientname & ""
    103.         objClient.ClientRef = recClient!ClientRef
    104.         objClient.clientshort = recClient!clientshort & ""
    105.         objClient.Comments = recClient!Comments & ""
    106.         objClient.contact1 = recClient!contact1 & ""
    107.         objClient.email1a = recClient!email1a & ""
    108.         objClient.fax1 = recClient!fax1 & ""
    109.         objClient.handler1 = recClient!handler1 & ""
    110.         objClient.handler2 = recClient!handler2 & ""
    111.         objClient.handler3 = recClient!handler3 & ""
    112.         objClient.joindate = recClient!joindate & ""
    113.         objClient.leavingdate = recClient!leavingdate & ""
    114.         objClient.namesref = recClient!namesref
    115.         objClient.salutation = recClient!salutation & ""
    116.         objClient.telephone1 = recClient!telephone1 & ""
    117.         objClient.usamemo1 = recClient!usamemo1 & ""
    118.         objClient.usamemo2 = recClient!usamemo2 & ""
    119.         objClient.Address1 = recClient!Address1 & ""
    120.         objClient.Address2 = recClient!Address2 & ""
    121.         objClient.Address3 = recClient!Address3 & ""
    122.         objClient.Address4 = recClient!Address4 & ""
    123.         objClient.Address5 = recClient!Address5 & ""
    124.         objClient.Address6 = recClient!Address6 & ""
    125.         objClient.Postcode = recClient!Postcode & ""
    126.  
    127.         Add objClient
    128.         recClient.MoveNext
    129.     Loop 'While Not recClient.EOF
    130.  
    131.  
    132.  
    133.     'DisconClientect recClientordset
    134.     Set recClient.ActiveConnection = Nothing
    135.  
    136.     'Close conClientection
    137.     conClient.Close
    138.  
    139.     'Destory objects
    140.     Set conClient = Nothing
    141.     Set cmdClient = Nothing
    142.     Set recClient = Nothing
    143.     Set objClient = Nothing
    144.  
    145.  
    146.  
    147.     GoTo CleanExit:
    148.  
    149. Load_Error:
    150.  
    151.     MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & vbCr & _
    152.         "Found In Class Module: Clients " & vbCr & "Found In Procedure: Load" & _
    153.         vbCr & IIf(Erl > 0, "Found In Line:" & Erl, ""), vbCritical, _
    154.         "Error Occurred"
    155.  
    156.     Call LogError("Clients:Load", Err.Description, Err.Number, Erl)
    157.  
    158. CleanExit:
    159.     On Error GoTo 0
    160.  
    161. End Sub

    hope this helps!!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: ADO data Control

    Quote Originally Posted by Hack
    Is there a reason that you want to display them one at a time as opposed to reading them all in and displaying them in a listbox or a listview?
    Wow, you guys are amazing!
    yes, there is a reason - I edit one Horse at a time, I click next, then edit the next one

    Thanx bombdrop, I'll quickly have a lokk

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: ADO data Control

    Hello again, I've tried inserting the code, but everywhere I put it, nothing happens
    How can I incorporate it in my Collection or class ?
    I'm still very new to this

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ADO data Control

    Where did you put it? In a class module or a .BAS module?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: ADO data Control

    I put it in my Class Module - is that wrong ?

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ADO data Control

    Quote Originally Posted by GrimmReaper
    I put it in my Class Module - is that wrong ?
    No. Based on his code I believe that to be exactly right. However, you say nothing happens when you try to use it?

    How are you calling it?

  13. #13
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: ADO data Control

    Here is a very quick and dirty example using nwind DB


    Hope this helps!!
    Attached Files Attached Files

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: ADO data Control

    Hello again guys!
    Bombdrop:
    Based on your example, I've tried to implement the same - everything looks ok, but, when the Form Loads I get this error:
    "Object Does Not Support this property or method"
    on this line:
    txtHorseName.Text = colHorse(FrmIndex).HorseName

    Waht am I doing wrong (again)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width