MultSelect Listbox code errors out

Skip Bisconer

Who Me?
Local time
Today, 05:52
Joined
Jan 22, 2008
Messages
285
I have a multiselect listbox with the below code that errors out to "Method or data member not found" the error point stops at "rs.Position = ctl.ItemData(varItem)". I have attached the form and a screenshot of the table referenced. It has to be some little thing I am not seeing. Please advise.

This form has fields that carry over from another form but updates a different table.

Code:
Private Sub Command24_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("tblTrainingRequirementsByAllPositions", dbOpenDynaset, dbAppendOnly)
  'make sure a selection has been made
  If Me.Position.ItemsSelected.Count = 0 Then
    MsgBox "Must select at least 1 position"
    Exit Sub
  End If
 
  'add selected value(s) to table
  Set ctl = Me.Position
  For Each varItem In ctl.ItemsSelected
    rs.Position = ctl.ItemData(varItem)
    rs.AddNew
    rs!CWSPolicy = Me.CWSPolicy
    rs!Title = Me.Title
    rs!Intv = Me.Intv
    rs.Update
  Next varItem
 
  DoCmd.Close
 
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
 

Attachments

  • AddToPositionReqTraining.jpg
    AddToPositionReqTraining.jpg
    73.2 KB · Views: 82
  • TableFieldsReqTraining.jpg
    TableFieldsReqTraining.jpg
    30.3 KB · Views: 85
I suspect you want this:

rs!Position = ctl.ItemData(varItem)

and after the AddNew line.
 
I believe you need to start your loop with .Edit.
 
Actually I think Paul's idea is probably a better guess.
 
Thanks Paul and RG that made the whole thing work. This db world is full of details. It's really nice have people like you willing to share your knowledge.
 
No problem, Skip. Now send some rain and snow up here, will ya?!?
 
I keep trying but LA seems to be getting all the water. Our lake Shasta is down so low you can see buildings and parts of the old Highway 99 bridge and road. In fact thery are using the highway as a boat ramp. As you probably are we are also in about the 4th year with less than 75% of normal.
 
You're up by Shasta? That's a nice area. We've gone through there a few times (usually playing golf), and have in-laws in Redding. We're have a similar water situation (or lack-of-water situation).
 

Users who are viewing this thread

Back
Top Bottom