Results 1 to 4 of 4

Thread: [2008] Data Grid View with Access

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    [2008] Data Grid View with Access

    Good Evening, I am learning DB programming at the moment so all new to it and I have learnt how to connect to a database insert new things delete read a line etc but there is something new I have come across that I don't have a clue in the world.

    I have a Data Grid View on a form and I want to load the entire contents of a table from an access database into it bar a couple of columns so first of all can someone show me how to fill a datagrid view with a table. I guess from there in the execute sting I just name the columns I want to grab.

    Thanks,
    Max
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] Data Grid View with Access

    something like this
    Code:
            Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=c:\MYApp\MyDB.mdb")
            Dim adapter As New OleDbDataAdapter
    
            Dim selCommand As New OleDbCommand
            With selCommand
                .CommandText = "Select Col1,Col2 FROM MYTable"
                .CommandType = CommandType.Text
                .Connection = conn
            End With
            'assign select command to data adapter 
            adapter.SelectCommand = selCommand
            Dim dt As New DataTable("MyTable")
            adapter.Fill(dt)
    
            dvMyGrid.DataSource = dt
    __________________
    Rate the posts that helped you

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    Re: [2008] Data Grid View with Access

    Hi, thanks for the reply but I am getting a little stuck. As I am so new to this I don't have a clue really of what does what and I am trying to merge your code with what I am being taught and getting an error. This is what I have so far:

    Code:
       Dim dbRec As New ADODB.Recordset
        Dim dbObj As New ADODB.Connection
    Form Load:

    Code:
    ConnectDB(strDatabasePath & "Data.mdb")
    
    Dim CMD As New ADODB.Command
            With CMD
                .CommandText = "Select col1,col2,col3,col4 FROM table"
                .CommandType = CommandType.Text
            End With
            'assign select command to data adapter 
            dbObj.SelectCommand = CMD
            Dim dt As New DataTable("table")
            dbObj.Fill(dt)
    
            datagridview.DataSource = dt
    ConnectDB Routine:

    Code:
        Private Sub ConnectDB(ByVal Database As String)
                    Dim intErr As Integer
            Database = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & Database & ";DefaultDir=" & Database
            dbObj.Open(Database)
            Me.Show()
                Call frmSuppliers_Load(Nothing, Nothing)
            End If
        End Sub

    I have edited certain bits to replace column names and table name.

    The error is happening on the line:


    dbObj.SelectCommand = CMD

    The Error:

    No named arguments. (Exception from HRESULT: 0x80020007 (DISP_E_NONAMEDARGS))

    Is there something that is coded wrong because I really don't have a clue what I am doing here lol.

    Thanks,
    Max
    M.Carpenter

    Server Admin for Wurm Online
    Wurm Online - A very unique and original MMO from Code Club AB
    https://www.youtube.com/watch?v=YQlYar2uHWAWurm Trailor


  4. #4
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] Data Grid View with Access

    code that you are using is vb6 code while in .NET there is special namespaces and classes to access the database related object.

    Look at this ADO.NET Tutorial
    __________________
    Rate the posts that helped you

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