+ Reply to Thread
Results 1 to 11 of 11

Thread: VBA SQL update statement

  1. #1
    Registered User
    Join Date
    11-26-2010
    Location
    Leeds England
    MS-Off Ver
    Excel 2003
    Posts
    6

    VBA SQL update statement

    Can anyone help with this???

    I am trying to create an update statement to find a field in a database and replace it with data from combo boxes and a text box.

    This is the code that i am currently using.

    Private Sub cmd_update_Click()
    
    Dim Details As String 'make sure the type is defined
    Dim Description As String 'make sure the type is defined
    Dim WT_category As String
    Dim clicked_item As String
    Dim pos_of_space As Integer
    Dim the_ref As Integer
    
    clicked_item = lst_results
    pos_of_space = InStr(clicked_item, " ")
    
    the_ref = Val(Left(clicked_item, pos_of_space - 1))
    Details = Replace(Details, "'", "")
    
    If cbo_type_update = "Working Together" Or cbo_type_update = "Double Whammy" Then
    
        strsql = "UPDATE [Main Table]SET([Detail],[Description],[WT_Category]) " _
            & "VALUES('" & txt_details_update & "','" & cbo_type_update & "','" & cbo_wt_update & "')" _
            & "WHERE Action_Number = " & the_ref & ""
    
    End If
    
    db.Open CONNECTION_STRING
    db.Execute strsql, , adCmdText
    db.Close
    
    MsgBox "Data updated"
    
    End Sub
    any help would be appreciated
    Last edited by DonkeyOte; 11-26-2010 at 12:43 PM.

  2. #2
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,247

    Re: VBA SQL update statement

    And the problem is ... what?

  3. #3
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: VBA SQL update statement

    shawby - welcome to the board though please note that CODE tags should encase your VBA at all times (per Forum Rules).

    Given first post etc I've modified your post for you but going forward please be sure to adhere to the above.

    For more information about these and other tags, found here

  4. #4
    Registered User
    Join Date
    11-26-2010
    Location
    Leeds England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA SQL update statement

    Quote Originally Posted by Bob Phillips View Post
    And the problem is ... what?
    The problem is that i get a syntax error for the update statement.

  5. #5
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: VBA SQL update statement

    You might first try adding a space prior to the SET command

    Thereafter, though you're seemingly trying to do something regards potential for apostrophe's (see Details variable) you're not as I see it catering for that possibility presently.
    It's also impossible to determine from the code where the variable values are being generated (presumably they have Module/Global level scope)

    Prior to the End If line add the following:

    Debug.Print strsql
    and review the output in the Immediate Window - this will show the SQL you're trying to perform (ie with variables inserted etc) - post it here.

  6. #6
    Registered User
    Join Date
    11-26-2010
    Location
    Leeds England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA SQL update statement

    This is what appears in the immediate window

    UPDATE [Main Table] SET([Detail],[Description],[WT_Category]) VALUES('quality checked Zoes Fraud pack test','Working Together','Trusted')WHERE Action_Number = 134
    Last edited by DonkeyOte; 11-29-2010 at 05:47 AM. Reason: removed unnecessary quote

  7. #7
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: VBA SQL update statement

    I don't know which DB platform you're using but do you need a space prior to the WHERE command ?

  8. #8
    Registered User
    Join Date
    11-26-2010
    Location
    Leeds England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA SQL update statement

    Quote Originally Posted by DonkeyOte View Post
    I don't know which DB platform you're using but do you need a space prior to the WHERE command ?
    unfortunately not that simple

  9. #9
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: VBA SQL update statement

    well without more info. (ie platform, field types, error line etc) there's little we can suggest I'm afraid

    if you step through the code using F8 does the connection actually open correctly - ie the debug occurs on the execution of the SQL itself.

    (it's also difficult to error check given we can't see how the greater majority of the variables in the code are being generated in the first instance)

  10. #10
    Registered User
    Join Date
    11-26-2010
    Location
    Leeds England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA SQL update statement

    the error occurs when trying to execute the strsql.

    basically all i am doing is taking data from a database, it is being output into a listbox using the following.
    lst_results.Clear
    
    db.Open CONNECTION_STRING
    
    'output to listbox
    rs.Open "SELECT Action_Number, Created, Detail, Name, WT_Category " _
        & "FROM [Main Table] " _
        & "WHERE Description = '" & cbo_type & "' " _
        & "AND [Name] = '" & my_name & "'" _
        & "AND Created between #" & from_date_us & "# AND #" & to_date_us & "#;", db, adOpenStatic, adLockReadOnly
    
    Do While Not rs.EOF
    
        lst_results.AddItem rs.Fields("Action_Number") & " " & rs.Fields("Created") & " " & rs.Fields("Detail")
        
        rs.MoveNext
    Loop
    rs.Close
    
    db.Close
    then when i click an item in lst_results it ouputs the data to combo and text boxes
    Private Sub lst_results_Click()
    
    update_frame.Visible = True
    
    Dim clicked_item As String
    Dim pos_of_space As Integer
    Dim the_ref As Integer
    
    clicked_item = lst_results
    pos_of_space = InStr(clicked_item, " ")
    
    the_ref = Val(Left(clicked_item, pos_of_space - 1))
    
    db.Open CONNECTION_STRING
    
    rs.Open "SELECT Action_Number, Created, Detail, Description, WT_category " _
        & "FROM [Main Table] " _
        & "WHERE Action_Number = " & the_ref & "", db, adOpenStatic, adLockReadOnly
    
    cbo_type_update = rs.Fields("Description")
    cbo_wt_update = rs.Fields("WT_category")
    txt_details_update = rs.Fields("Detail")
    
    rs.Close
    
    db.Close
    
    If cbo_type_update = "Working Together" Then
    cbo_wt_update.Visible = True
    Else
    cbo_wt_update.Visible = False
    
    End If
    
    End Sub
    I then change any of the data that has been output and click an update button which is where i am falling down with the code I have posted above.

    unsure what you mean by the type of database though.

  11. #11
    Registered User
    Join Date
    11-26-2010
    Location
    Leeds England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA SQL update statement

    Quote Originally Posted by DonkeyOte View Post
    well without more info. (ie platform, field types, error line etc) there's little we can suggest I'm afraid

    if you step through the code using F8 does the connection actually open correctly - ie the debug occurs on the execution of the SQL itself.

    (it's also difficult to error check given we can't see how the greater majority of the variables in the code are being generated in the first instance)
    As this is done entirely in excel 2007 would it help if i sent the file over?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0