Results 1 to 10 of 10
  1. #1
    redknite is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    12

    sql update in VBA

    I have a table called tblListings with a field called "Status"

    I have another table called tblSales. In the form for this table I want to have cmd button that will change the "Status" field in tblListings from PEN to SLD. The two tables are related with a field called "TRNumberL"



    I can't seem to get it to run right. Can someone tell me what I'm doing wrong?

    Code:
    Private Sub cmdCloseSale_Click()
    Dim myUpdate As String
    Dim mySet As String
    Dim myWhere As String
    Dim strSQL As String
    'set sql update statement
    myUpdate = "UPDATE tblListings "
    'set sql set statement
    mySet = "SET Status = 'SLD' "
    'set sql where statemewnt
    myWhere = "WHERE TRNumberL=" & " ' " & Me!TRNumberL & "'"
    'define full sql statement
    strSQL = myUpdate & mySet & myWhere & ";"
    DoCmd.RunSQL strSQL
    End sub

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you tried just creating a query that joins the two tables so you can directly change fields in both tables?

  3. #3
    redknite is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    12
    That's an option, but not what i really wanted to do. Is there something wrong with the code?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you put in a MsgBox for strSQL just before the RunSql so you can see whatb you are trying to run? What does the code do? Describe better than it doesn't run right please.

  5. #5
    pkstormy's Avatar
    pkstormy is offline Access/SQL Server Expert
    Windows XP Access 2003
    Join Date
    Mar 2010
    Location
    Madison
    Posts
    682
    Wouldn't it be easier to just create the strSQL itself versus breaking it into pieces first using myUpdate, mySet, and myWhere and then piecing it together to form strSQL?

    ie..
    strSQL = "UPDATE tblListings set Status = 'SLD'..... WHERE...."

    I personally like to avoid creating unnecessary variables such as myUpdate, mySet, and myWhere (since declaring new variables does eat up memory.)

  6. #6
    redknite is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    12
    when i run it in the msgbox this is what I get:

    UPDATE tblListings Set Status = 'SLD' WHERE TRNumberL= 'L-10-043';

    When I run the cmd without the msgbox i get a prompt saying

    You are about to update 0 rows. Like it can't find a record in tblSales with a TRNumberL of L-10-043.

  7. #7
    redknite is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    12
    Quote Originally Posted by pkstormy View Post
    Wouldn't it be easier to just create the strSQL itself versus breaking it into pieces first using myUpdate, mySet, and myWhere and then piecing it together to form strSQL?

    ie..
    strSQL = "UPDATE tblListings set Status = 'SLD'..... WHERE...."

    I personally like to avoid creating unnecessary variables such as myUpdate, mySet, and myWhere (since declaring new variables does eat up memory.)
    I tried that first. I separated it out so i could try to find my error. I originally had this...

    "strSQL = "UPDATE tblListings SET TRNumberL ='SLD' Where TRNumberL=" & Me.TRNumberL"

    But that didn't work. Any Ideas on that?

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Unless you changed something, this:
    "WHERE TRNumberL=" & " ' " & Me!TRNumberL & "'"
    creates a WHERE condition with a leading space.
    This: UPDATE tblListings Set Status = 'SLD' WHERE TRNumberL= ' L-10-043';
    instead of: UPDATE tblListings Set Status = 'SLD' WHERE TRNumberL= 'L-10-043';

  9. #9
    redknite is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    12
    PURE GENIUS. On your part that is! Thank you so much.

    I owe you a beer...

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Glad we could help. Are you ready to use the Thread Tools and mark this thread as Solved?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-22-2011, 12:23 PM
  2. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 PM
  3. Update Query- selective update?
    By stephenaa5 in forum Queries
    Replies: 1
    Last Post: 10-29-2009, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums