Results 1 to 12 of 12

Thread: [RESOLVED] ADODB Recordset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Philadelphia, PA
    Posts
    120

    Resolved [RESOLVED] ADODB Recordset

    Hello,
    I'm here again for some help.

    Here is what i have,
    VB Code:
    1. Dim ConnectionString As String = "Provider=MSDASQL.1;Password=101098;Data Source=main;Persist Security Info=True"
    2.  
    3.         'Create my objects
    4.         Dim conn As New ADODB.Connection
    5.         Dim cmd As New ADODB.Command
    6.         Dim rs As New ADODB.Recordset
    7.  
    8.         Try
    9.             conn.Open(ConnectionString)
    10.             cmd.ActiveConnection = conn
    11.             cmd.CommandText = "SELECT customername FROM customers"
    12.  
    13.        'make a recordset here
    14.  
    15.         Catch ex As Exception
    16.             Debug.WriteLine(ex.Message.ToString)
    17.  
    18.         End Try

    I need help build a recordset from that query and displaying it in a datagrid or a flexgrid.

    Thanks in advance!

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

    Re: ADODB Recordset

    Why not use ADO.NET and a DataGrid?
    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

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Philadelphia, PA
    Posts
    120

    Re: ADODB Recordset

    could you please provide an example?

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

    Re: ADODB Recordset

    You need to import the SQL client namespace. Then set up a connection to your db.
    VB Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Public Function DBConn() As SqlConnection
    4.     '<CONNECT TO SQL DB>
    5.     Dim oSQLCnn As SqlConnection
    6.     Try
    7.         oSQLCnn = New SqlConnection
    8.         oSQLCnn.ConnectionString = "Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=SSPI;" 'Integrated Security
    9.         oSQLCnn.Open()
    10.         DBConn = oSQLCnn
    11.     Catch exSQL As SqlException
    12.         MessageBox.Show(exSQL.Errors.Item(0).Message, exSQL.Errors.Item(0).Number.ToString & ": " & _
    13.         exSQL.Errors.Item(0).Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
    14.         DBConn = Nothing
    15.     Catch ex As Exception
    16.         MessageBox.Show(ex.Message.ToString, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
    17.         DBConn = Nothing
    18.     End Try
    19.     '</CONNECT TO SQL DB>
    20. End Function
    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ADODB Recordset

    All the data access examples in the 101 samples are ADO.NET except the one that explicitly says it uses ADO 2.6. Get the 101 samples from the sticky thread at the top of this very forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: ADODB Recordset

    Then we need to make the connection and populate a datatable.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.     Dim oSQLCnn As SqlConnection
    4.  
    5.     oSQLCnn = DBConn()
    6.     Dim dt As New DataTable
    7.     Dim da As New SqlDataAdapter("SELECT * FROM Table1", oSQLCnn)
    8.     da.Fill(dt)
    9.     Me.DataGrid1.DataSource = dt
    10.     Me.DataGrid1.ReadOnly = True' Or False
    11.     Me.DataGrid1.ColumnHeadersVisible = True
    12.     Me.DataGrid1.CaptionVisible = False
    13.  
    14. End Sub
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Philadelphia, PA
    Posts
    120

    Re: ADODB Recordset

    thanks for ur reply.
    But i have an access database i dont run a sql server. Any way to modify that to work with access?

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

    Re: ADODB Recordset

    Doh! I should have asked first but its not too much different. There is a OLEDBDataAdapter, OLEDBConnection, and OLEDBCommand. Dont have time right now to write an example, leaving for home, but if you cant get it or no one else posts one, then I will.
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Philadelphia, PA
    Posts
    120

    Re: ADODB Recordset

    looking forward to it
    i've spent about 6 hours today trying to do 3 things... and still not much luck

    -display query results in a datagrid or a flexgrid
    -pull field values from a recordset
    -update database

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ADODB Recordset

    Wherever Rob has used "Sql", you would use "OleDb". You would also need to import the System.Data.OleDb namespace, where for Rob's code you would import SqlClient. You'll have to change the connection string for OleDb as well. Try www.connectionstrings.com
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Philadelphia, PA
    Posts
    120

    Re: ADODB Recordset

    VB Code:
    1. Dim ConnString As String = "Provider=MSDASQL.1;Password=101098;Data Source=main;Persist Security Info=True"
    2.  
    3.         Dim Query As String = "SELECT customer FROM customers "
    4.  
    5.         'Create a connection object to connect to the database
    6.         Dim conn As New OleDbConnection(ConnString)
    7.         'Create a command object to run queries against the database
    8.         Dim cmd As New OleDbCommand(Query, conn)
    9.  
    10.         'open the connection to the database
    11.         conn.Open()
    12.  
    13.  
    14.  
    15.         'so now how do i execute the whole thing and bind the data to a datagrid
    16.  
    17.  
    18.  
    19.         'close the connection
    20.         conn.Close()

    is this anywhere close? no how would i get a recordset from there and have it be displayed in a datagrid?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ADODB Recordset

    Rob's code is:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.     Dim oSQLCnn As SqlConnection
    4.  
    5.     oSQLCnn = DBConn()
    6.     Dim dt As New DataTable
    7.     Dim da As New SqlDataAdapter("SELECT * FROM Table1", oSQLCnn)
    8.     da.Fill(dt)
    9.     Me.DataGrid1.DataSource = dt
    10.     Me.DataGrid1.ReadOnly = True' Or False
    11.     Me.DataGrid1.ColumnHeadersVisible = True
    12.     Me.DataGrid1.CaptionVisible = False
    13.  
    14. End Sub
    Your code should be to the effect of:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.     Dim con As New OleDb.OleDbConnection("connection string here")
    4.     Dim dt As New DataTable
    5.     Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Table1", con)
    6.     da.Fill(dt) 'Retrieve data into DataTable.
    7.     Me.DataGrid1.DataSource = dt 'Bind DataGrid to DataTable.
    8.     Me.DataGrid1.ReadOnly = True' Or False
    9.     Me.DataGrid1.ColumnHeadersVisible = True
    10.     Me.DataGrid1.CaptionVisible = False
    11.  
    12. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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