Results 1 to 7 of 7
  1. #1
    ndehhh is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2015
    Posts
    7

    DLookup not working for form

    Hi guys, I'm new to Access here!



    I've got 4 tables, Students, Courses, Course_Section and Registration.

    I've created a form based off Registration and am trying to get the details (Last_Name, First_Name, etc.) found in table "Students" to autofill when I key in the "Students" primary key, Student_ID.

    This is what I have:

    Option Compare Database

    Private Sub Student_ID_AfterUpdate()


    PopulateFields

    End Sub


    Private Sub PopulateFields()


    [Last Name] = DLookup("[Last Name]", "Students", "Student_ID = " & Me.Student_ID)
    [First Name] = DLookup("[First Name]", "Students", "Student_ID = " & Me.Student_ID)
    [Email Address] = DLookup("[Email Address]", "Students", "Student_ID = " & Me.Student_ID)
    [Telephone] = DLookup("[Telephone]", "Students", "Student_ID = " & Me.Student_ID)

    End Sub

    Autofill works, but I keep getting 'You must enter a value in the 'Students.Last_Name' field', and I've no idea why.

    Much thanks!

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you tried using a JOIN instead?

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You keep typing "Last_Name" - with an underscore,
    but your code has "[Last Name]" - with a space. So which is it?


    BTW, shouldn't use spaces in object names....

  4. #4
    ndehhh is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2015
    Posts
    7
    Hi sorry it is "Last Name" without an underscore! What's the rationale for not using spaces in object names?

    I now have this problem where DLookup only autofills the details for Student ID 1, no matter what Student ID I input into the form.
    Also, when I click the button to register for course, the details do not appear in the empty "Registration" table even though my form's record source is selected as "Registration" and "Data Entry" under data is Yes

    My button's code is as follows:

    If Me.Dirty Then Me.Dirty = False

    Any ideas?

  5. #5
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Check to see if your control [Last Name] is linked to the [Last_Name] field you intended. That error is suggestive that your table-level requirements for "not blank" are not being satisfied. If you have text in your control, it should save to your designated field -- unless there is not a link between your control and the field.

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    The rationale for not using spaces in object names is that the Access Gnomes have no way to know that Last Name is a single object. In code you can work around this, by surrounding the name in Square Brackets, as you've done here, but sooner or later you'll forget to do this and spend time trying to figure out why your code is bombing...it's just easier to simply not use spaces. Everyone has their own preferences, here, but an Underscore

    Last_Name

    or CamelBack formatting

    LastName

    are the most common.

    Your statement

    Quote Originally Posted by ndehhh
    I now have this problem where DLookup only autofills the details for Student ID 1, no matter what Student ID I input into the form
    makes me wonder about the Datatype of Student_ID. You syntax is correct if the Datatype is a Number, but if Student_ID is defined as Text, it would have to be

    Code:
    [Last Name] = DLookup("[Last Name]", "Students", "Student_ID = '" & Me.Student_ID & "'")

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Begs the question - Why are you populating fields with student info anyway? This appears to be duplicating data in multiple tables.
    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.

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

Similar Threads

  1. Replies: 3
    Last Post: 06-17-2015, 05:18 PM
  2. Dlookup not working
    By shaunacol in forum Forms
    Replies: 8
    Last Post: 06-12-2015, 09:28 AM
  3. Dlookup from another table in form not working
    By ascool_asice in forum Forms
    Replies: 6
    Last Post: 01-04-2015, 03:39 AM
  4. DLookup isn't working
    By cec in forum Access
    Replies: 7
    Last Post: 12-28-2012, 12:21 PM
  5. order by dlookup not working
    By broof in forum Queries
    Replies: 3
    Last Post: 01-11-2011, 03:15 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