Share via

Dlookup in RecordSource

Anonymous
2010-09-27T13:17:20+00:00

I have a single view form who's data source is derived from Me.Recordsource (see below).  I would like to add a text box (Me.JobDescr) to this form who's data is derived from a dlookup command within VBA.  When I ran the updated form I noticed that Me.JobDescr in all of the records does not match up to the appropriate data but instead is/are a copy of the first record.  So the solution (I think) is to add the dlookup inside my recordsource.  However, VBA is erroring this out as bad syntax.  Can someone look at my syntax and comment on this approach?

Private Sub Form_Open(Cancel As Integer)

Dim strLastname As String

Dim strFirstname As String

Dim strFullName As String

Dim strMgrID As String

strLastname = DLookup("[Lname]", "tblUser", "[UserID] = " & Forms!frmLogin!cboUser)

strFirstname = DLookup("[Fname]", "tblUser", "[UserID] = " & Forms!frmLogin!cboUser)

strFullName = strLastname & ", " & strFirstname

strMgrID = Forms!frmLogin!cboUser

Select Case Forms!frmMainMenu.FrameEE

Case 1

    Me.RecordSource = "SELECT tblEmployee.EmplID, tblEmployee.EmplName, tblEmployee.JobCode," & vbCrLf & _

    "DLookup("Descr", "tblJobCostCtrCode", "[Code]=" & Me.jobcode) as JobDescr" &vbCrLf & _                                    'this is the added line

    "tblEmployee.CostCtr, tblEmployee.DOH, tblEmployee.DOT, tblEmployee.MgrID, tblEmployee.DirID " & vbCrLf & _

    "FROM tblEmployee WHERE (((tblEmployee.DOT) is null) and ((tblEmployee.MgrID)=" & strMgrID & ")) ORDER BY tblEmployee.EmplName ;"

...

Me.JobDescr.ControlSource= "JobDescr"

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

11 answers

Sort by: Most helpful
  1. Anonymous
    2010-09-27T15:59:51+00:00

    If the line is displayed in red, then the VBA statement is syntactically incorrect.  I thought it was the resulting SQL statement that was in error.

    Because of line wrapping, I can't tell exactly what your code looks like, but maybe the problem is a missing _ at the end of the line with DLookup??

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-09-27T14:32:34+00:00

    The code is located in the OnOpen EP on the form.  When I run debug compile the whole me.recordsource is in red font and the term Descr  in my second row is highlighted.  It appears that the new Dlookup line is causing the problem.  Not sure how helpful this is but at one point I had this line:

    Me.JobDescr = DLookup("Descr", "tblJobCostCtrCode", "[Code]=" & Me.jobcode)

    at the end of my code (before I tried to insert into Me.Recordsource) and it worked fine. 

    At this point, debug is not available.  Your thoughts?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-09-27T14:17:50+00:00

    Is the Code field a Text field?

    Did you use Debug - Compike menu to compike your code?  If you did, did the cinoiker geve any indication of where the error is located?

    A good way to debug this kind of thing is to use Debug.Print

    Dim strSQL As String

    strSQL = "SELECT tblEmployee.EmplID, ...

    Debug.Print strSQL

    Me.RecordSource = strSQL

    and look at the result in the Immediate window

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-09-27T13:42:58+00:00

    Marshall,

    I inserted your suggestion into my code but still getting a syntax error.  Your thoughts?

        Me.RecordSource = "SELECT tblEmployee.EmplID, tblEmployee.EmplName, tblEmployee.JobCode," & vbCrLf & _

        "DLookup(""Descr"", ""tblJobCostCtrCode"", ""[Code]=" & Me.jobcode & ") as JobDescr," & vbCrLf &

        "tblEmployee.CostCtr, tblEmployee.DOH, tblEmployee.DOT, tblEmployee.MgrID, tblEmployee.DirID " & vbCrLf & _

        "FROM tblEmployee WHERE (((tblEmployee.DOT) is null) and ((tblEmployee.MgrID)=" & strMgrID & ")) ORDER BY tblEmployee.EmplName ;"

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2010-09-27T13:29:34+00:00

    All the quotes inside quotes have to be doubled up:

    "DLookup(""Descr"", ""tblJobCostCtrCode"", ""[Code]=" & Me.jobcode & ") as JobDescr" &vbCrLf &

    Was this answer helpful?

    0 comments No comments