Results 1 to 4 of 4

Thread: vb6 update record in the database

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    1

    vb6 update record in the database

    Hello, I have a combo box to select the ID, then it will display the particular record from from the database into the text boxes. And now I'm trying to make a update command button. Then I can just change the record which displaying in the text box. And click update, then the record will be updated in the database. But my cmdUpdate_Click() statement seems doesn't work. Anyone can help please? Cheers

    Option Explicit
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset

    Private Sub cmbID_Click()
    If cmbID.Text = "[Make Selection]" Then Exit Sub
    rs.Open "SELECT * FROM co_authors WHERE ID = " & cmbID.Text
    txtName.Text = rs!author_Name
    txtCountry.Text = rs!country
    txtPaperID.Text = rs!paper_ID
    rs.Close
    End Sub

    Private Sub cmdGo_Add_submission_info_Click()
    submission_info.Show
    update_co_authors.Hide
    End Sub

    Private Sub cmdUpdate_Click()
    Dim ar As ADODB.Recordset
    Set ar = New ADODB.Recordset
    ar.Open "SELECT * FROM co_authors WHERE ID = " & cmbID.Text
    If ar.EOF Then ar.AddNew
    Else: ar.Update
    End If
    ar!author_Name = txtName.Text
    ar!country = txtCountry.Text
    ar!paper_ID = txtPaperID.Text
    ar.Update
    End Sub

    Private Sub Form_Load()

    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\262CS\project\db1.mdb;" & _
    "Persist Security Info=False"

    rs.Open "SELECT ID FROM co_authors", _
    conn, adOpenForwardOnly, adLockReadOnly, adCmdText

    cmbID.Text = "[Make Selection]"

    Do Until rs.EOF
    cmbID.AddItem rs!ID
    rs.MoveNext
    Loop

    rs.Close

    End Sub

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If rs.State = adStateOpen Then rs.Close
    If conn.State = adStateOpen Then conn.Close
    Set rs = Nothing
    Set conn = Nothing
    End Sub

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: vb6 update record in the database

    You probably have not gotten an answer because this is in the wrong forum. This is the code bank for working samples. Try posting in the "Classic Visual Basic" Forum.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: vb6 update record in the database

    Moved from the codebank.


    Has someone helped you? Then you can Rate their helpful post.

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

    Re: vb6 update record in the database

    I think you have to tell it what you want to update (I'm not completely sure as I don't use addnew or update, but try this and see what happens.
    VB Code:
    1. Private Sub cmdUpdate_Click()
    2. Dim ar As ADODB.Recordset
    3. Set ar = New ADODB.Recordset
    4. ar.Open "SELECT * FROM co_authors WHERE ID = " & cmbID.Text
    5. If ar.EOF Then ar.AddNew
    6.    ar!author_Name = txtName.Text
    7.    ar!country = txtCountry.Text
    8.    ar!paper_ID = txtPaperID.Text
    9.    ar.Update
    10. Else
    11.    ar!author_Name = txtName.Text
    12.    ar!country = txtCountry.Text
    13.    ar!paper_ID = txtPaperID.Text
    14.    ar.Update
    15. End If
    16. End Sub

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