Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics 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 June 20th, 2003, 01:17 AM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Setting a Variable = Result of a SQL Query

I want to retreive one piece of text data from the database and assign it to a String variable. The SQL statement that would successfully retreive the correct piece of data would be something like:

"SELECT FirstName FROM Employee WHERE EmpID = x"

where x would be a value set in the VB application.
How would I set a variable, say "CurrentEmp", = to the result of that query?

TIA,
Pete S - Tucson, AZ
 
Old June 20th, 2003, 08:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You don't indicate your database environment, so this may have to be adapted for your particular situation.

There are two ways.

1. Open a recordset from the query and read the row. Recordset.fields(0).value will have the value returned, as:

CurrentEmp = Recordset.fields(0).value

2. Use a command object and execute a stored procedure which returns a string parameter. A SQL Server stored procedure definition might be along the lines of:
Code:
Create Procedure GetName
  @EmpID integer
  @CurrentEmp varchar(whatever)
as
  set nocount on
  SELECT @CurrentEmp=FirstName FROM Employee WHERE EmpID = @EmpID;
The command object has two parameters: an input parameter giving the EmpID you want to select, and an output parameter which will contain the result. Something like:
Code:
   Dim cmd as Command
   Set cmd = New Command
   With cmd
      .CommandType = adCmdStoredProc
      .CommandText = "GetName"
      .Parameters.Append .CreateParameter(, adInteger, dParamInput, ,EmpID)
      .Parameters.Append .CreateParameter(, adVarChar, adParamOutput, <lengthofname>)
      Set .ActiveConnection = <your connection>
   End With
   cmd.Execute , , adExecuteNoRecords
   CurrentEmp=cmd.Parameters(1).value
The command object/stored procedure method is way more efficient since a recordset object is pretty "heavy".

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing an SQL query and using it's result Andrew.Berry ASP.NET 2.0 Professional 5 April 14th, 2008 08:25 AM
get the execute query result in variable mahen_pali SQL Server 2005 2 April 7th, 2008 04:04 AM
Query Result to Session Variable johnp ASP.NET 2.0 Basics 1 April 6th, 2006 11:15 PM
pass the result of a query to a variable ... eusebio Pro VB Databases 6 March 30th, 2006 07:41 PM
Paging of Sql Query Result. SubodhKumar SQL Language 4 December 13th, 2003 06:58 PM





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