Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33

    SimpleDLookup - "So I thought"

    I just trying to do a simple lookup in my UsedbookingNumber table to check if a numbers has been used.
    This code is runs after LostFocus of the BookingNoticeBookingNo field.

    Code:
    DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = _
    " & Forms![frmAsiaBooking]![BookingNoticeBookingNo])
    I have tried many variations and get errors like:
    "Type Mismatch" ,
    "The expression you entered as a query parameter produced this error: WLK533741"
    Which happens to be first number in my used number table.
    and Runtime error 3075. Syntax error (missing operator) "[UsedBookingNum] = "
    I have verified all fields are data type "Text"

    These are some tests from my Immediate window:
    ? DLookup("[UsedBookingNum]", "UsedBookingNumber")
    WLK533741 - First number in used number table

    ?Forms![frmAsiaBooking]![BookingNoticeBookingNo]
    WLK533748 - Number from active form

    Thank you in advance for any and all suggestions

    OldCityCat

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try:
    Code:
    DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = _
    '" & Forms![frmAsiaBooking]![BookingNoticeBookingNo] & "'")
    Here's a link I always reference: http://www.mvps.org/access/general/gen0018.htm

  3. #3
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33

    SimpleDLookup - "So I thought"

    RuralGuy,
    Thanks for your response,
    This was one of my variations on the code, funny thing when I run it in the immediate window is it returns the booking number the on my open form.

    Code:
    ? DLookup("[UsedBookingNum]", "UsedBookingNumber", "UsedBookingNum] = '" _ 
    & Forms![frmAsiaBooking]![BookingNoticeBookingNo] & "'")
    WLK533741
    But when I run it from the form I get a "Run Time Error 13 Type mismatch".
    I'm at a loss so, In case I'm missing something here my complete sub:
    Code:
    Private Sub BookingNoticeBookingNo_LostFocus()
     If DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" _ 
    & Forms![frmAsiaBooking]!BookingNoticeBookingNo & "'") Then
            DoCmd.CancelEvent
    MsgBox "This Number has already been used!", vbExclamation, "Error"
    Me![cboBookingNoticeShipLine].SetFocus
        SendKeys "{TAB}", 0.25
            End If
    
    End Sub
    Thanks
    OldCityCat

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    It sounds like the field is not normalized so you are data sensitive. try:
    If DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" _
    & Nz(Forms![frmAsiaBooking]!BookingNoticeBookingNo, "") & "'") Then

  5. #5
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33

    SimpleDLookup - "So I thought"

    So far so good - I will continue my testing and let you know.
    Thanks again

    OldCityCat

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Glad to help. You may want to mark this thread as Solved and start a new one later.

  7. #7
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    Well after further test I'm still getting a mismatch error,
    This happens when adding a new record, and using a booking number that I know is in the UseBookingNumber table.
    Obviously I should get "This Number has already been used!" message.
    The code works correctly when adding a new booking number.
    I'm not sure where to go from here.

    Thanks
    OldCityCat

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    DLookup() returns a value and your code is not looking for it. Try:
    [code]If Len(DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" & Nz(Forms![frmAsiaBooking]!BookingNoticeBookingNo, "") & "'")) > 0 Then

  9. #9
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    What I don't understand is why the code works correctly in the immediate window.

    [Code]
    ? DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" & _
    Nz(Forms![frmAsiaBooking]!BookingNoticeBookingNo, "") & "'")
    WLK3643601
    ? DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" & _
    Nz(Forms![frmAsiaBooking]!BookingNoticeBookingNo, "") & "'")
    WLK533741
    ? DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" & _
    Nz(Forms![frmAsiaBooking]!BookingNoticeBookingNo, "") & "'")
    WLK534729
    [Code]
    These are samples of the immediate window results, all three booking numbers are from
    thee different records that were the current record on the form at the time I ran the code in the immediate window.

    [Code]
    ? If DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" & _ Nz(Forms![frmAsiaBooking]!BookingNoticeBookingNo, "") & "'") Then
    DoCmd.CancelEvent
    MsgBox "This Number has already been used!", vbExclamation, "Error"
    [Code]
    When I ran this from the immediate window it returned my MsgBox text.

    Why would running it from my form cause the Type Mismatch?

    OldCityCat

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The "?" in the immediate window is displaying the returned value. Try the immediate window without the "?" and see what happens.

  11. #11
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    I get
    "Compile Error"
    Expected:=

    from
    DLookup("[UsedBookingNum]", "UsedBookingNumber", "[UsedBookingNum] = '" _
    & Nz(Forms![frmAsiaBooking]!BookingNoticeBookingNo, "") & "'")

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Exactly! Did my suggestion in Post #8 work for you?

  13. #13
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    Yes
    Sorry,
    I should have tried it before sending my last posts.

    Please explain how this DLookup code works.

    Thanks

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    DLookup() returns either the value you are looking for or a Null if not located. To test for a return you need code that can handle either and Len() fills the bill nicely. Does that explain it for you?

  15. #15
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    Sorry but I not getting it!
    In my testing (see below) DLookup should have found a match ,but still returned an error?
    where does Null come into play?
    So LEN looks for any string grater then 0?

    type mismatch error,
    when adding a new record, and using a booking number that I know is in the UsedBookingNumber table.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2010, 10:22 AM
  2. Replies: 21
    Last Post: 06-03-2009, 05:54 PM
  3. aSTR = Dir("C:\*.*") >> "Type Mismatch"
    By JGrant in forum Programming
    Replies: 1
    Last Post: 03-28-2009, 05:17 AM
  4. Replies: 2
    Last Post: 08-31-2006, 12:19 PM
  5. "Count" and "Countif" in Reports
    By JMantei in forum Reports
    Replies: 1
    Last Post: 06-20-2006, 02:20 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