Results 1 to 2 of 2

Thread: Question about adding record to database so it shows in datagrid.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    1

    Question Question about adding record to database so it shows in datagrid.

    Hello, I am new to VB6 and to the forums. I am attempting to make a form that allows the user to fill out text boxes that will add a new item to the inventory which is displayed as a datagrid on another form. I have the datagrid set up to display the records it currently has but when I try to add new records it seems to not know how to find the database. My code is as follows.

    <code>
    Private Sub Command1_Click()
    con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=INVENTORY.mdb;Persist Security Info=False"
    Set frmData3.DataGrid1.DataSource = rs
    frmData3.DataGrid1.rs.Fields("itemName") = itemName.Text
    frmData3.DataGrid1.rs.Fields("itemType") = itemType.Text
    frmData3.DataGrid1.rs.Fields("itemNumber") = itemNumber.Text
    frmData3.DataGrid1.rs.Fields("itemQuantity") = itemQuantity.Text
    frmData3.DataGrid1.rs.Fields("itemCost") = itemCost.Text
    frmData3.DataGrid1.rs.Fields("itemPrice") = itemPrice.Text
    frmData3.DataGrid1.rs.Update
    MsgBox "Item Added!"
    End Sub

    </code>


    Any help is appreciated! Thank you in advance

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,031

    Re: Question about adding record to database so it shows in datagrid.

    Whew!

    Keep the Connection open for the duration of the run, there is nothing to be gained by repeatedly opening and closing it and everything to lose.

    If you have a separate Recordset reference use that and don't try to use the more limited interface exposed by the DataGrid control.

    Be sure you call the Recordset.AddNew method to add a row.

    In order to get the DataGrid to display an added row you must Recordset.Requery then Set DataGrid.DataSource = DataGrid.DataSource to refresh your grid from the database.


    I wouldn't use a naked Connection and naked Recordset at all. As soon as you need to make multiple use you are better off with a DataEnvironment.

    Working example here:

    http://www.vbforums.com/showthread.p...=1#post4916099

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