Solved

How Do I execute a DLookUp function in VB.Net

Posted on 2014-04-07
3
2,141 Views
Last Modified: 2014-05-04
I have been using the following code in access for a number of years, now that i have moved to VB.net i need the equivalent, is there such a function?

If IsNull(DLookup("[txtname]", "Advisories", "[txtname]='" & Me![ReportedBy] & "'")) Then
Exit Sub
End If

Open in new window

0
Comment
Question by:mickeyshelley1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 39984162
the equvlilant of Dlookup in VB.NET is to use a SQL Query
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 39984184
an example would be


 Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Errorbox.Text = ""

        Try
            con.ConnectionString = "Data Source=XXXXX;Initial Catalog=EAFiles;Persist Security Info=True;User ID=Login;Password=XXXXXXX"
            con.Open()
            cmd.Connection = con
            cmd.CommandText  "SELECT name, advisoroes WHERE Name = txtname"
cmd.ExecuteNonQuery()

if isnull then
exit sub
end if

Catch ex As Exception
                        Errorbox.Text = "Error while inserting record on table..." & ex.Message
        Finally
            con.Close()
        End Try  

endsub
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39985618
A better, working example would be

Public Function DLookup(TableName As String, FieldName As String, FieldValue As String) As Object
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim retVal As Object = Nothing

Try
     con.ConnectionString = "Data Source=XXXXX;Initial Catalog=EAFiles;Persist Security Info=True;User ID=Login;Password=XXXXXXX"
     con.Open()
     cmd.Connection = con
     cmd.CommandText  "SELECT Count(*) FROM " & TableName & " WHERE " & FieldName & " = '" & FieldValue & "'"
     retVal = cmd.ExecuteScalar()
Catch ex As Exception
  Msgbox "ERROR: " & VBCRLF & ex.Tostring
Finally
  cmd.Dispose()
  con.Close()
End Try  
Return retVal
End Function

Open in new window



Note: This method is not complete as it does not perform any validation on the input which may leave it open to SQL Injection attacks and it only deals with string (varchar) fields currently and so is only intended as an example
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question