Results 1 to 8 of 8
  1. #1
    ckra81 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2017
    Posts
    3

    auto fill form, or something

    I have a form with a list box where the user selects an employee name. After selecting the employee, I need the employee's current department and office to populate into a title and department field. This has to be done so that if/when an employee's title and department changes the data at the time the record was created does not.

    I haven't done real coding since about 2001, so if it's required to do this, I'll need some detailed assistance. If it's beyond free advice, I understand that too. Any help is appreciated.

    The employee table contains:
    first name
    last name
    company via lookup


    location via lookup
    title

    The injury table contains:
    employee name, linked to employee table
    company, need to get from employee.company
    office, need to get from employee.location
    title, need to get from employee.title




  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    lacigol is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2017
    Posts
    14
    Idk exactly what your listbox is called or what employee info displays in said listbox, but maybe you could add an on update event to the employee selection list box and use the following code IF (1) your employee's table is named [employeesTable], (2) your employee listbox is named lstEmployees, (3) your listbox displays lastnames only, (4) your title and department fields are textboxes named txtTitle and txtDepartment, and (5) your department field is equivalent to location?

    Code:
      dim sql, employeeName as string
      dim rstEmployees as recordset
    
      ' escape apostrophe's in names like O'Brien or O'Neil
      ' so the select query doesn't break on those names
      employeeName = Replace(lstEmployee.value, "'", "''")
    
      sql = "select * from employeesTable where lastName='" & employeeName & "'"
      set rstEmployees = currentdb.open(sql, dbopensnapshot, dbreadonly, dbreadonly)
    
      if not rstEmployees.eof
        txtTitle = rstEmployees.field("title")
        txtDepartment = rstEmployees.field("location")
      end if
    P.S. I see your location field is a lookup, so what table is it in and what field (id or lastname?) links/relates to it in the employeesTable?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    And (6) you don't have two employees with the same last name.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    lacigol is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2017
    Posts
    14
    Hehe it works, just not effectively - would select the first one found each time

    I can't recall the last time i created a table without an id autonumber field and a creation date/time field default to =now()

  6. #6
    ckra81 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2017
    Posts
    3
    Thanks for the replies. I've been pulled away for a bit and will try this soon. I'll post what happens. Thanks again.

  7. #7
    ckra81 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2017
    Posts
    3
    PBaldy wins the award with his link to instructions. Seems so simple now. Thanks!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Woo hoo! Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Auto Fill a form
    By Suzemt in forum Access
    Replies: 3
    Last Post: 02-18-2013, 11:20 AM
  2. Auto-Fill text fields in the form
    By sk88 in forum Access
    Replies: 2
    Last Post: 01-10-2012, 08:22 PM
  3. Auto fill a form
    By dreli_20 in forum Forms
    Replies: 11
    Last Post: 11-09-2011, 06:24 PM
  4. Multiple Auto Fill in Form
    By mwebster in forum Forms
    Replies: 1
    Last Post: 07-06-2011, 10:39 AM
  5. Replies: 2
    Last Post: 06-21-2011, 10:08 AM

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