Results 1 to 8 of 8

Thread: [RESOLVED] ADO Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Resolved [RESOLVED] ADO Help

    i read the ADO Beginners Tutorial by beacon. i still dont undertand. is there easier tutorial or can someone help me please? thank you

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: ADO Help

    Here is my quick attempt at a small ADO tutorial/demo. Sure you may need to add more error handling/trapping but this should give you the basics so you can get a feel for how it all works.

    Assumes you wanted VB 6 code and Access 2000-2003
    Access 2002-2003 compatible database attached
    Change the path to the db to reflect the location on your system
    This demo functions just like Access Forms do. Click Add first to set it in editmode. Then add the text and cick update.


    vb Code:
    1. Option Explicit
    2. 'Add a reference to MS ActiveX Data Objects x.x Library
    3. 'Declare our connection and recordset objects
    4. Private oCnn As ADODB.Connection
    5. Private oRs As ADODB.Recordset
    6.  
    7. Private Sub Form_Load()
    8.     On Error GoTo MyError
    9.     Dim sSQL As String
    10.     'instanciate the connection object variable
    11.     Set oCnn = New ADODB.Connection
    12.     'Set the connection string to our designated database (2000-2003)
    13.     oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\VB-Guru\Documents\RobDog888.mdb;User Id=admin;Password=;"
    14.     'Open the connection to the db
    15.     oCnn.Open
    16.     'Setup the sql statement that will be our recordset's datasource (get last record)
    17.     'Assumes a Table1 with only a single Field of Field1 name
    18.     sSQL = "SELECT Field1 FROM Table1 ORDER BY Field1 DESC"
    19.     'Instanciate the recordset object variable
    20.     Set oRs = New ADODB.Recordset
    21.     'Open the recordset
    22.     oRs.Open sSQL, oCnn, adOpenKeyset, adLockOptimistic, adCmdText
    23.     'Test for returned record(s)
    24.     If oRs.BOF = False And oRs.EOF = False Then
    25.         Text1.Text = oRs.Fields("Field1").Value
    26.     Else
    27.         MsgBox "No Record(s) found"
    28.     End If
    29.     Exit Sub
    30. MyError:
    31.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    32. End Sub
    33.  
    34. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    35.     'Close and clean up object variables
    36.     If TypeName(oRs) <> "Nothing" Then
    37.         If oRs.State = adStateOpen Then oRs.Close
    38.     End If
    39.     If TypeName(oCnn) <> "Nothing" Then
    40.         If oCnn.State = adStateOpen Then oCnn.Close
    41.     End If
    42.     Set oRs = Nothing
    43.     Set oCnn = Nothing
    44. End Sub
    45.  
    46. Private Sub cmdBOF_Click()
    47.     If oRs.BOF = False Then
    48.         oRs.MoveFirst
    49.         Text1.Text = oRs.Fields("Field1").Value
    50.     Else
    51.         Text1.Text = vbNullString
    52.     End If
    53. End Sub
    54.  
    55. Private Sub cmdEOF_Click()
    56.     If oRs.EOF = False Then
    57.         oRs.MoveLast
    58.         Text1.Text = oRs.Fields("Field1").Value
    59.     Else
    60.         Text1.Text = vbNullString
    61.     End If
    62. End Sub
    63.  
    64. Private Sub cmdNext_Click()
    65.     If oRs.EOF = False Then
    66.         oRs.MoveNext
    67.         If oRs.EOF = False Then
    68.             Text1.Text = oRs.Fields("Field1").Value
    69.         Else
    70.             oRs.MovePrevious
    71.         End If
    72.     End If
    73. End Sub
    74.  
    75. Private Sub cmdPrevious_Click()
    76.     If oRs.BOF = False Then
    77.         oRs.MovePrevious
    78.         If oRs.BOF = False Then
    79.             Text1.Text = oRs.Fields("Field1").Value
    80.         Else
    81.             oRs.MoveNext
    82.         End If
    83.     End If
    84. End Sub
    85.  
    86. Private Sub cmdAdd_Click()
    87.     Text1.Text = vbNullString
    88.     'Only allow one addnew at a time until the update button is clicked
    89.     If oRs.BOF = True And oRs.EOF = True Then
    90.         oRs.AddNew
    91.     Else
    92.         If oRs.EditMode = adEditNone Then
    93.             oRs.AddNew
    94.         End If
    95.     End If
    96. End Sub
    97.  
    98. Private Sub cmdDelete_Click()
    99.     oCnn.Execute "DELETE Table1 FROM Table1 WHERE Field1 = '" & Text1.Text & "';"
    100.     'Refresh the recordset with the deleted data
    101.     oRs.Requery
    102.     'Check where we should position the recordset so it displays the data correctly in the textbox
    103.     cmdBOF_Click
    104. End Sub
    105.  
    106. Private Sub cmdUpdate_Click()
    107.     oRs.Fields("Field1").Value = Trim$(Text1.Text)
    108.     oRs.Update
    109.     'Refresh the recordset with the updated data
    110.     oRs.Requery
    111. End Sub

    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by RobDog888; Mar 4th, 2007 at 05:20 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: ADO Help

    Quote Originally Posted by BrainA
    i read the ADO Beginners Tutorial by beacon. i still dont undertand. is there easier tutorial or can someone help me please? thank you
    That tutorial is as basic as it gets.

    What specifically don't you understand?

    Did RobDog888's example help?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Re: ADO Help

    Quote Originally Posted by RobDog888
    Here is my quick attempt at a small ADO tutorial/demo. Sure you may need to add more error handling/trapping but this should give you the basics so you can get a feel for how it all works.

    Assumes you wanted VB 6 code and Access 2000-2003
    Access 2002-2003 compatible database attached
    Change the path to the db to reflect the location on your system
    This demo functions just like Access Forms do. Click Add first to set it in editmode. Then add the text and cick update.


    [highlight=vb]Option Explicit
    'Add a reference to MS ActiveX Data Objects x.x Library
    'Declare our connection and recordset objects
    Private oCnn As ADODB.Connection
    Private oRs As ADODB.Recordset


    thx for the example..but when i tried to run it says "Compile Error:
    User-defined type not defined" and it highlights "oCnn As ADODB.Connection" i did change the location and it doesnt run? what should i do?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: ADO Help

    Did you add a reference to ADO linke shown in the comments?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Re: ADO Help

    Quote Originally Posted by RobDog888
    Did you add a reference to ADO linke shown in the comments?
    I believe so. can you show me where they are please? thanks

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: ADO Help

    Project > References > check "MS ActiveX 2.x Data Objects Library > click OK.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Re: ADO Help

    Quote Originally Posted by RobDog888
    Project > References > check "MS ActiveX 2.x Data Objects Library > click OK.
    thx man it works..ill figure the rest out myself..thanks much

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