Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 20th, 2003, 01:17 AM
Registered User
Join Date: Jun 2003
Location: Tucson, AZ, .
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?

Pete S - Tucson, AZ
Reply With Quote
  #2 (permalink)  
Old June 20th, 2003, 08:14 AM
Friend of Wrox
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post

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:
Create Procedure GetName
  @EmpID integer
  @CurrentEmp varchar(whatever)
  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:
   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
The command object/stored procedure method is way more efficient since a recordset object is pretty "heavy".

Jeff Mason
Custom Apps, Inc.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 12:05 AM.

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