Solved

How Do I execute a DLookUp function in VB.Net

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

15 Experts available now in Live!

Get 1:1 Help Now