473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Variable Recordset Fields

I would be grateful if someone could give me a direction in solving
this problem. Here is the simplified setup for the code that I am
trying to run in an Access form. STRFLD is an array that I assigned
earlier. I am trying to access for example rs!Fields!AREG or
rs!fields!ASER depending on what STRFLD was supplied earlier in the
code. To save space I shortened my SQL and code quite a bit, but I hope
this will give you a picture of what I am trying to do.

Dim db As Database
Dim rs As Recordset

STRSQL = "SELECT mms.[LINKNUM], TableA.[SER] AS ASER, TableB.[SER] AS
BSER FROM TableA LEFT JOIN TableB ON TableB.LINKNUM = TableA.LINKNUM
ORDER BY TableA.SER;"

Set db = CurrentDb()
Set rs = db.OpenRecordset(STRSQL, dbOpenDynaset)
icol = 1
I have tried the following

strdat = rs!Fields("A" & STRFLD(icol)) ' I get a run-time error
3265 Item not found

strdat = rs.fields(fldnm) ' where fldnm = "A" & STRFLD(icol) again
I get error 3265

strdat = Controls("rs!Fields(" & fldnm & ")") ' Get a error 2465 can't
find field 'rs!Fields(AREG)'

I have even tried CallByName and countless other combinations of the
above examples. I know the field exists in the database. I know
STRFLD(icol) was set right because on debug I can see it has the right
value. I have searched help as well as many newsgroup postings and
have not found my solution. Have I overlooked something? Is it
possible to have a dynamic field in a recordset?

Thank you very much!

-Matt

Sep 29 '06 #1
2 9189
Hi Matt,

not completely clear on what you are trying to do, but here is a shot in
the dark:

Dim arrFlds As Variant, DB As DAO.Database, RS As DAO.Recordset, i As
Integer, j As Integer, k As Integer, strSql As String

arrFlds = Array("fld1", "fld2", fld3",..."fld10")
if something then
i = somenum
j = anothernum
k = someothernum
End If

strSql = "Select t1." & arrFlds(i) & ", t1." _
& arrFlds(j) & ", t2." & arrFlds(k) & " From tbl1 t1 " _
& "Join tbl2 t2 On t1.ID = t2.ID Where..."

Set DB = CurrentDB
Set RS = DB.OpenRecordSet(strSql")
Do While Not RS.EOF
...
RS.MoveNext
Loop
Just remember that a sql string is just a string, and you can build it
any way that you want. You can concatenate whatever you want to your
string - just make sure that it is a valid sql string. Then you can
assign it as a recordsource to your recordset object. You build the
string first - dynamically - on the fly - whatever - then you assign it.

Note: if your string is a valid sql string, you can retrieve the formed
string by using Debug.Print strSql and copy it from the debug window.
Then you can paste it into the sql window of the query builder tool and
run the query. If it is correct, the query will run. If it is not
correct - well - the query won't run, but you could switch the sql
window to the query graphical view and that should/might tell you where
the problem is.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Sep 29 '06 #2

Rich,

Thank you for your response! I followed your advice and rechecked the
SQL which I thought I did earlier. I found that when I tried to
simplify what I was doing I somehow undid a change i made previously to
the SQL variable that addressed one of the fields I was trying to
reference. The field I was referenceing in fact was left out of the
SQL. I fixed the SQL string and now the following works -

dim fldnm as string
fldnm = "A" & STRFLD(icol)
astring = rs.Fields(fldnm)

Thank you again for your response Rich, you got me to sit back and look
at what I was doing and recheck the obvious (that I wrongly assumed I
recently checked)! I will try to double check all of my code next time
before I pose a question! THANK YOU!

Best,

Matt

Sep 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Adrian Parker | last post by:
Hi. I would like to query a database, given several where clauses to refine my search, and return the value of one single field in the database. eg: I have a table that lists teachers. Their...
6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
4
by: Michael Haas | last post by:
I'm trying to use a variable of retrieved via request from a form to select records.I'm pretty sure I am having a basic syntax error or something else basic. I retrieve the variable from a ...
19
by: laurenq uantrell | last post by:
I'm using Access 2K. I'm hoping someone can tell me which method performs faster- (currently I'm using a mix of both methods) a.) creating custom properties and then calling functions to set and...
5
by: Newbie in ChiTown | last post by:
I created a subprocedure that includes a recordset as follows: Private Sub Combo17_Click() 'Declaring object and string variables Dim DB As Database Dim rec As Recordset ...
8
by: KelvinA | last post by:
I'm constructing a SQL statement called strTxSQL that is supposed to use two variables: lngItemCode (long integer) and datMyDate (date defined as MM/DD/YYYY). The SQL statement will not work with...
6
by: Oko | last post by:
I'm currently developing an MS Access Data Project (.adp) in MS Access 2002. One of the reports within the DB uses data that is Dynamic and cannot be stored on the SQL Server. To resolve this, I...
1
by: john | last post by:
LINQ works a lot like an Access DB Recordset in the way it functions, one of the things I could do in Access is refer to the Fields thru a Variable as below allowing me to Iterate over the...
2
by: Whasigga | last post by:
Hello I am working on a project to export data from a query into an Excel file. I have a form with a button on it and the button when clicked runs a function that is supposed to run the query and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.