Results 1 to 9 of 9
  1. #1
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36

    Use updateable query error

    I don't understand which part of this statement is an attempt to update a query. I am trying to update the Restaurant table. The WHERE clause references a list box that contains a query.



    Private Sub AssignButton_Click()
    Dim SQLMasterUpdate As String
    Dim iRow As Variant

    If Me!RestaurantListBox.ItemsSelected.Count <> 0 Then
    For Each iRow In Me!RestaurantListBox.ItemsSelected
    SQLMasterUpdate = "UPDATE Restaurant SET " & _
    "Restaurant.AccountManagerID = (SELECT AccountManagerID FROM AccountManager " & _
    "WHERE AccountManagerName = " & Me!NameComboBox.Value & ")" & _
    "WHERE ((SELECT RestaurantPhone FROM Restaurant) = " & _
    Me!RestaurantListBox.Column(1, iRow) & ");"

    DoCmd.RunSQL SQLMasterUpdate
    Next iRow
    Else
    MsgBox "Nothing was selected from the list", vbInformation
    Exit Sub 'Nothing was selected
    End If
    End Sub

  2. #2
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    Would this solve the problem you're facing?

    Make sure you use single quote or Char(34) for those values that are text (or string).

    Code:
    SQLMasterUpdate = "UPDATE Restaurant SET " & _
      "Restaurant.AccountManagerID = (SELECT AccountManagerID FROM AccountManager " & _
      "WHERE AccountManagerName = '" & Me!NameComboBox.Value & "')" & _
      "WHERE RestaurantPhone = '" & Me!RestaurantListBox.Column(1, iRow) & "');"

  3. #3
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Oh yeah. Well, I changed that, but still get the same error. It seems like it has to do with my subquery in the WHERE clause.

  4. #4
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Is the "non-updateable query" the table I am updating? Could it be because the phone number field does not allow duplicates? If so, what would be the way around that?

  5. #5
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    This update works:

    SQLMasterUpdate = "UPDATE Restaurant SET " & _
    "Restaurant.AccountManagerID = 7" & _
    " WHERE RestaurantPhone = '" & _
    Phone & "';"

    I used the number 7 instead of the SELECT statement. So how can I store and use the value obtained from:

    "SELECT AccountManagerID FROM AccountManager WHERE AccountManagerName = '" & Me!NameComboBox.Value & "'"

  6. #6
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    You can try using the DISTINCT keyword in the SQL to get unique row.

    Alternatively, you could change your combo box to store AccountManagerID as a hidden column. So, you can just access that selected value using combobox.Column(1). This will save you from writing the subquery in your update statement.

  7. #7
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    DISTINCT didn't cut it, but the other suggestion worked perfectly. I already had the ID hidden in the Combo Box knowing I would need it.

    Now the only question is how do I handle multiple updates without having to confirm each row updating separately?

  8. #8
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    You can do this 2 ways.

    1st way:
    Code:
    DoCmd.SetWarnings False
    .....
    DoCmd.RunSQL strSQL
    .....
    DoCmd.SetWarnings True
    I prefer the 2nd way:
    Code:
    Set db = CurrentDb
    db.Execute strSQL, dbFailOnError
    With the 2nd method, you can wrap in a transaction so as to control whether or not you commit transaction. You also have access to db.RecordsAffected. This means you can prompt user whether they want to update the number of records before committing the transactions. If not, then, either way is fine.

  9. #9
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Thanks. Useful info. Stay tuned... this project is not finished yet! (are they ever?)

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

Similar Threads

  1. Must use updateable query
    By adams.bria in forum Queries
    Replies: 1
    Last Post: 08-29-2011, 10:31 AM
  2. Need Query To Be Updateable
    By robsworld78 in forum Queries
    Replies: 11
    Last Post: 07-17-2011, 09:06 PM
  3. Replies: 3
    Last Post: 05-02-2011, 12:08 PM
  4. Non-Updateable Query
    By swalsh84 in forum Queries
    Replies: 4
    Last Post: 04-27-2011, 12:39 PM
  5. Replies: 0
    Last Post: 03-27-2010, 01:25 PM

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