overly long formulas in Update Queries (1 Viewer)

zoobeeda

Registered User.
Local time
Today, 09:58
Joined
Mar 30, 2014
Messages
28
Problem: dealing with Switch formulas that are too long.

step 1: The following Switch formula will work in an Update Query.

Switch([field]= "red","01",
[field]= "yellow","02",

[field]= "blue","03",
[field]= "orange","04",
[field]= "green","05",
[field]= "purple","06",
[field] Is Null,"x")

step 2: However, if a Switch formula such as the above were some length longer there would be a point where Access would return the error that "The text is too long to be edited."

I understand that there is a limit of about 14 conditions in a Query. You can get around this by using the concatinator "&" and repeating the Switch formula as follows:

Switch([field]= "red","01",
[field]= "yellow","02",

[field]= "blue","03",
[field]= "orange","04")&
Switch([field]= "green","05",
[field]= "purple","06",
[field] Is Null,"x")

But there seems to also be a limit to the total amount of text in the formula as well. With such a situation breaking up the formula with inclusions of

&Switch

will not help.

step 3: I tried splitting the formula into two columns in the Update Query, each headed by the same heading (the same Field). It doesn't seem that Access will allow having two columns in a Query sourcing the same Field. Is there any way to split a formula between two columns in a Query where it does work?

step 4: My solution to the above was to split the formula between two Update Queries. The problem with that is that each time one of the two Update Queries is run it will delete the values that the other Query entered.

I was thinking that if I entered script that said something like: "Ignore the remaining fields and do nothing to the value in the corresponding field" that that might keep the Query from wiping out the results of the other of the two Queries. I tried something to achieve this, but it didn't work.

Ideally, if I could have all this (a long Switch formula) done in one Query that would be best. Is that possible? If I can't get around splitting the formula up into two Queries, how do I get around the cancelling of the value change done by the other of the two Queries?

Thank you
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:58
Joined
Jan 20, 2009
Messages
12,851
Try putting the colour to number translations in a table and join it into the query.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:58
Joined
May 7, 2009
Messages
19,227
use a function to do this, put in a module:

public function fnColorNumber(vInput As Variant) As String
vInput = vInput & ""
Select Case vInput
Case Is =""
fnColorNumber="X"
Case Is = "red"
fnColorNumber = "01"
Case Is = "yellow"
fnColorNumber = "02"
Case Is = "Blue"
fnColorNumber = "03"
Case Is = "orange"
fnColorNumber = "04"
Case Is = "green"
fnColorNumber = "05"
Case Is = "purple"
fnColorNumber = "06"
Case Else
fnColorNumber = "99"
End Select
End function

now use this function in your query or form/reports textbox controlsource.
for query: Expr: fnColorNumber([field])

for textbox controlsource:

=fnColorNumber([field])
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:58
Joined
Jan 20, 2009
Messages
12,851
use a function to do this, put in a module:

The advantage of the function is that it definitely would not cause the recordset to be non-updateable which might happen depending on how the query is structured.

However the joining the table in the query has considerable advantages. Firstly, more colour codes can be added to the table without changing any code. Secondly its performance will be significantly better than the function.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:58
Joined
May 7, 2009
Messages
19,227
also, using any expression in the query (whether function, or just plain concatenation of fields) will render the resulting recordsets non-updeatable.
 

zoobeeda

Registered User.
Local time
Today, 09:58
Joined
Mar 30, 2014
Messages
28
Thank you for the replies!

Galaxiom and arnelgp offered two methods of approach. I'm a newbie and both solutions seemed a bit daunting. After thinking about it overnight I thought I might be able to do something with Galaxiom's suggestion:

> Try putting the colour to number translations in a table and join it into the query.

So I thought that a Switch formula such as follows might do the trick:

Expr1: Switch([Table_1]![data_1]=[Table_2]![colors],[Table_2]![numbers])

Where:

[Table_1]![data_1] is the the table and field where I want to check color values, and then entered translated values into a second field in the same Table.

[Table_2], the Table with translated pairs, has two relevant fields: [colors], [numbers] (which I indicated already in the Switch formula I posted earlier):

[color], [numbers]
red,01,
yellow,02,

blue,03,
orange,04,
green,05,
purple,06,
Is Null,x)

You might tell me if I'm on the right track.

I went to see if this would work in the actual Table I'm working on, in a Select Query first,. The results showed some promise, but something else bizarre was happening.

Now that I was entering two Tables into the Select Query and not just one:

[Table_1]: the Table where I was checking (and eventually translating) values
[Table_2]: the Table with the translation pairs


something odd was happening.

When I only had [Table_1] in the Select Query and view-ran it I gave 203 rows in the result. This is because [Table_1] has 203 rows and the Query did 203 rows of translations.

When I had both [Table_1] and [Table_2] in the Select Query and view-ran it I got 3045 rows in the result. This is exactly 15 times more than the 203 rows I got earlier with just the one [Table_1]. When I counted the number of rows in [Table_2], the table with the translation pairs, there were 15 rows. I concluded that the number of rows in [Table_2], 15 of them, somehow caused the number of rows in the results to be multiplied 15 times. The rows that successfully got translated though was less than 203, so it seems that the translations did not get multiplied 15 times, whereas the total number of rows did.

In the actual Table I am working on, the Table that I treated as a [Table_2] for the Select Query is also sourced as a Combo Box from a Form. I was wondering if the Combo Box link might have something to do with the bizarre behavior of multiplying the results by the number of rows in the translating table [Table_2]. I tried deleting the Combo Box link from that translating Table [Table_2] that I was working on. This didn't make a difference, as soon as I entered the second Table into the Query, running the Query would give me the multliplied-by-15 number of rows.

Now I'm going to go and start a Database from scratch and keep it free of Combo Box links and see if I will still get the bizarre multiplying of rows in a Select Query. If I don't get a similar bizarre multiplication of rows then I can deduce that the Combo Box Links are probably causing the bizarre row multiplication in a Select Query.

Any insights?
 

plog

Banishment Pending
Local time
Today, 11:58
Joined
May 11, 2011
Messages
11,635
Any insights?

1. Action queries (UPDATE, INSERT, MAKE TABLE) are usually band-aids used to cover up poorly structured tables or a misunderstanding of what a relational database is. Why are you running an UPDATE query?

2. You are getting more rows than you expected because of the JOIN in your query. Let's use a simple example:

Table1
T1ID
1
2
2
4


Table2
T2ID
1
1
2
2
2



Table1 has 4 records, Table2 has 6 records. If you were to make an INNER JOIN query from Table1 to Table2 ON the ID field in each, that query would return 8 rows:

T1ID, T2ID
1, 1
1, 1
2, 2
2, 2
2, 2
2, 2
2, 2
2, 2

The number of rows that get returned is the product of how many match between the 2 tables. ID=1 has 1x2 records, ID=2 has 2x3 records, ID=4 has 1x0 records. Therefore, you have duplicates in at least one of your tables in the field you are joining it to the other table.

3. It's a database, but your colors in a table. And make numbers numeric--'01' probably isn't a number in your database but short text.
 

zoobeeda

Registered User.
Local time
Today, 09:58
Joined
Mar 30, 2014
Messages
28
1. Action queries (UPDATE, INSERT, MAKE TABLE) are usually band-aids used to cover up poorly structured tables or a misunderstanding of what a relational database is. Why are you running an UPDATE query?

From what I can tell, the reason that I got the weird multiplying of rows was because I put two tables into a Query without there being ANY Join between the two tables. Correct me if I'm wrong.

I think I have some idea of what a relational database is—maybe not a tremendously good one. I was probably less clear of the importance of items such as Tables in a Query requiring Joins. If I'm not wrong again, multiple Tables in a Query require for there to be Joins between the Tables. The Joins connect the data in the Tables. No Join, and you get weird multiplication of rows. If you haven't experience weird multiplication of rows, put two Tables without a Join into a Query and run it. On the periphery of my comprehension it did seem to make sense that multiple tables in a Query without a join might cause problems.
_______

I stuck two Tables into one Query because Galaxiom suggested that I:

Try putting the colour to number translations in a table and join it into the Query.

What I did was my Newbie response to what Galaxiom suggested.

Since my last post I made a very simple database and I still got the multiplication of rows effect. I then realized the Combo Box linking didn't have anything to do with it.

Please go to my first post to understand what I'm trying to do. I have been trying to implement Switch functions and it's actually been working. I then ran into the problem of Switch formulas that are too long where Access wouldn't run an overly long Switch formula in a Query. I split the Switch formula into two Queries but ran into the problem of the Queries (as Update Queries) cancelling eachother's data changes out.

I got another idea in my Newbie head. Maybe I could "join" the two Tables I'm dealing with. Each of these tables has one column that contain the same spectrum of data entries, which are:

red, yellow, blue, green, orange, purple, (blank)

If Galaxiom's suggestion could work, maybe a bit more explanation of the procedure might help.

Try putting the colour to number translations in a table and join it into the Query.

If explaining my database project and what I'm doing in greater detail could help, I could write that up.
 

plog

Banishment Pending
Local time
Today, 11:58
Joined
May 11, 2011
Messages
11,635
Yes your "weird" results were because of the lack of a join. Without a JOIN, each record in Table1 matched each record in Table 2 yielding [NumberOfRecordsInTable1] x [NumberOfRecordsInTable2] total records in your query. In very fringe cases would you ever have a query without a JOIN between tables.

For the issue you initiall posted, you are trying to convert a color to a number. Let's call the table your query is based on YourTable and the field with the color string [field]. First you would move your color/number values to a table so:

Colors
ColorName, ColorNumber
red, 1
yellow, 2
blue, 3
...
...

Then instead of a Switch where you store all your data, you would JOIN Colors to YourTable. You would JOIN them via [field] to [ColorName]. Then instead of the Switch field, you would simply bring ColorNumber into your query.

I would suggest a 1 paragraph explanation in plain english (no database jargon) of what real world system you are modeling with your database. Just tell us what it is your organization does and what you hope to achieve with your database. Also, a screenshot of your relationships or just upload the database itself.
 

zoobeeda

Registered User.
Local time
Today, 09:58
Joined
Mar 30, 2014
Messages
28
personal circumstances:

I am working on a project on my own. This project is simply a database to store and manage data about the software I've accumulated. This database will become a model for further databases I will create to store and manage data, presumably just personal matters unless other needs arise.

the database:

Everything with the database is working except for one issue. I will try and describe those elements involved with just that issue.

Really only one Table [Table_1] is involved with the issue, but a second Table [Table_2] is indirectly involved and could be made to be involved directly. I will provide simplified versions of the Tables I am actually working on.

[Table_1]
![Value]

high
medium
low

If I do an ascending alphanumeric sort on the Value Field I would get:

[Table_1]
![Value]

high
low
medium

This is not what I want. I want the sort to follow the values of the values: high > medium > low. The importance of this is in my being able to observe other Fields in the Table as they are sorted according to the rating I have given them, or the importance I have assigned them.

I could add numbers to the values, placing them at the beginning of values.

[Table_1]
![Value]

01 high
02 medium
03 low

An ascending sort on these values will give me the order that I want. But it doesn't look so good to have these numbers, more so maybe for a Form.

Solution:

create a second column that would be autopopulated with appropriate numerical values determined by a Switch function.

[Table_1]
![Value], ![Number]

high, 01
medium, 02
low, 03

So in an Update Query, I've recently learned that a Switch formula written as follows will automatically populate the ![Number] Field with the numbers I need for a desired value-based sort.

Switch([Table_1]![Value]="high","01",
([Table_1]![Value]="medium","02",
([Table_1]![Value]="high","03")

So far so good. Things are working.

If the Switch formula goes beyond a certain length Access gives the error that there is too much text.

I tried splitting the long Switch formula into two columns in the Update Query. That is not allowed—a Query sourcing the same Field twice. I split the long Switch formula into two Queries. The problem with that is that each Query will wipe out the values in
![Number] that were entered by the other Query. I might be able to get around this problem if I new how to write an equation that would go approximately as follows:
Switch([Table_1]![Value]="high","01", ([Table_1]![Value]="medium","02", ([Table_1]![Value]="low","03", If False, Ignore) How would I correctly write the "If False, Ignore" part. This would have Access ignore any value in [Table_1]![Value] that is neither high, medium or low and not wipe out any number entries in the corresponding [Table_1]![Number] cell. I just would need to know how to write the "If False, Ignore" part. I've actually started a new thread in the Forum asking about this. If you (whoever you may be) might know the answere, feel free to provide it in this thread. the method using an additional Table A few people at the Forum have suggested that I create a second Table listing the value and translation pairs. That I then import them into the Query. Such a second Table might go as follows: [Table_2] ![Value], ![Number]
high, 01
medium, 02
low, 03

I will mention here that such a Table already exists in my Database. It exists to populate a Combo Box in a Form. The following approximates the two text boxes in the form.

Value: __________^

The "Value" represents the title box and the long underline and the caret approximate the second text box that is blank by default. The caret represents the drop down menu arrow that appears when the curser hovers (or selects) the blank text box. The drop down menu when opened will (obviously) display:

high
medium
low

my limited success with the method using an additional Table

After I realized that having two or more Tables in a Query that are not joined by a Join won't accomplish anything, I realized that the two Tables I was dealing with do have one column in common each that deal with the same array of values. So I joined them with a join.

[Table_1] is changed a bit to mimic possible variations

[Table_1]
![Value], ![Number]

low, (blank)
high, (blank)
(blank), (blank)
low, (blank)
medium, (blank)

[Table_2] is also used for a Form Combo Box (it doesn't need to be though)

[Table_2]
![Value], ![Number]

high, 01
medium, 02
low, 03

So in the relationships pane I linked [Table_1]![Value] with [Table_2]![Value] with a Join (represented by that little diagonal line that appears joining the Tables involved).

Then in an Update Query I entered the two above Tables. They were showing the Join line I think. I entered a Switch formula such as follows:

formula 1

Switch ([Table_1]![Value] = Switch([Table_2]![Value], Switch([Table_2]![Number])

When I ran the Update Query it actually did what I wanted. It populated the ![Number] field in [Table_1] with all the right numbers. The problem was that it ignored the blanks. For the blanks I created a second Update Query with the following formula:

formula 2

Switch ([Table_1]![Value] Is Null, "x")

Running this Query it populated [Table_1]![Number] with an x if [Table_1]![Value] were a blank, which is what I wanted, but it wiped out all the values entered by the first Query.

The solution to this was to run formula 2 first, and then run formula 1. Formula 1 just ignored the blanks and their corresponding ![Number] fields.

This did what I wanted.

new problems

Then I started adding new Joins and new Switch formulas, because I want to do this with more than one Field. I stuck the formulas all in one Query. When I set up the same type of Switch formula for other Fields I found that they didn't work, but the initial Field with a Switch Formula continued working. This problem didn't occur with the Switch formula dealing with blanks, just with the Switch formula dealing with actual strings. After a while even the Switch Query that had been working stopped working. I tried deleting all the Joins except for one between two Tables, but I couldn't get any Update Queries to work properly afterwards. Remember that I did manage to get an Update Query for strings (connected to a Table) to work initially.

It also seemed that when I tried deleting Joins, that Access acknowledged the deletion, but still seemed to keep the Joins in its memory somehow. They returned when I did some things in Access, even though I did not formally recreate those Joins.

This was all kind of exhausting.

conclusion

If I can get the "If False, Ignore" situation working in the

Switch([Table_1]![Value]="high","01",
([Table_1]![Value]="medium","02",
([Table_1]![Value]="low","03",
If False, Ignore)

. . . I might be inclined to just forget about the method of importing-a-Table-into-a-Query-method. On the other hand, the nice thing about the importing-a-Table-into-a-Query-method was that when I changed values in the translation Table (which also linked to a Combo Box) that the whole process updated as a consequence, and the new values were entered for the translations. I thought that this would possibly be good to implement for all the Combo Box Fields I have (there are maybe over 70) but then what I had got working properly, all crashed when I tried to add similar instances of it.

As it stands, I can get Update Queries with Switch formulas to work, but there is no method I can rely on when the Switch formula gets too long.
 

plog

Banishment Pending
Local time
Today, 11:58
Joined
May 11, 2011
Messages
11,635
I would suggest a 1 paragraph explanation in plain english (no database jargon) of what real world system you are modeling with your database. Just tell us what it is your organization does and what you hope to achieve with your database. Also, a screenshot of your relationships or just upload the database itself.

I don't know about others, but I'm not reading that last post of yours.
 

Users who are viewing this thread

Top Bottom