How can i code for adding and deleting data in datagrid

Discussion in 'Visual Basic ( VB )' started by viv345, Jun 24, 2010.

  1. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Any body can help me in making coding for adding and deleting data in datagrid with example attached
     

    Attached Files:

  2. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    how to add and delete data in datagrid?
     
  3. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    Dim conn As ADODB.Connection
    Dim Rs As ADODB.Recordset
    Private Sub cmdAdd_Click()
    If Len(Trim(Text1.Text)) > 0 Then
    With Rs
    .AddNew
    DataGrid1.DataField = "Field1"
    DataGrid1.DataField = "Field1"
    .Update
    DataGrid1.Refresh
    End With
    DataGrid1.Field(1).DataField Text1.Text
    DataGrid1.Field(2).DataField Text2.Text
    DataGrid1.Field(3).DataField Text3.Text
    DataGrid1.Field(4).DataField Text4.Text
    DataGrid1.Field(5).DataField Text5.Text
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    Else
    MsgBox "Please Insert Date"
    End If
    End Sub
    Private Sub Form_Load()
    Set conn = New ADODB.Connection
    conn.Provider = "Microsoft.Jet.OLEDB.4.0"
    conn.ConnectionString = "Data Source=" & App.Path & "\db1.mdb"
    conn.Open
    Set Rs = New ADODB.Recordset
    Rs.Open "SELECT * from Table1", conn, adOpenStatic, adLockOptimistic
    Do While Not Rs.EOF = True
    Text1.DataField = "Field1"
    Text2.DataField = "Field2"
    Text3.DataField = "Field3"
    Text4.DataField = "Field4"
    Text5.DataField = "Field5"
    Rs.MoveNext
    Loop
    End Sub
    
    WHERE I AM WRONG
     
    Last edited by a moderator: Jul 28, 2010
  4. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Dim conn As ADODB.Connection
    Dim Rs As ADODB.Recordset
    Private Sub cmdAdd_Click()
    If Len(Trim(Text1.Text)) > 0 Then
    With Rs
    .AddNew
    DataGrid1.DataField = "Field1"
    DataGrid1.DataField = "Field1"
    .Update
    DataGrid1.Refresh
    End With
    DataGrid1.Field(1).DataField Text1.Text
    DataGrid1.Field(2).DataField Text2.Text
    DataGrid1.Field(3).DataField Text3.Text
    DataGrid1.Field(4).DataField Text4.Text
    DataGrid1.Field(5).DataField Text5.Text
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    DataGrid1.Text = ""
    Else
    MsgBox "Please Insert Date"
    End If
    End Sub
    Private Sub Form_Load()
    Set conn = New ADODB.Connection
    conn.Provider = "Microsoft.Jet.OLEDB.4.0"
    conn.ConnectionString = "Data Source=" & App.Path & "\db1.mdb"
    conn.Open
    Set Rs = New ADODB.Recordset
    Rs.Open "SELECT * from Table1", conn, adOpenStatic, adLockOptimistic
    Do While Not Rs.EOF = True
    Text1.DataField = "Field1"
    Text2.DataField = "Field2"
    Text3.DataField = "Field3"
    Text4.DataField = "Field4"
    Text5.DataField = "Field5"
    Rs.MoveNext
    Loop
    End Sub

    WHERE I AM Wrong Can anybody help me?
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    If you post the same thing 10 more times it would not help. Your code is just perfect.

    Your first error was you attached the complete code without any hint on where you are stuck. Would you download somebodies code debug it and reply where he is wrong.

    You made the same mistake couple of more times.

    Your code is just correct but it does not work like you want and that is normal.
     
  6. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    Code:
    Private Sub cmdadd_Click()
     If Len(Trim(Text1.Text)) > 0 Then
     Adodc1.Recordset.AddNew
     Adodc1.Recordset.Fields("date") = Text1.Text
     Adodc1.Recordset.Fields("Customer Name") = Text2.Text
     Adodc1.Recordset.Fields("amount") = Text3.Text
     Adodc1.Recordset.Update
    Adodc1.RecordSource = "SELECT * FROM Table1"
    Adodc1.Caption = Adodc1.RecordSource
    Else
    MsgBox "Please Insert Date"
    End If
    
    End Sub
    
    use this to add records

    for deleting the selected row use this
    Code:
    Private Sub Command1_Click()
    If Adodc1.Recordset.RecordCount > 0 Then
    Adodc1.Recordset.Delete
    Else
    MsgBox "no record found"
    End If
    End Sub
    
     
    Last edited: Jul 28, 2010
  7. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Sorry to state that it is not an copied code from some bodies project. It is my own project. First I tried the codes to say data in MS Access it saved. After than i changed the codes to save the data in List boxes. But I was not satisfied because in some forms I have to Add/delete the names of customer and which should be displayed in Forms.

    But thanks for helping.
     
  8. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Study in Books and then try to make the project.

    THANKS AGAIN!
     
  9. viv345

    viv345 New Member

    Joined:
    Feb 22, 2010
    Messages:
    76
    Likes Received:
    0
    Trophy Points:
    0
    Thanks problem solved
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice