Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
|
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2008, 04:02 PM
Registered User
 
Join Date: Oct 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get a Single Data Value from a Table

How can I simply capture a value from one of my tables and assign it to a variable for use in a form?

I'm developing a windows app with a local database connection. The database is an MS Access 2003 mdb file, and is functioning basically the same as SQL Server--with a datasource, databindingsource, and tableadapter.

One of my forms has no bound controls (however, I can add the databindingsource if necessary). I need to capture a value from one of my tables and assign it to a variable for use in the form.

Here's what I want to get:
select ParentID from Position where PositionID = '1'

MS Access has a VB function that does it in one line:
var = DLookup ( expression, domain, [criteria] )
or
var = DLookup ( "ParentID", "Position", "PositionID = '1'" )

Doesn't VB2005 have something similar? Or how can I issue a simple sql command?

Thanks.

pencilneck



 
Old January 8th, 2008, 02:55 PM
Registered User
 
Join Date: Oct 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found the answer:

Public Class Form1
Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Data.mdb"

Private Sub Form1_Load(...)
Sql1 = "select MainPosID from Options where OptionID = 1"
var = LookupString(Sql1)

Function LookupString(ByVal Sql1 As String)
Dim Ans As String
Using Con As New OleDbConnection(ConStr)
Dim Cmd As New OleDbCommand(Sql1, Con)
Try
Con.Open()
Ans = Cmd.ExecuteScalar()
Con.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
Return Ans
End Function








Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple views of single table jatatman ASP.NET 3.5 Basics 1 June 2nd, 2008 01:34 AM
html table data single line -- Solved rupen HTML Code Clinic 1 August 8th, 2005 02:26 PM
Concatenate multiple records from table to single Ron Howerton SQL Language 6 August 23rd, 2004 08:49 AM
Storing complete table rows in a single field KenNeedham Oracle 2 January 22nd, 2004 01:45 PM
RDS Single Record and Table Binding with Access qwerty Classic ASP Databases 0 July 15th, 2003 02:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.