Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 35
  1. #16
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    Whoa!!
    Let's get an understanding here. Do you want to write many new records to Problem_Record when the button is clicked? Many, because the listboxes are multiselect.
    If so, you DO need to do an AddNew/Update for each one.


    Now, for ProblemID. Your listboxes show a text field. You require a long integer to satisfy ProblemID. That means you have to have a multicolumn listbox which will include the ProblemID and the Description. Have you taken that into account? Have a look at this:
    http://software-solutions-online.com...umn-listboxes/

  2. #17
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    Yes I do want to write many new records to the Problem-Record. When this works it is doing just that! Perfect.
    Here is an expanded copy of my form which shows three columns
    ProblemID, Category ID, Problem

    Here is the Row Source for the first box. Each box uses a different Category ID to filter it.
    LstCAR Row Source - SELECT problem.ProblemID, problem.[Category ID], problem.Problem FROM problem WHERE (((problem.[Category ID])=1));
    Attached Thumbnails Attached Thumbnails form with expanded lstbox.PNG  

  3. #18
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    This should get the problemID:
    Code:
    rst!problemID = lstWhatever.Column(0, varItem)

  4. #19
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    1) If Me.lstMyListBox.ItemsSelectedCcount = 0 Then
    msgbox...
    Exit Sub
    End If

    As for the other, hope my fellow forum members chime in if they have an opportunity. I will have to look at this in more depth later (gotta run); just had a moment to address the first question. Thought you might have orphaned records. If you have a one to many relationship and are creating records, the one side has to exist before you can create an entry on the many side. Not sure if that helps.

  5. #20
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    Ok. I have put the rst!Addnew and rst!Update back in my code. My code now looks like it did in post #6.

    It is getting an error on this line.
    Code:
    Me.nonconformance_record_NonconformanceRecordID = problem_record.NonconformanceRecordID
    (when I was in Debug mode and I hovered over the first part it shows the correct NonconformanceRecordID - which is currently 445)
    (when I was in Debug mode and I hovered over the second part it shows the NonconformanceRecordID =Null)

    I have tried several version of this line of code and still can't get it to work.

    SO I know this is holding the correct value. What I need the other end of this statement to write that NonconformanceRecordID to the Probblem_Record tbl

    It is creating a record in the Nonconformance_record tbl but it is not "linking" the NonconformanceRecordID to the Probblem_Record tbl so I can pick up the records from the listboxes.

    Any suggestions would be greatly appreciated.

  6. #21
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    I assume your form is bound to nonconformance_record. It appears that the current record for nonconformance_record does not yet exist when looping thru the listboxes. In that case, let's try to force a save:
    Code:
    Option Compare Database
    Private Sub cmdAddRec_Click()
        
        Dim varItem As Variant    'Selected items
        Dim strDelim As String    'Delimiter for this field type.
        Dim i As Variant
        Dim recCnt As Integer  'record count to identify how many problem_record
        'were added based on this nonconformance record
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        
        On Error GoTo cmdAddRec_Click_Error
          Set dbs = CurrentDb
          Set rst = dbs.OpenRecordset("Problem_Record") 'Form Recordsource = Nonconformance_Record tbl
    
        if Me.dirty then me.dirty = false   'force record save
        debug.print "Key is " & me.nonconformanceRecordID 
    
        'Loop through the ItemsSelected in the list box.
        With Me.lstCAR
            'here we are getting the Problemid of the selected Items '*********************************
          For Each varItem In .ItemsSelected
              If Not IsNull(varItem) Then
                  rst.AddNew
                 recCnt = recCnt + 1
                  rst!ProblemID = lstCAR.ItemData(varItem)
                  rst!NonconformanceRecordID = Me.NonconformanceRecordID
                   rst.Update
                End If
                 
            Next
       End With
    Make sure you have a textbox named nonconformanceRecordID bound to that field.
    Then down near the end, take out the "if form.dirty..." and the "docmd.goto..."

    You can determine if the record exists yet by leaving out the "if me.dirty..." and see if you get an error with the debug.print because the field is empty.

  7. #22
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    Dave!
    You just said something that clicked. I don't have a text box for NonconformanceRecordID on the form. Do I need to add one and make it non-Visible?

  8. #23
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    Added the text box which shows the record number on the form! Made the other code changes.
    Now I get

    Me.nonconformance_record_NonconformanceRecordID = problem_record_NonconformanceRecordID

    Error
    You can't assign a value to this object

  9. #24
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    You cannot change/update an autonumber primary key in its own table. Ever.
    Where is that code? I don't see it in any previously posted...

  10. #25
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    I don't want to change it. I want to pass it to the field NonconformanceRecordID in the Problem_Record tbl to create the linking records.

    How do I write a line(s) to do that. The textbox is showing the correct NonconformanceRecordID so I know it is being picked up.

  11. #26
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    Code:
    Me.nonconformance_record_NonconformanceRecordID = problem_record_NonconformanceRecordID
    That code makes no sense. Again, where is it in your posted code?
    The field on the left of the "=" is the receiving field...

  12. #27
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    Here is the latest code
    Code:
          Set dbs = CurrentDb
          Set rst = dbs.OpenRecordset("Problem_record") 'Form Recordsource = Nonconformance_Record tbl
          If Me.Dirty Then Me.Dirty = False   'force record save
        Debug.Print "Key is " & Me.nonconformance_record_NonconformanceRecordID
        
        'Loop through the ItemsSelected in the list box.
        With Me.lstCAR
            'here we are getting the Problemid of the selected Items '*********************************
          For Each varItem In .ItemsSelected
              If Not IsNull(varItem) Then
                  rst.AddNew
                  recCnt = recCnt + 1
                  rst!ProblemID = lstCAR.ItemData(varItem)
                   Me.problem_record_NonconformanceRecordID = Me.nonconformance_record_NonconformanceRecordID
                   rst.Update
                End If
                 
            Next
       End With
    OH!! I thought the field on the left was the sending field!

    I flipped it around. It is now creating the linking records but look at the screen shot. It is only writing the NonconformanceRecordID once and it should be on every record like the records above.

    This is the line I have had trouble with from the beginning.

    REcord 448 is the latest record.
    Attached Thumbnails Attached Thumbnails ProblemRecord 3-21.PNG  

  13. #28
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    Code:
    Set dbs = CurrentDb
          Set rst = dbs.OpenRecordset("Problem_record") 'Form Recordsource = Nonconformance_Record tbl
          If Me.Dirty Then Me.Dirty = False   'force record save
        Debug.Print "Key is " & Me.nonconformance_record_NonconformanceRecordID
        
        'Loop through the ItemsSelected in the list box.
        With Me.lstCAR
            'here we are getting the Problemid of the selected Items '*********************************
          For Each varItem In .ItemsSelected
              If Not IsNull(varItem) Then
                  rst.AddNew
                  recCnt = recCnt + 1
                  rst!ProblemID = lstCAR.ItemData(varItem)
                  debug.print "added from lstCAR # " & reccnt & ", " & lstCAR.itemdata(varItem) 
                   Me.problem_record_NonconformanceRecordID = Me.nonconformance_record_NonconformanceRecordID
                   rst.Update
                End If
                 
            Next
       End With
    Add the redline to see what's happening

    Also note that your code will not update existing records in Problem_Record. It will only create new records.

  14. #29
    PinkLady50 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    171
    It does not return any errors. It is running like before.
    Also note that your code will not update existing records in Problem_Record. It will only create new records.
    I only need it to create records not update.

  15. #30
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    The debug.print displays in the VB Editor Immediate window.
    CTRL-G while in the editor.

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Time doesn't add up correctly
    By Larryg in forum Reports
    Replies: 2
    Last Post: 09-03-2015, 11:41 PM
  2. Listbox won't display time correctly
    By UserX in forum Access
    Replies: 7
    Last Post: 06-26-2014, 05:39 PM
  3. Run Time Errors
    By Stephanie53 in forum Access
    Replies: 7
    Last Post: 03-27-2013, 07:48 AM
  4. Replies: 4
    Last Post: 10-07-2011, 10:58 AM
  5. Replies: 11
    Last Post: 03-16-2006, 12:28 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