Results 1 to 15 of 15
  1. #1
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46

    Help with ItemData

    The form changes roles depending on what department is selected.

    There is an input combo box that I can choose personnel from different departments.
    I would like the combo box to default to the selected department. people can be borrowed from a different department

    The code snippet looks up the ID in the table that matches the name of the selected department.
    then tries to apply that to the combo box's ItemData property. I have tried many iterations

    Code:
    Private Sub CBO_SelDept_AfterUpdate()
    strDepartment = Me.CBO_SelDept.Column(0)
    Dim DeptList As Long
    DeptList = DLookup("[ID]", "Department_Personnel", "[Department] = '" & strDepartment & "'") 'works!
    MsgBox DeptList 'It works!
    Me.CBO_Department.ItemData (DeptList) 'Not working! :(
    Me.CBO_Department.Requery
    Me.CBO_ThorneID.Requery
    End Sub
    returns with


    Compile error:
    Invalid use of property


    -Tevis

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why do you have two comboboxes for department?

    Why using ItemData?

    Me.CBO_Department = DeptList
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    From HELP: "The ItemData property returns the data in the bound column for the specified row in a combo box."

    In your code, if the variable "DeptList" has a value of 102365, do you really have that many rows in the combo box???

    You probably don't have more than 10 departments in the combo box... so ItemData has a range of 0 to 10.

    When I use the ItemData property, it is for looping through the rows of a combo box/list box.

  4. #4
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    I do have two combo boxes for departments, one is for the production departments(5), such as mixing, encap, powder fill, bottle line etc.. the other is for employees assigned department(10) such as warehouse, sanitation, CQ, mixing, powder fill, encap.. etc.

    Most of the time, employees are assigned work from within their own department, but not always. So, I just wanted the convenience of the combo box already having the selected department for production, select the corresponding department for personnel.

    My trouble is don't know what it want as an input other than a hand type value. .ItemData( variable here? )

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    If the lists for each combobox are not the same, how could you set the second equal to selection from the first? This makes no sense to me. Are combobox selections limited to the listed items? Is value of combobox an ID and the text description is displayed (this is an 'alias')? If the item is not in the list and ID is the value, there will not be an 'alias' to display, even if combobox selection is not limited to list.
    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
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    screen shot

    Click image for larger version. 

Name:	KPI db.jpg 
Views:	9 
Size:	100.5 KB 
ID:	23155

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    That doesn't answer my questions from previous post. Did some edits. Might read again.

    The image really does not clarify anything for me.
    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
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    strDepartment = Me.CBO_SelDept.Column(0)
    Dim DeptList As Long
    DeptList = DLookup("[ID]", "Department_Personnel", "[Department] = '" & strDepartment & "'") 'works

    gets the name of the selected department and stores it in the string strDepartment. A combo box that lists departments from the Departments table.
    Then I lookup the ID of the corresponding department in the Department_Personnel table and assign it to the DeptList Long Variable.

    I then report that ID number in a message box to check to see if the Dlookup was correct.
    DeptList is holding the ID of the corresponding department. (The ID of the entry in the Department_Personnel table that has the same name in the Department table)

    I then try to use that variable (DeptList) to select the corresponding list item in the combo box.

  9. #9
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    Click image for larger version. 

Name:	KPI listbox.jpg 
Views:	10 
Size:	69.3 KB 
ID:	23156

    that is the contents of the listboxes, the contents of the tables are the same.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    If the ID is in the list, then this should work:

    Me.CBO_Department = DeptList

    If you want to provide db for analysis, follow instructions at bottom of my post.
    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.

  11. #11
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    in very short:
    1.Top combo box, department table.
    2. Lookup ID of department with same name in department_personnel table
    3. use that ID to preselect the value in the personnel department combo box

  12. #12
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    Quote Originally Posted by June7 View Post
    If the ID is in the list, then this should work:

    Me.CBO_Department = DeptList

    If you want to provide db for analysis, follow instructions at bottom of my post.
    ok.

    .......

  13. #13
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    *facepalm*
    You'd think I was a German engineer trying to come up with the solution..
    Simple, just assign the other combo box the same value, if the user wants something different, they will select it.

  14. #14
    tcox's Avatar
    tcox is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Oct 2015
    Posts
    46
    Code:
    Private Sub CBO_SelDept_AfterUpdate()
    strDepartment = Me.CBO_SelDept.Column(0)
    Me.CBO_Department = Me.CBO_SelDept.Column(0)
    Me.CBO_ThorneID.Requery
    End Sub
    simple and sweet.
    I think I can blame it on the 10 hr shifts we are doing during the holidays (to get 3 day weekends!)
    asleep at 11, up at 3:40, work by 5am... zzzZZZZ

  15. #15
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Just an FYI......

    You don't need to add ".Column(0)" - that is the default (the bound column).



    You apparently do not have these two lines
    Code:
    Option Compare Database
    Option Explicit
    at the top of EVERY code module, because in your posted code, "strDepartment" has not been declared. While the code will execute, you can have variables misspelled and not know it. This can result in debugging hell.

    There is an option in the VBA IDE to require variable declaration (TOOLS/OPTION/EDITOR tab/Require Variable Declaration check box).
    Checking the box will add "Option Explicit" to a NEW module.
    For existing modules (standard, form & report), you would need to add the line manually.

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

Similar Threads

  1. Replies: 1
    Last Post: 08-03-2012, 01:44 PM
  2. ItemData from a listbox
    By eliotchs in forum Forms
    Replies: 4
    Last Post: 12-29-2011, 04:33 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