Solved

How Do I execute a DLookUp function in VB.Net

Posted on 2014-04-07
3
1,964 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now