Results 1 to 14 of 14
  1. #1
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383

    Update Subform using apend query


    I hope I can explain this well enough.Some of you have my db already without of course the latest revisions. I need for the append query I run to update the subform which is located on my main form. Main form is named TimeCards and subform is named TimeCardCatAndProdSub. I am using a Listbox to tag my product selection then append to the Products table. I also need a way to make it add those selected products to my subform named TimeCardCatAndProdSub. Listbox form is named FProductMultiItemSelect.
    I run 2 append queries at a time using the Append command button on the form FProductMultiItemSelect, one named QAppendToProducts and the other named QTimeCardCatAndProdSub.

    Warning, "Please don't try this on an empty stomach or after drinking!!!" It will make you lose your religion, I know, I have already lost mine.

    Thanks in Advance,

    Dave
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Open TimeCards and immediately get error message: The form name 'TimeCardPaymentSub' is misspelled or refers to a form that doesn't exist. I deleted the SourceObject property of the subform control so not annoyed by that message. I also reset form ShortcutMenu property to Yes because I like the shortcut menu when I debug. I turn off shortcut menu in project Options and override with shift key when I open project.

    Do the append queries successfully add records?

    Is TimeCards already open when the append queries are executed? Then just need to requery the subform? Try:
    Forms!TimeCards!TimeCardCatAndProdSub.Requery
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Yes, the append queries work correctly. Forgot to remove frustrating stuff form scaled down db, sorry!

    Yes, the TimeCards form will be open. NO, don't think that will work since I tried it. It adds a blank record though.

  4. #4
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    One more thing, for some reason the Qty does NOT get updated when adding items to the tblOtherTable, it does however update the OtherValue correctly.
    Maybe force it somehow to use the stored value of OtherValue, ? Ran into a problem with error message ; Enter Parameter Value for Forms!TimeCards!TimeIDA
    Had to make the control visible for it to go away.All my hidden controls are in RED BTW ! TimeIDA is in the bottom left corner of TimeCards form. Sorry for the confusion.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    So user has to click AddItems to create and populate temporary table tblOtherTable (where is code to create this table?) then they have to click Append to run the query. Personally, instead of creating temp table and adding records to that table in a loop, I would just append directly to the Products table in that loop. I would make only one button to accomplish this.

    For Each varItem In ctl.ItemSelected
    CurrentDb.Execute "INSERT INTO Products([Product Name], [Unit Price], [TaxStatus], [Qty] " & _
    "VALUES('" & ctl.Column(1, varItem) & "', " & _
    ctl.Column(2, varItem) & ", '" & _
    ctl.Column(3, varItem) & "', " & _
    InputBox("Enter a Numeric Quantity for product " & ctl.Column(1, varItem))
    Next

    Why is the Qty field in the listbox? Isn't this is the value requested by the InputBox?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    So, to be clear, it will look like this?

    If Me.ListOne.ItemsSelected.Count = 0 Then
    MsgBox "You must select at least 1 Item"
    Exit Sub
    End If

    Set ctl = Me.ListOne
    For Each varItem In ctl.ItemsSelected
    rs.AddNew
    CurrentDb.Execute "INSERT INTO Products([Product Name], [Unit Price], [TaxStatus], [Qty] " & _
    "VALUES('" & ctl.Column(1, varItem) & "', " & _
    ctl.Column(2, varItem) & ", '" & _
    ctl.Column(3, varItem) & "', " & _
    InputBox("Enter a Numeric Quantity for product " & ctl.Column(1, varItem))
    rs.Update
    Next

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Or could be:
    Code:
    If Me.ListOne.ItemsSelected.Count = 0 Then
        MsgBox "You must select at least 1 Item"
    Else
        Set ctl = Me.ListOne
        For Each varItem In ctl.ItemsSelected
            rs.AddNew
            CurrentDb.Execute "INSERT INTO Products([Product Name], [Unit Price], [TaxStatus], [Qty] " & _
            "VALUES('" & ctl.Column(1, varItem) & "', " & _
            ctl.Column(2, varItem) & ", '" & _
            ctl.Column(3, varItem) & "', " & _
            InputBox("Enter a Numeric Quantity for product " & ctl.Column(1, varItem))
            rs.Update
        Next
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I get a syntax error in INSERT INTO statement.

    (also, just thinking out loud, what if the user has MultiItems form open and either they or another user adds items to the table? If you navigate to another record, what then? Will it update the record you were on, same TimeID or what? Finally, will this add the products to the subform TimeCardCatAndProdSub and if not then, no new calculations will be made for that order.) ? ( Maybe you cant get there from here)

    Private Sub cmdAddRecords_Click()

    Dim strSQL As String
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim ctl As Control
    Dim varItem As Variant

    On Error GoTo ErrorHandler

    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Products", dbOpenDynaset, dbAppendOnly) ' Changed from tblOtherTable


    'New Code
    If Me.ListOne.ItemsSelected.Count = 0 Then ' Make sure a seleciton has been made
    MsgBox "You must select at least 1 Item"
    Else
    Set ctl = Me.ListOne
    For Each varItem In ctl.ItemsSelected
    rs.AddNew
    CurrentDb.Execute "INSERT INTO Products([Product Name], [Unit Price], [TaxStatus], [Qty] " & _
    "VALUES('" & ctl.Column(1, varItem) & "', " & _
    ctl.Column(2, varItem) & ", '" & _
    ctl.Column(3, varItem) & "', " & _
    InputBox("Enter a Numeric Quantity for product " & ctl.Column(1, varItem))
    rs.Update
    Next
    End If


    ExitHandler:
    Set rs = Nothing
    Set db = Nothing
    Exit Sub

    ErrorHandler:
    Select Case Err
    Case Else
    MsgBox Err.Description
    DoCmd.Hourglass False
    Resume ExitHandler
    End Select

    End Sub
    Last edited by burrina; 11-14-2012 at 12:01 AM. Reason: Edited Out Old Code, More thoughts

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Sorry, I missed paren:

    ...[TaxStatus], [Qty])

    Can remove all the recordset related code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I still get the same error message.
    CurrentDb.Execute "INSERT INTO Products([Product Name], [Unit Price], [TaxStatus], [Qty]) " & _

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    If you hadn't guessed, I was writing the code directly into post, not testing. So I finally put it in the db and tested and debugged - parens will kill me, another I missed.

    InputBox("Enter a Numeric Quantity for product " & ctl.Column(1, varItem)) & ")"

    Why don't you experiment with debugging the code before doing this edit. To debug sql statement, useful to use a variable for the sql string. Set breakpoint in code and after the variable is populated print it to the immediate window. Link at bottom of my post has guidelines for debugging techniques.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I am trying to clear the list of selected items form my listbox named ListOne, here is my code that does NOT work.

    For i As Integer = (ListOne.SelectedItems.Count - 1) To 0 Step -1 ' This line of code is the problem. It highlites As and points to it as the problem.
    ListOne.Items.Remove (ListOne.SelectedItems(i))
    Next
    __________________________________________________ _____________________________________
    Tried this code as well.;
    ' Determine if the currently selected item in the ListBox ' is the item displayed at the top in the ListBox. If listOne.TopIndex <> listBox1.SelectedIndex Then
    ' Make the currently selected item the top item in the ListBox.
    ListOne.TopIndex = ListOne.SelectedIndex
    End If
    ' Remove all items before the top item in the ListBox. Dim x As Integer
    For X = ListOne.SelectedIndex - 1 To 0 Step -1
    ListOne.Items.RemoveAt (X)
    Next X

    ' Clear all selections in the ListBox.
    ListOne.ClearSelected() ' This is where it errors out, it says expected ='s
    Last edited by burrina; 11-14-2012 at 02:14 PM. Reason: Tried this code as well

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2002
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Thanks, with an adaption, I was able to make that work.

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

Similar Threads

  1. Replies: 1
    Last Post: 11-07-2012, 05:31 PM
  2. Replies: 1
    Last Post: 07-20-2012, 09:48 AM
  3. Replies: 3
    Last Post: 03-11-2012, 08:24 PM
  4. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 PM
  5. Apend Query for master and sub form
    By lupis in forum Forms
    Replies: 4
    Last Post: 05-28-2010, 06:27 PM

Tags for this Thread

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