Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 Stringstr = "FirstName"
name = rs![str]----->The error occurs here.However this code will work:
dim rs as recordset
dim name as Stringname = 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

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

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??

![]() |
![]() |
![]() |

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