Results 1 to 6 of 6
  1. #1
    Rizvi is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    12

    dlookup for autonumber field

    Hello Everyone
    Just stuck in below query, i know that it is simple but could not figuring it out. i have a text box "txnsearch" in a form in which user will input a value. i need to search this value in a table field "Txn_number" which is an autonumber field to see if the search record is there in table "tbl_fxmain" or not. i have put code to check the null value in the text box but am trying to check the text box value in the table as per below code but it is giving me error. if any one can help me out keeping in view the Txn_number is an autonumber type field in table.

    Thanks in advance.


    Private Sub Command5_Click()
    Dim txnsearch As Long
    txnsearch = (DLookup("Txn_number", "tbl_fxmain", "Txn_number= " & "txtsearch"))




    If IsNull(txtsearch) Or IsNull(txnsearch) Then
    MsgBox (" Please enter Transaction Number "), vbOKOnly
    txtsearch.SetFocus

    Else

    DoCmd.OpenForm "frm_user_edit", , , "[Txn_number]=" & txtsearch
    End If


    End Sub

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    If it is numeric(autonumber), then you don't need the 2nd quote.

    txnsearch = DLookup("Txn_number", "tbl_fxmain", "Txn_number= " & txtsearch)

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You say textbox is txnsearch but then your code uses txtsearch and declares txnsearch as a variable.

    The txnsearch variable can never be Null. Only Variant type variable can hold Null. Code will have runtime error if the DLookup returns Null. But if txtsearch is null, then certainly the DLookup will return Null.

    Don't put reference to textbox within quote marks.

    Code:
    Private Sub Command5_Click()
    If IsNull(DLookup("Txn_number", "tbl_fxmain", "Txn_number= " & Me.txtsearch)) Then
       MsgBox (" Please enter Transaction Number "), vbOKOnly
       Me.txtsearch.SetFocus
    Else
        DoCmd.OpenForm "frm_user_edit", , , "[Txn_number]=" & Me.txtsearch
    End If
    End Sub
    However, use a LimitToList combobox instead of textbox and users can only enter valid txn number and DLookup would not be necessary.

    Suggest you give button more meaningful name than Command5.
    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.

  4. #4
    Rizvi is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    12
    Quote Originally Posted by June7 View Post
    You say textbox is txnsearch but then your code uses txtsearch and declares txnsearch as a variable.

    The txnsearch variable can never be Null. Only Variant type variable can hold Null. Code will have runtime error if the DLookup returns Null. But if txtsearch is null, then certainly the DLookup will return Null.

    Don't put reference to textbox within quote marks.

    Code:
    Private Sub Command5_Click()
    If IsNull(DLookup("Txn_number", "tbl_fxmain", "Txn_number= " & Me.txtsearch)) Then
       MsgBox (" Please enter Transaction Number "), vbOKOnly
       Me.txtsearch.SetFocus
    Else
        DoCmd.OpenForm "frm_user_edit", , , "[Txn_number]=" & Me.txtsearch
    End If
    End Sub
    However, use a LimitToList combobox instead of textbox and users can only enter valid txn number and DLookup would not be necessary.

    Suggest you give button more meaningful name than Command5.
    Thanks for the reply
    i have tried the above code, it is working fine if user is inputs any value but when the txtsearch is blank it is giving me below error, if you can help me out on this.
    "run-time error 3075 syntax error (missing operator) in query expression 'Txn_number='.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    You could check and see if "txtsearch is blank" and prevent the error/ask a question/give a message.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Sorry, should have suggested:

    If IsNull(DLookup("Txn_number", "tbl_fxmain", "Txn_number= " & Nz(Me.txtsearch,0))) Then
    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: 7
    Last Post: 08-28-2017, 02:50 PM
  2. Replies: 3
    Last Post: 05-21-2014, 05:38 PM
  3. Replies: 4
    Last Post: 11-21-2013, 05:06 PM
  4. Replies: 1
    Last Post: 09-25-2012, 03:58 AM
  5. Autofill field based on autonumber field
    By frevilla in forum Forms
    Replies: 1
    Last Post: 09-11-2009, 02:50 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