Solved

How Do I execute a DLookUp function in VB.Net

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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