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
Solved

How Do I execute a DLookUp function in VB.Net

Posted on 2014-04-07
3
2,086 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlServer no dupes 25 37
VB.net and sql server 4 45
Convert Ctime to date time in textfile? 7 30
Code enhancement 4 20
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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