error in updating records in access db...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikas1111
    New Member
    • Feb 2008
    • 122

    error in updating records in access db...

    Hi All..
    I am accessing 2 colums from access database and updating them .

    [code=vb]
    Option Explicit
    Dim c As New adodb.Connectio n
    Dim cn As New adodb.Connectio n
    Dim cm As adodb.Command
    Dim rsnew2 As New adodb.Recordset

    Private Sub cmd_edit_Click( )

    If text1.Text = "" Then
    MsgBox " enter the company name"
    text1.SetFocus
    rsnew2.Fields(" text1") = text1.Text
    End If
    If text2.Text = "" Then
    MsgBox " enter the company address"
    text2.SetFocus
    rsnew2.Fields(" text2") = text2.Text
    End If
    rsnew2.Update
    MsgBox " add new successful ", vbInformation, "successful "

    End Sub

    Private Sub Form_Load()
    Dim cs As String
    c.Open ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=D:\COMIO N.MDB;Persist Security Info=False")
    rsnew2.Open "select * from company", c, adOpenDynamic
    Me.text1.Text = rsnew2(2)
    Me.text2.Text = rsnew2(3)

    End Sub
    [/code]

    There is error in RSNEW2.UPDATE ..
    CAN ANYBODY HELP ME TO SOLVE MY PROBLEM ??????????
    Last edited by debasisdas; Feb 25 '08, 08:25 AM. Reason: added code=vb tags
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    You need to call AddNew or Edit method of recordset before calling Update.

    Comment

    • vikas1111
      New Member
      • Feb 2008
      • 122

      #3
      Originally posted by debasisdas
      You need to call AddNew or Edit method of recordset before calling Update.
      rsnew2.Recordse t.AddNew
      is it correct???

      Comment

      • vikas1111
        New Member
        • Feb 2008
        • 122

        #4
        Originally posted by vikas1111
        rsnew2.Recordse t.AddNew
        is it correct???
        [code=vb]
        Option Explicit
        Dim c As New adodb.Connectio n
        Dim cn As New adodb.Connectio n
        Dim cm As adodb.Command
        Dim rsnew2 As New adodb.Recordset
        Dim rsnew3 As New adodb.Recordset


        Private Sub cmd_close_Click ()
        Unload.me
        End Sub

        Private Sub cmd_edit_Click( )
        rsnew3.Open "select * from company", c, adOpenDynamic, adLockOptimisti c
        If text1.Text = "" Then
        MsgBox " enter the company name"
        text1.SetFocus
        rsnew3.Fields(" text1") = text1.Text
        End If
        If text2.Text = "" Then
        MsgBox " enter the company address"
        text2.SetFocus
        rsnew3.Fields(" text2") = text2.Text
        End If
        rsnew3.Update
        MsgBox " add new successful ", vbInformation, "successful "

        End Sub

        Private Sub Form_Load()
        Dim cs As String
        c.Open ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=D:\COMIO N.MDB;Persist Security Info=False")
        rsnew2.Open "select * from company", c, adOpenDynamic
        Me.text1.Text = rsnew2(2)
        Me.text2.Text = rsnew2(3)

        End Sub
        [/code]

        I am getting message that "edit is successful", but its not updating in database..
        What should i do ????
        Last edited by debasisdas; Feb 25 '08, 08:24 AM. Reason: added code=vb tags

        Comment

        • debasisdas
          Recognized Expert Expert
          • Dec 2006
          • 8119

          #5
          try using this

          [code=vb]
          c.BeginTrans
          c.execute "Your update command here"
          c.CommitTtans
          [/code]

          Comment

          • vikas1111
            New Member
            • Feb 2008
            • 122

            #6
            Originally posted by debasisdas
            try using this

            [code=vb]
            c.BeginTrans
            c.execute "Your update command here"
            c.CommitTtans
            [/code]

            Error invalid sql statement...

            Comment

            • debasisdas
              Recognized Expert Expert
              • Dec 2006
              • 8119

              #7
              Originally posted by vikas1111
              Error invalid sql statement...
              you need to write a valid sql statmnet .

              Comment

              • lotus18
                Contributor
                • Nov 2007
                • 865

                #8
                Originally posted by vikas1111
                Error invalid sql statement...
                You need to specify the table and the fields you want to update.
                I think you need learn the basics of sql

                Sample

                [CODE=sql]"Update <TableName> Set <FieldName1>='M y 1st Record', <FieldName2>='M y 2nd Record', <FieldNameN>='M y N Record'"[/CODE]

                Remember enclosing with ' for string, # for dates, and nothing (as in nothing) if it is a number type. : )

                Comment

                • vikas1111
                  New Member
                  • Feb 2008
                  • 122

                  #9
                  Originally posted by lotus18
                  You need to specify the table and the fields you want to update.
                  I think you need learn the basics of sql

                  Sample

                  [CODE=sql]"Update <TableName> Set <FieldName1>='M y 1st Record', <FieldName2>='M y 2nd Record', <FieldNameN>='M y N Record'"[/CODE]

                  Remember enclosing with ' for string, # for dates, and nothing (as in nothing) if it is a number type. : )



                  I am using Microsoft Access Database.......

                  Comment

                  • debasisdas
                    Recognized Expert Expert
                    • Dec 2006
                    • 8119

                    #10
                    Originally posted by vikas1111
                    I am using Microsoft Access Database.......
                    kindly post the UPDATE query that you are executing

                    Comment

                    • vikas1111
                      New Member
                      • Feb 2008
                      • 122

                      #11
                      Originally posted by debasisdas
                      kindly post the UPDATE query that you are executing
                      Code:
                      Option Explicit
                      Dim C As New ADODB.Connection
                      Dim cn As New ADODB.Connection
                      Dim cm As ADODB.Command
                      Dim rsnew2 As New ADODB.Recordset
                      Dim rsnew3 As New ADODB.Recordset
                      
                      
                      Private Sub cmd_close_Click()
                      Unload.me
                      End Sub
                      
                      Private Sub cmd_edit_Click()
                      rsnew3.Open "select * from company", C, adOpenDynamic, adLockOptimistic
                      If text1.Text = "" Then
                      MsgBox " enter the company name"
                      text1.SetFocus
                      rsnew3.Fields("text1") = text1.Text
                       End If
                      If text2.Text = "" Then
                      MsgBox " enter the company address"
                      text2.SetFocus
                      rsnew3.Fields("text2") = text2.Text
                      End If
                      rsnew3.Update
                      MsgBox " add new successful ", vbInformation, "successful"
                      
                      End Sub
                      
                      Private Sub Form_Load()
                      Dim cs As String
                      C.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\COMION.MDB;Persist Security Info=False")
                      rsnew2.Open "select * from company", C, adOpenDynamic
                      Me.text1.Text = rsnew2(2)
                      Me.text2.Text = rsnew2(3)
                      
                      End Sub
                      Getting message that database has been updated.. but no changes are made ..

                      Comment

                      • debasisdas
                        Recognized Expert Expert
                        • Dec 2006
                        • 8119

                        #12
                        I asked you for your UPDATE query .

                        Refer to my previous post.

                        Comment

                        • vikas1111
                          New Member
                          • Feb 2008
                          • 122

                          #13
                          Originally posted by debasisdas
                          I asked you for your UPDATE query .

                          Refer to my previous post.
                          Code:
                          Option Explicit
                          Dim C As New ADODB.Connection
                          Dim cn As New ADODB.Connection
                          Dim cm As ADODB.Command
                          Dim rsnew2 As New ADODB.Recordset
                          Dim rsnew3 As New ADODB.Recordset
                          
                          
                          Private Sub cmd_close_Click()
                          Unload.me
                          End Sub
                          
                          Private Sub cmd_edit_Click()
                          rsnew3.Open "select * from company", C, adOpenDynamic, adLockOptimistic
                          If text1.Text = "" Then
                          MsgBox " enter the company name"
                          text1.SetFocus
                          rsnew3.Fields("text1") = text1.Text
                           End If
                          If text2.Text = "" Then
                          MsgBox " enter the company address"
                          text2.SetFocus
                          rsnew3.Fields("text2") = text2.Text
                          End If
                          C.BeginTrans
                          C.Execute "rsnew3.Update"
                          C.CommitTtans
                          
                          MsgBox " add new successful ", vbInformation, "successful"
                          
                          End Sub
                          
                          Private Sub Form_Load()
                          Dim cs As String
                          C.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\COMION.MDB;Persist Security Info=False")
                          rsnew2.Open "select * from company", C, adOpenDynamic
                          Me.text1.Text = rsnew2(2)
                          Me.text2.Text = rsnew2(3)
                          
                          End Sub
                          Last edited by NeoPa; Feb 26 '08, 12:44 PM. Reason: I expect this is simply a repost of the same code as the OP was confused

                          Comment

                          • debasisdas
                            Recognized Expert Expert
                            • Dec 2006
                            • 8119

                            #14
                            Can't you understand what is a UPDATE query.

                            Comment

                            • lotus18
                              Contributor
                              • Nov 2007
                              • 865

                              #15
                              [CODE=vb]Private Sub cmd_edit_Click( )
                              If text1.Text = "" Then
                              MsgBox " enter the company name"
                              text1.SetFocus
                              Exit Sub
                              ElseIf text2.Text = "" Then
                              MsgBox " enter the company address"
                              text2.SetFocus
                              Exit Sub
                              Else
                              C.Execute "Update Company Set text1='" & Text1.Text & "', text2='" & Text2.Text & "' Where ID='" & txtID.Text & "'"
                              MsgBox "Update successful ", vbInformation, "successful "
                              End If[/CODE]

                              Im assuming that your field names are text1 and text2 (I've based it on your codes) and lastly txtID.Text as your Primary Key (ID Field)

                              Rey Sean
                              Last edited by lotus18; Feb 25 '08, 11:43 AM. Reason: forgot to add Exit sub

                              Comment

                              Working...