Results 1 to 5 of 5
  1. #1
    DCV0204 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    85

    DLookUp from unbound textbox or DLookUp from another unbound textbox

    Ok, the code below works for my unbound textbox named SrchMed_ID; but have discovered that sometimes the Med_ID is not recognized in my table when it is present when performing a search, so I have decided to added an additional unbound textbox named srchnpi to my form. I am wanting to be able to enter a number (txt) into either SrchMed_ID or srchnpi click my button named btnsrch and it search my "Provider" table for the record and perform the docmd's in my code. I have tried changing my code several different ways I have indicated a few in red in my code below and can not figure it out. Should I be using one unbound textbox to accomplish this? Can anyone help me? Thanks.

    Private Sub btnsrch_Click()
    Dim MedID As String
    ' Get Value from Medicaid_ID textbox on the Form:
    Me.btnsrch.SetFocus
    Me.SrchMed_ID.SetFocus
    MedID = Me.SrchMed_ID.Text

    Dim Msg, Style, Title
    Msg = "File Already Exist" & vbNewLine & vbNewLine & "Do not add to database." & vbNewLine & "Do not create another file folder." & vbNewLine & vbNewLine & "Would you like to update the database record. "
    Title = "Information"
    If IsNull(DLookup("[Medicaid_ID]", "Provider", "Medicaid_ID = '" & SrchMed_ID & "'" I tried adding an Or statement here)) Then
    ' NAME is Null because the Medicaid_ID doesn't exist.
    DoCmd.OpenForm "AddNewFile", , , , acFormAdd
    I have also tried to duplicate the dlookup statement above here change the focus to the "NPI_Number" (additional field in table to be searched)
    Else
    Msg = "File Already Exist" & vbNewLine & vbNewLine & "Do not add to database." & vbNewLine & "Do not create another file folder." & vbNewLine & vbNewLine & "Would you like to update the database record. "
    Style = vbYesNo + vbInformation + vbDefaultButton2
    Title = "Information"

    Response = MsgBox(Msg, Style, Title)
    If Response = vbYes Then 'User chose Yes.
    MyString = "Yes"
    DoCmd.OpenForm "UpdateCurrentFile", , , "Medicaid_ID = '" & SrchMed_ID & "'"

    'Else
    'DoCmd.OpenForm "UpdateCurrentFile", , , "NPI = '" & srchnpi & "'"
    If Response = vbNo Then 'User chose No.
    MyString = "No" 'Perform some action.



    End If
    End If
    End If


    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't see the point of the second textbox; I'd try to find out why it sometimes fails. Is there a pattern to when, or what value it misses?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    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
    Code:
    If IsNull(DLookup("[Medicaid_ID]", "Provider", "Medicaid_ID = '" & SrchMed_ID & "'" I tried adding an Or statement here)) Then

    Asking to return the [Medicaid_ID] if the [Medicaid_ID] exists, which is what you're doing, doesn't really make a lot of sense to me. What you want to know is if a Record exists with the given [Medicaid_ID]. This is normally done using DCount, rather than DLookup, and the syntax would be something like this:

    Code:
    If DCount("*", "Provider", "Medicaid_ID = '" & SrchMed_ID & "'") = 0 Then


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

    All posts/responses based on Access 2003/2007

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I have to side with Linq; DCount() would be better than DLookup().

    I did rework your code a little .... (this is untested)
    Code:
    Private Sub btnsrch_Click()
       Dim strMedID As String
       Dim varMed_ID As Variant  ' can hold a NULL
       Dim Msg, Style, Title
    
       ' Get Value from Medicaid_ID textbox on the Form:
       strMedID = Me.SrchMed_ID
       '   Me.btnsrch.SetFocus
       '   Me.SrchMed_ID.SetFocus
       '   MedID = Me.SrchMed_ID.Text
    
       '------------------
       ' for testing - comment or delete when debugging complete
       MsgBox strMedID & ""
       '------------------
    
       varMed_ID = DLookup("[Medicaid_ID]", "Provider", "Medicaid_ID = '" & strMedID & "'")
    
       If Len(varMed_ID & "") = 0 Then
          ' Medicaid_ID doesn't exist.
          DoCmd.OpenForm "AddNewFile", , , , acFormAdd
    
       Else
          ' Medicaid_ID exists.
          Msg = "Medicaid ID Already Exists" & vbNewLine & vbNewLine & "Do not add to database." & vbNewLine & "Do not create another file folder." & vbNewLine & vbNewLine & "Would you like to update the database record. "
          Style = vbYesNo + vbInformation + vbDefaultButton2
          Title = "Information"
    
          Response = MsgBox(Msg, Style, Title)
          If Response = vbYes Then   'User chose Yes.
             DoCmd.OpenForm "UpdateCurrentFile", , , "[Medicaid_ID] = '" & strMedID & "'"
          End If
    
       End If
    
    
    End Sub

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would use DCount() myself, but I don't think that's a factor in why the DLookup() fails intermittently.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 8
    Last Post: 04-12-2013, 08:59 PM
  2. Unbound, form textbox populated
    By WiReLaD in forum Access
    Replies: 7
    Last Post: 11-19-2012, 12:54 PM
  3. Undo Typing in Unbound Textbox
    By June7 in forum Programming
    Replies: 4
    Last Post: 08-29-2012, 12:14 AM
  4. Unbound textbox in bound form
    By Evilferret in forum Forms
    Replies: 5
    Last Post: 08-15-2012, 01:26 PM
  5. Unbound textbox and report linked to vba
    By Shambler2 in forum Programming
    Replies: 7
    Last Post: 06-09-2011, 04:29 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