473,395 Members | 2,795 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.

Syntax for code convert fieldnames to values in field

Hi,

In the code below I scroll throught the firldnames of a query ignoring
the first three then converting the remaining fields to first: the
fieldnames as a record in a field called Product (e.g- Fieldname is
BEER, convert to BEER as the record value in a field called product)
Second: The values of the field to be populated in a field called
Baseline Units. I capture the fieldname as a string but when the code
below tries to populate the Products field it pastes the field values
instead of the field name string of BEER.

I assume it is either in the syntax of the SQL statement or the string
type is incorrect. Please help.

Thanks and regards,

Dean

Sub Convert()

Dim db As Database
Dim qryProd As QueryDef
Dim SQL As String
Dim fldProd As Field
Dim strProd As String
Dim strProd1 As Variant
Set db = CurrentDb()
Set qryProd = db.QueryDefs("Query1")

For Each fldProd In qryProd.Fields

strProd = fldProd.Name

If strProd <"Planning Customer" Then
If strProd <"Business Plan Id" Then
If strProd <"Week Number" Then

DoCmd.SetWarnings False
SQL = "INSERT INTO calc_Output ( [Planning Customer], [Business Plan
Id], [Week Number], Product,[Baseline Units] ) " & _
"SELECT Query1.[Planning Customer], Query1.[Business Plan Id], Query1.
[Week Number], " & strProd & " AS Product, Query1.[" & strProd & "] AS
[Baseline Units]" & _
"FROM Query1;"

DoCmd.RunSQL SQL

Else

End If
End If
End If

Next

DoCmd.SetWarnings True
MsgBox ("Update Complete")

Exit Sub

End Sub

Jan 31 '07 #1
1 2028
<SNIP>
For Each fldProd In qryProd.Fields

strProd = fldProd.Name

If strProd <"Planning Customer" Then
If strProd <"Business Plan Id" Then
If strProd <"Week Number" Then

DoCmd.SetWarnings False

SQL = "INSERT INTO calc_Output ( [Planning Customer], [Business Plan
Id], [Week Number], Product,[Baseline Units] ) " & _
"SELECT Query1.[Planning Customer], Query1.[Business Plan Id], Query1.
[Week Number], '" & strProd & "' AS Product, Query1.[" & strProd & "]
AS [Baseline Units]" & _
"FROM Query1;"

DoCmd.RunSQL SQL.

Easy problem. You need to delimit string literals with single quotes
in SQL.

eg
....cQUOTE & strProd & cQUOTE...
it's one of those things that the query engine/grid whatever does for
you behind the scenes, so if you build your own SQL, you have to put
it in.

Testing the first 3 fields... you _could_ use something like
For intCounter = 2 to qdf.fields.count-1
'do your thing
next intCounter

which would skip the first 3 fields in the querydef... if you want to
test it, try something like

for intCounter = 0 to qdf.Fields.Count-1
debug.print qdf.fields(intCounter).Name
next intCounter

Feb 2 '07 #2

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

Similar topics

4
by: Little PussyCat | last post by:
Hello, I have had a request, one of our tables is used as a report and I have been asked that all fieldnames for months have dashes in them, like Jan-05 instead of Jan05 and so on... Now what...
1
by: Jason | last post by:
I've got two different tables with similar data but the fieldnames are different and laid out slightly different. Is there a quick way to build an append query where maybe I could draw lines...
4
by: Patrick Arkins | last post by:
I have a Main form (named FRM_Main in the Forms Window) with a Subform (named FRM_User_Search in the Forms window, form_User_Search on Forms_Main). FRM_User_Search has a combo-box (cboSort) the...
3
by: Beren | last post by:
Hello, I have an sproc to query a couple of tables, that have the same fieldnames, for example DateCreated. ------------------------------------- Select a.* , b.* From tablea a Join table b...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
6
by: Miles Keaton | last post by:
Is there a simple way to list fieldnames in a table, from PHP? When on the command-line, I just do \d tablename But how to get the fieldnames from PHP commands? ...
1
by: meltedown | last post by:
This is a function that returns the names a table field on a pgsql database $skips is simply the field names that will be left out of the result. This function acts differently on two different...
7
by: Trickynick1001 | last post by:
Hi, a newbie here. I don't have a real firm grasp on the idea of Javascript, as I'm used to programming in Qbasic and C. I'm not used to OOP. Anyway, I really don't have any idea what the...
2
by: john | last post by:
In the query builder I made an edit-query in which I have linked table A to table B on IDnr. For every record that matches I want 5 fields of table A to get the values of corresponding fields in...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...
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.