473,395 Members | 1,608 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.

How to use variable to reference rst!Item?

Here's how the loop would look *without* variables - I've hardcoded
FirstName, LastName and Company

Do While Not rstSource.EOF
strSql = "INSERT INTO [tblTest] SELECT rst!FirstName " & _
"AS FirstName, rst!LastName AS LastName, rst!Company " & _
"As Company FROM tblSource"
db.Execute strSql
Loop

The problem is, I don't know what the rst items will be (not always
FirstName, etc.) I have other code that tests for the items and builds the
rst, but I don't know what they are. So I want to use variables like this:

'pseudo code
Do While Not rstSource.EOF
strSql = "INSERT INTO [tblTest] SELECT rst!strVar1 " & _
"AS " & strVar1 & ", rst!strVar2 AS " & strVar2 & ", rst!strVar3 " &
_
"As " & strVar3 & " FROM tblSource"
db.Execute strSql
Loop

But how do I get rst!strVar when strVar is a variable?

Thanks in advance.
Nov 13 '05 #1
4 2628
Maybe it's just me, but this post is really confusing. If you're
inserting records from one table to another, then why are you aliasing
the fields using AS?

If you don't know what the field names are, then you can do something
like

rst.Fields(intCounter)

It might help if you backed up a little bit and gave an example of your
table structure (tblTest) and some sample data (just a single record
should suffice).

Can what you are attempting be done? I think so, but it's very hard to
tell exactly what you want from your description.

Nov 13 '05 #2
On Sun, 31 Jul 2005 17:29:15 -0700, "deko" <de**@nospam.com> wrote:
Here's how the loop would look *without* variables - I've hardcoded
FirstName, LastName and Company

Do While Not rstSource.EOF
strSql = "INSERT INTO [tblTest] SELECT rst!FirstName " & _
"AS FirstName, rst!LastName AS LastName, rst!Company " & _
"As Company FROM tblSource"
db.Execute strSql
Loop

The problem is, I don't know what the rst items will be (not always
FirstName, etc.) I have other code that tests for the items and builds the
rst, but I don't know what they are. So I want to use variables like this:

'pseudo code
Do While Not rstSource.EOF
strSql = "INSERT INTO [tblTest] SELECT rst!strVar1 " & _
"AS " & strVar1 & ", rst!strVar2 AS " & strVar2 & ", rst!strVar3 " &
_
"As " & strVar3 & " FROM tblSource"
db.Execute strSql
Loop

But how do I get rst!strVar when strVar is a variable?

Thanks in advance.


This'll get you pointed in the right direction...

Dim fld As DAO.Field
For Each fld in rstSource.Fields
Debug.Print fld.Name
Next
Nov 13 '05 #3
deko wrote:
Here's how the loop would look *without* variables - I've hardcoded
FirstName, LastName and Company

Do While Not rstSource.EOF
strSql = "INSERT INTO [tblTest] SELECT rst!FirstName " & _
"AS FirstName, rst!LastName AS LastName, rst!Company " & _
"As Company FROM tblSource"
db.Execute strSql
Loop

The problem is, I don't know what the rst items will be (not always
FirstName, etc.) I have other code that tests for the items and builds the
rst, but I don't know what they are. So I want to use variables like this:

'pseudo code
Do While Not rstSource.EOF
strSql = "INSERT INTO [tblTest] SELECT rst!strVar1 " & _
"AS " & strVar1 & ", rst!strVar2 AS " & strVar2 & ", rst!strVar3 " &
_
"As " & strVar3 & " FROM tblSource"
db.Execute strSql
Loop

But how do I get rst!strVar when strVar is a variable?

Thanks in advance.

You can use variables
Dim strField As String
strField = "SomeField"
rst(strField) = "SomeData"
Nov 13 '05 #4
> You can use variables
Dim strField As String
strField = "SomeField"
rst(strField) = "SomeData"


That was easy.

What I'm trying to do is create an SQL statement based on fields in a
particular table. But I never know what fields are in the table. So I loop
through the Fields collection to get a string of comma-separated field
names.

Here's what I came up with (not tested yet)

Dim astrFld() As String
astrFld = Split(strFields, ",")
Set rstSource = db.OpenRecordset(strSql)
Do While Not rstSource.EOF 'loop through each row in source table
For Each varFld In astrFld
strFld = Trim(varFld)
If Not IsNull(rstSource(strFld)) Then
strValue = Chr(34) & rstSource(strFld) & Chr(34)
Else
strValue = Chr(34) & Chr(34)
End If
strValues = strValues & IIf(Len(strValues) <> 0, ", " & strValue,
strValue)
Next varFld
strSql = "INSERT INTO tblOrders ( " & strFields & " ) VALUES ( " &
strValues & " );"


Nov 13 '05 #5

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

Similar topics

6
by: deko | last post by:
Set strFoundCt = ("SELECT COUNT(*) FROM (SELECT DISTINCT Entity_ID FROM qrySearch" & strPxSx) This doesn't work, but hopefully describes what I'm trying to do. I have several different queries...
2
by: K Williams | last post by:
I recently discovered that you can do the following: if currentdb.openrecordset ("select statement.....").eof = true then for cases when you just want to see if any records exist. This...
1
by: JMCN | last post by:
hello- i received an runtime error message: You tried to call Update or CancelUpdate or attempted to update a Field in a recordset without first calling AddNew or Edit. (Error 3020). when i look...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
9
by: ineedahelp | last post by:
Can anyone help me? I am getting a syntax error on my strSQL statement. I think it doesn't like the way I am trying use my variable mNewDate. The user originally types the date as a string...
2
by: Jim | last post by:
Hello: I have an Access database with a table with sequentially numbered fields (e.g., Q10_1, Q10_2, Q10_3, etc. What I want to do is cycle through the variables to set them to values. What I...
10
by: nspader | last post by:
I want to start out saying I am a novice code writer. I am trying to send a report via email based on each supplier. The code below is what I am using. I need to base recordset on Form, report...
1
by: Kev | last post by:
Hello All, I am attempting to use the variable "AccAllow " which is populated by opening a recordset. I am using AccAllowed to refer to a control on the active form (the Switchboard) but I am...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
13
by: ChrisD76 | last post by:
Hi, I am new to using VBA, and have been working with DAO Recordsets. I'm working on a little problem, and think that DAO Recordsets are the solution. I've been playing around with them to try and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.