Results 1 to 10 of 10
  1. #1
    lukekelly is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    8

    DLookup code not working


    Hi all,

    I have this code run through a macro to open a project:

    Code:
    Function OpenAProject()
      
      Dim p
      p = InputBox("Please enter Project Number", "Open a Project")
      If p > "" Then
        p = UCase(p)
        If DLookup("[Project Number]", "Projects", "[Project Number] = '" & p & "'") = p Then
          DoCmd.OpenForm "Projects", , , "[Project Number] = '" & p & "'"
        Else
          MsgBox "Sorry - " & p & " isn't a valid project number", vbExclamation, "CPAMS"
        End If
      End If
      
    End Function
    I have a table called Projects, the "UID" is the Project Number column, and also have a form called Projects. The above code produces a box to prompt for the Project number and then to open that record up. However im getting an error:

    "Data type mismatch in criteria expression"

    and the following line is highlighted as the problem:

    Code:
    If DLookup("[Project Number]", "Projects", "[Project Number] = '" & p & "'") = p Then
    I'm perplexed as I'm sure I have used this code before in other projects. If anyone has any pointers or could assist me that would be great.

    Thanks


    Luke
    Last edited by lukekelly; 06-15-2010 at 02:08 AM. Reason: Code

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    DLookup("[Project Number]", "Projects", "[Project Number] = '" & p & "'") = "p"

    try this

  3. #3
    lukekelly is offline Novice
    Windows 7 Access 2003
    Join Date
    Jan 2010
    Posts
    8
    Hi Maximus thanks for your reply,

    I have tried the quotations around the p with still no effect,

    Is there anything else you could possibly suggest?

    Thanks

    Luke

  4. #4
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Project Number is that a Field with field Type text or integer.

  5. #5
    lukekelly is offline Novice
    Windows 7 Access 2003
    Join Date
    Jan 2010
    Posts
    8
    Hi Maximus,

    Its an autonumber integer

    Thanks

  6. #6
    lukekelly is offline Novice
    Windows 7 Access 2003
    Join Date
    Jan 2010
    Posts
    8
    Formatting wouldn't be an issue would it?

  7. #7
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Now here are some issues that you need to look into:

    Sure it can be an Issue is that is a autonumber, I would have my lookup as :
    DLookup("[Project Number]", "Projects", "[Project Number] =" & p ) = p

  8. #8
    lukekelly is offline Novice
    Windows 7 Access 2003
    Join Date
    Jan 2010
    Posts
    8
    Hi Maximus,

    Yes I now realised that the last time I used this code it was for a text UID where the project number was assigned using another piece of code,

    I have tried your suggestion thanks, It takes me a step further however I'm just getting a runtime error now - "You cancelled the previous operation"

    Looking into it now

    Thanks

  9. #9
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    I would rather have the code like this:

    I have assumed:
    1) Project Number is a integer
    2) P has to be integer
    3) Search whether The projec number exist.
    4) if yes open a form where Project Number = intp
    5) Else error msgbox.

    Dim intp as integer
    Dim intCount as Integer

    intp=InputBox("Please enter Project Number", "Open a Project")

    intCount=iif(isNull(Dcount("[Project Number]", "Projects", "[Project Number] = " & intp)),0,Dcount("[Project Number]", "Projects", "[Project Number] = " & intp))

    if intCount=0 then
    MsgBox "Sorry - " & intp & " isn't a valid project number", vbExclamation, "CPAMS"
    else
    DoCmd.OpenForm "Projects", , , "[Project Number] = " & intp

    end if

  10. #10
    lukekelly is offline Novice
    Windows 7 Access 2003
    Join Date
    Jan 2010
    Posts
    8
    Hi Maximus, was half way there

    Works a treat, thanks a lot for your time and help.

    Luke

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

Similar Threads

  1. VB code not working
    By cwwaicw311 in forum Programming
    Replies: 17
    Last Post: 04-26-2010, 07:02 PM
  2. VBA Code for working with IE
    By smikkelsen in forum Programming
    Replies: 3
    Last Post: 04-15-2010, 01:05 AM
  3. Dlookup??
    By Vikki in forum Access
    Replies: 4
    Last Post: 02-16-2010, 07:59 AM
  4. Replies: 4
    Last Post: 05-12-2009, 01:50 PM
  5. Simple Nav Form Code Not Working
    By alsoto in forum Forms
    Replies: 10
    Last Post: 04-10-2009, 09:30 AM

Tags for this Thread

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