Computing.Net > Forums > Programming > VB6 Using Recordset Methods

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VB6 Using Recordset Methods

Reply to Message Icon

Name: BenGrabau
Date: February 2, 2007 at 20:34:52 Pacific
OS: Windows XP
CPU/Ram: 3.2Ghz, 1G RAM
Product: Intel Pent. 4
Comment:

Dear All,

Can someone please give me some ideas on this. When ever i try and retrieve data from an Access Database Table i cannot use a String variable to specify which field. That is, the following code(simplified) will NOT work

dim rs as recordset
dim str as String
dim name as String

str = "FirstName"
name = rs![str]----->The error occurs here.

However this code will work:

dim rs as recordset
dim name as String

name = rs![FirstName]

Even though the string, str , is exactly the same as what I type in the square brackets i cannot get it to return a value. I have tried all different combinations double quotes and single quotes around str but still no joy. Can anyone please tell me how I can go about using a variable inside the square brackets?

Cheers, Ben




Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: February 2, 2007 at 22:21:06 Pacific
Reply:

rs![FirstName] is a field name taken from the recordset. It must be a field name or some other field identifier such a field number. It can't be just any old string variable. As there is no field names called str, it generates an error.

You can use string variables to identify as field name in an SQL statement, but not in a recordset.


Stuart


0

Response Number 2
Name: BenGrabau
Date: February 2, 2007 at 23:14:19 Pacific
Reply:

Thanks Stuart,

Yeah thats right, FirstName would be an existing field in the recordset rs. If the field I want to access is specified by a variable and i cant use rs![Variable] to return a given value how would i access the field using SQL to do this?

I can use SQL to populate a recordset but then the only way i know how to get the data out of this queried recordset is by using rs![].

Here is an exert of my actual code:

public Function GetPlayerTips(strRound as string) as string

Dim strFieldTitle As String
Dim rs As Recordset
Dim strSQL As String


strFieldTitle = "Round1"'--->This will vary

strSQL = "SELECT ""strFieldTitle"" FROM [Tips] WHERE [TipsPlayerID] = 1"

Set rs = mdbCurrentDatabase.OpenRecordset(strSQL)

'rs will consist of one Field "strFieldTitle" and one record i.e One cell containing a string

rs.MoveFirst

'------->How would I access this cell's data??


0

Response Number 3
Name: StuartS
Date: February 3, 2007 at 00:45:13 Pacific
Reply:

name = rs![FieldName]

Name being a string variable

Stuart


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VB6 Using Recordset Methods

vb6 db queries from ms access www.computing.net/answers/programming/vb6-db-queries-from-ms-access/3787.html

How do i Access SQL2000 using VB6 www.computing.net/answers/programming/how-do-i-access-sql2000-using-vb6/7710.html

Messenger type program using vb6 www.computing.net/answers/programming/messenger-type-program-using-vb6/7348.html