CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Aug 2002
    Location
    malaysia
    Posts
    234

    Red face SQl update data problem

    hello everybody,
    i have a problem using sql in updating my data to the access database.
    my code is like this:

    Public Sub CmdUpdate()
    Dim name as String
    Dim sql as String
    name = txtname.Text
    sql="UPDATE my_table SET name=' " & name & " ' "

    my_cn.BeginTrans
    my_cn.Execute(SQL)
    my_cn.CommitTrans

    End Sub
    but i got this error message:

    Run-time error '91':
    Object variable or with block variable not set.
    i have no problem in insert the data to the database but have problem in updating it.
    i don't know where i have code wrong. maybe you guy can have a look.

    thanks,
    gechin

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    my_cn

    seems as if "my_cn" has been set to nothing (or is out of scope here), or it might be "txtname" which is not visible from this sub.

    You can code:
    Code:
    sql= FormName.txtname.text
    and
    Code:
    if my_cn is nothing then
        set my_cn=new adodb.Connection
       '...set up the connection string again
       'and open it
    end if
    before trying to use my_cn
    Last edited by Cimperiali; November 19th, 2003 at 04:36 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Aug 2002
    Location
    malaysia
    Posts
    234
    well, "my_cn" is ok. i did not set it to nothing.
    this is because as my project load, i retrieve value from my_table and it is ok. but when i want to update, that's the problem i get.

    is my snytax wrong some where?

    gechin

  4. #4
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    Can u paste my_cn declartions

  5. #5
    Join Date
    Aug 2002
    Location
    malaysia
    Posts
    234
    er.... actually i'm now in the internet cafe and i did not copy down the connection on my paper.

    i have my project at my house PC. so it might be a problem here.

    gechin

  6. #6
    Join Date
    Aug 2002
    Location
    malaysia
    Posts
    234
    ok how about this, assume the connection is ok.

    is there anything wrong with my SQL syntax?
    gechin

  7. #7
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    Nothing wrong with SQL
    as the error suggest it is someting releted to the connection

  8. #8
    Join Date
    Aug 2002
    Location
    malaysia
    Posts
    234

    thanks

    oh i see.
    ok i will go back home and check it out.

    there is one more thing to ask. is there any diffrent between this SQL?

    sql = "UPDATE my_table SET my_table!name = ' " & name & " ' "

    well this get the same result as the previous sql statement?

    gechin

  9. #9
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    Nothing. It is the same

  10. #10
    Join Date
    Aug 2002
    Location
    malaysia
    Posts
    234
    ok i try on the connection this time and wait for my reply.
    thanks for the help. will get back to you as soon as i can.

    thanks,
    gechin

  11. #11
    Join Date
    Jan 2001
    Posts
    486
    Another possibility is this:

    Dim sql as String

    sql="UPDATE my_table SET name=' " & name & " ' "

    my_cn.Execute(SQL)

    Variable was lower case when declared and when value was assigned to it, but upper case when used.
    If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren

    Do canibals not eat clowns because they taste funny?

  12. #12
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    is VB variables are case sensetive???

  13. #13
    Join Date
    Jan 2001
    Posts
    486
    Not sure, was just a suggestion. Ergo use of the word "possibility".
    If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren

    Do canibals not eat clowns because they taste funny?

  14. #14
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    I checked
    If u difine variable as sql and if u type it SQL it will convert it to sql automattically




  15. #15
    Join Date
    Aug 2002
    Location
    malaysia
    Posts
    234
    VB variables are case sensetive.

    sorry for the late reply. i have fix the problem according to dinesh's suggestion. it is the connection's problem.

    i did not close my connection until the program end. that is why i think i still have the connection.

    i fix the program by creating a new connection inside the same function and it done.

    thank you guys for solving my problem.

    gechin

Page 1 of 2 12 LastLast

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

Featured