Dlook up from query if empty cancel (1 Viewer)

theinviter

Registered User.
Local time
Today, 07:25
Joined
Aug 14, 2014
Messages
240
Hi guys;

need help please , i designed a form with text field once the user update this text filed with Number (ID) then all field on the form will be filled with data form query based on the ID entered but if the ID not present i need to cancel update and show message box " no record found"

how can i do that.
this the code below .


Private Sub Command1487_Click()


[Drug Name] = DLookup("[Drug Name]", "Recycle_query", "[ID] = " & Me.Text1477 & "")
Order_Date = Now()

End Sub



if the ID dos not exit the to cancel update
 

isladogs

MVP / VIP
Local time
Today, 15:25
Joined
Jan 14, 2017
Messages
18,209
Try this

Code:
Private Sub Command1487_Click()
      
Dim strText as String

StrText = Nz(DLookup("[Drug Name]", "Recycle_query", "[ID] = " & Me.Text1477 & ""),"")
If strText<>"" Then
  [Drug Name] = strText
  Order_Date = Now()
Else
  MsgBox "No record found", vbCritical
End if 

End Sub

You really should name your controls something meaningful rather than Command1487
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:25
Joined
Oct 29, 2018
Messages
21,456
Hi. Another option/approach, which I tend to use/prefer more often is DCount() over DLookup(), for cases like this. Cheers!
 

Users who are viewing this thread

Top Bottom