Using VBA To Change The Name Of A Field In A Union Query

Feb 16, 2007

I have a table called LLD Import Table with the following fields in it.

FileNumber, LandDescription, Qtr1, Sec1, Qtr2, Sec2, Qtr3, Sec3, Qtr4, Sec4, Qtr5, Sec5

Data Example:
123456, T43 R2 W5M, S, 6
123457, T43 R1 W5M, SE, 18, SW, 17
123456, T43 R1 W5M, E, 19, E, 30, SW, 29, E, 31, NE, 18

What I have done so far is to create a Union query to create a new record with the file and land description repeating for each row where there is quarter and section data with the following code:

SELECT [LLD Import Table].[FileNumber], [LLD Import Table].[LandDescription], [LLD Import Table].Sec1 AS Section, [LLD Import Table].Qtr1 AS Quarter
FROM [LLD Import Table]
WHERE ((([LLD Import Table].Sec1) Is Not Null));
UNION
SELECT [LLD Import Table].[FileNumber], [LLD Import Table].[LandDescription], [LLD Import Table].Sec2 AS Section, [LLD Import Table].Qtr2 AS Quarter
FROM [LLD Import Table]
WHERE ((([LLD Import Table].Sec2) Is Not Null));
UNION
SELECT [LLD Import Table].[FileNumber], [LLD Import Table].[LandDescription], [LLD Import Table].Sec3 AS Section, [LLD Import Table].Qtr3 AS Quarter
FROM [LLD Import Table]
WHERE ((([LLD Import Table].Sec3) Is Not Null));
UNION
SELECT [LLD Import Table].[FileNumber], [LLD Import Table].[LandDescription], [LLD Import Table].Sec4 AS Section, [LLD Import Table].Qtr4 AS Quarter
FROM [LLD Import Table]
WHERE ((([LLD Import Table].Sec4) Is Not Null));
UNION
SELECT [LLD Import Table].[FileNumber], [LLD Import Table].[LandDescription], [LLD Import Table].Sec5 AS Section, [LLD Import Table].Qtr5 AS Quarter
FROM [LLD Import Table]
WHERE ((([LLD Import Table].Sec5) Is Not Null));

To give this result:

FileNumber, LandDescription, Quarter, Section
123456, T 43 R 1 W5M, NE, 18
123456, T 43 R 1 W5M, E, 19
123456, T 43 R 1 W5M, SW, 29
123456, T 43 R 1 W5M, E, 30
123456, T 43 R 1 W5M, E, 31
123456, T 43 R 2 W5M, S, 6
123457, T 43 R 1 W5M, SW, 17
123457, T 43 R 1 W5M, SE, 18

However the number of Quarters and Sections under a file changes, so next time I my table could have fields up to Qtr20 and Sec20 What I’d like to do is to create a function that will automatically change the # behind the field names (to replace the * in the example below) so that I don’t have to rewrite the Union Query each time. I’ve seen some code examples that can change the value, but don’t quite understand them enough to write one.

SELECT [LLD Import Table].[FileNumber], [LLD Import Table].[LandDescription], [LLD Import Table].Sec* AS Section, [LLD Import Table].Qtr* AS Quarter
FROM [LLD Import Table]
WHERE ((([LLD Import Table].Sec*) Is Not Null));

Your help would be GREATLY appreciated!

View Replies


ADVERTISEMENT

Queries :: UNION / UNION ALL Query Crashed Access

Oct 24, 2013

I'm having a problem with a UNION / UNION ALL query.It seems there is a application crash fault when running the query that MS are aware of and have issued a hot fix. Unfortunately it will take my IT dept some time to check and install the hot fix If they agree to do it at all.

Problem signature:
Problem Event Name: APPCRASH
Application Name: MSACCESS.EXE
Application Version: 12.0.6606.1000

[code]...

View 1 Replies View Related

Hyperlink Field In Union Query

Mar 8, 2006

Hi,
I have a query that I build using VBA based on some user input. One of the fields I pull out is a hyperlink to particular files on our local network, so the user can click the link and open the relevant file. My problem is that if the SELECT statement contains one (or more) UNIONs the hyperlinks no longer work. The query returns all the fields but the hyperlink field is just text of the form "display_text#link_address#".
I am using MS Access 2000. The UNION statements are required so I can search for multiple keywords in various fields within the table.

An example of the SQL query I generate is:
SELECT DocRef, DocTitle, DocAbstract, DocLink, DocAuthor, DocDate FROM TechDocs
WHERE DocType IN ('TechRep', 'Misc')
AND DocAbstract LIKE ('*drug*')
UNION
SELECT DocRef, DocTitle, DocAbstract, DocLink, DocAuthor, DocDate FROM TechDocs
WHERE DocType IN ('TechRep', 'Misc')
AND DocAbstract LIKE ('*release*');

If I do the query a different way (when I am searching for phrases, not keywords) the hyperlinks work fine. As you can imagine this is very frustrating! I have read that Access 2000 has some problems with UNIONs where it has to order the individual SELECT results before the union or something, but I can't work it out. :confused:

Any help is much appreciated.

View 1 Replies View Related

Setting A Format In A Field In A Union Query

Jun 21, 2007

Hi everyone,

Please would someone be able to help me?
I have created a union query however, one of the columns, has not picked up the same format as it has in the tables. As in the tables it has this format
'00000'.

Please woud you be able to advise me how I can change the format on one of the 'columns' in my union query. As one column is 'numbers' and the other is 'text'. I need to change the number column so the format is '00000'.

Thank you in advance for your help.

Nats

View 5 Replies View Related

Union Query Truncates Memo Field????

Jan 4, 2005

I created a Union Query for several linked Excel tables. Certain fields in the Excel table exceed 256 characters and Access (rightfully so) assigns these fields as "Memo". I have create a report based upon the Union Query; however, it will truncate the "Memo" fields to 255 (or 256 characters).

As a side note, if I create a report based upon a simple query using only one of the linked tables, it does not truncate the field.

Any suggestions on what maybe causing this truncation issue?

View 2 Replies View Related

Queries :: Hide Field In Union Query?

Dec 11, 2013

Is there a way to hide a field in a union query? I need to keep the field in the SQL statement because I need to order by it. The field is "Rank," but I don't want it showing up.

View 3 Replies View Related

Modules & VBA :: Union Query - Automate Date Field

Mar 18, 2014

The statement below is a snippet from a union query and is repeated 6 times within the SQL Statement for various reasons, I have to manually change this every month, again is there i tiny bit of code I can insert to replace the following so that it automatically runs the previous months data.

WHERE ArrivalDateTime >= 'February 1 2014'

View 5 Replies View Related

Union Query - Create Additional Field / Clear Records

Jul 20, 2006

The code pasted below creates a union query for a set of tables (J000171, J000174, J000178 etc) and stores the results of the query in a table called temp.

The first piece of code queries the ‘status’ field of a table rjobs for those records with a ‘status’ field of “Live”. Another field within this rjobs table, ‘JobID’, happens to be the name of a table where additional information relating to that job record is held eg. J000178 All of the tables selected in the query on rjobs are then included in the union query.

The second piece of code stores this information in a table called temp

I would like to be able to do 2 things with this;

1.add an additional field to the union query which holds the JobID field value from rjobs (or alternatively the table name from which the data originates eg J000178 etc as that is the same as the JobID file din rjobs)

2.create an option to clear the info in the temp table. Currently additional info is appended, so whenever the query is refreshed new data is simply added to old data. I would like to be able to clear that data where possible.

The union query is run from the on click of a command button on a simple form. Perhaps an additional button could be used to clear the records from the table temp.

Any ideas greatly appreciated.

Here is the existing code …

Option Compare Database
Option Explicit


Private Sub Command0_Click()
Dim db As Database
Dim rsRjobs As Recordset
Dim rsRapps As Recordset
Dim LengthofUnionSQL As Long
Dim sql As String
Dim UnionSQL As String
Set db = CurrentDb
Set rsRjobs = db.OpenRecordset("Select * from rjobs where Status = 'Live'", dbOpenSnapshot)
Do While Not rsRjobs.EOF
UnionSQL = UnionSQL & "Select ObjectID, SearchNo, DateSearched, Consultant, from " & rsRjobs!jobID & " Union "
rsRjobs.MoveNext
Loop
'following two lines are to remove the trailing word Union from the string unionsql
LengthofUnionSQL = Len(UnionSQL)
UnionSQL = Mid(UnionSQL, 1, LengthofUnionSQL - 7)
' Now variable Unionsql will hold the value something like
' Select ObjectID, SearchNo, DateSearched, Consultant from J000145
' Union Select ObjectID, SearchNo, DateSearched, Consultant from J000146
' Union Select ObjectID, SearchNo, DateSearched, Consultant from J000147
MsgBox UnionSQL


Set db = CurrentDb
Dim rsUnionquery As Recordset
Dim rstemp As Recordset
Set rstemp = db.OpenRecordset("temp", dbOpenDynaset, dbSeeChanges)
Set rsUnionquery = db.OpenRecordset(UnionSQL)
Do While Not rsUnionquery.EOF
rstemp.AddNew
rstemp!ObjectID = rsUnionquery!ObjectID
rstemp!SearchNo = rsUnionquery!SearchNo
rstemp!DateSearched = rsUnionquery!DateSearched
rstemp!Consultant = rsUnionquery!Consultant
rstemp!jobID = rsUnionquery!jobID

rstemp.Update
rsUnionquery.MoveNext
Loop

End Sub

View 2 Replies View Related

Queries :: Multi-Field Union Query (Joining Questions And Pictures Into One Report)

Apr 5, 2013

I have two tables containing (let's say for simplicity) questions and attachments (pictures). I am trying to perform a union query to join all the questions and pictures into one report, but it won't let me union the attachment because 'the multi-valued field 'TableA.Pictures' cannot be used in a union query'.

I have searched and searched for a solution (and got kind of close) but i still can't get it to work. The best I can do is union everything like below, which gives all the questions as desired, but says #Error in the pictures column:

SELECT TableA.*
FROM TableA
Union
SELECT TableB.*
FROM TableB;

(Note tables A and B have the same structure, several yes/no and open text questions as well as one attachment field. )

View 8 Replies View Related

Field Change Query

Apr 16, 2008

All,

Is there a way to create a query to show when text in a specific field was changed?

View 2 Replies View Related

Change Field Facts In Query?

Oct 20, 2005

Lets say I have a table with the following columns:

Chainname
Storenumber
Storename
Revenue in value (sold goods in $)
Revenue in volume (sold goods in pieces or SKUs)
Month

I create an Append query where I use these fields from my table:

Chainname
Storenumber
Storename
Revenue in value (sold goods in $)
Month

What happends afterwards doesn't matter, but I what if I would like to change a field in my Append query to this:

Chainname
Storenumber
Storename
Revenue in volume (sold goods in pieces/SKUs)
Month

I can do that manually by opening the Append query and then change it in the field...But can I change it without actually opening the query. Lets say using a Form were I choose if I wanna use:

Revenue in value (sold goods in $)

or

Revenue in volume (sold goods in pieces/SKUs)

??

View 3 Replies View Related

Change Field In A Query By Form

Aug 5, 2006

Is the a way of changing the fields selected in a query by a form.

I have a form showing how many people need to do each module. Each textbox is populuted by a count from the table.

The idea is simply to avoid having 26 queries based on each module. Is there a way to have the field name changed via the form.

I have used the on dbl click event to pass the field name to a hidden field on the record. I just need to pass the data in the hidden field to the select query.

View 2 Replies View Related

Form Change A Query Field?

Jul 11, 2012

Can a form change the field on a query?

Right now one of the tables in my query has multiple years across the top (as the fields), 2010, 2011, 2012, 2013. Each year contains different rates that get applied to labor hours.

I would like to know if there is a way to have a form change the field in my query. I have attached a word document showing print screens of what I mean.

View 1 Replies View Related

Queries :: Change Field Format Within A Query

Sep 23, 2013

I'm having a format problem. I have a query (Q1) that, among other things format a date field as YYYYMM (Field1).

I have a second query (Q2) whose data source is Q1.

In Q2 I need to link Q1 to a table but Field1 is being reformatted as text (confirmed by running a make table query).

I want Field1 to be Number format to eliminate a mismatch error.

I know how to change the format of a field in a table using VBA but I cant seem to find a way of changing the format within a query.

View 2 Replies View Related

Queries :: Change Field Name In Pass Through Query

Feb 10, 2014

In a table of a SQL-Server 2008 I have a fieldname "Alter", the German word for age.

When sending a PT-qry to the server I get an error "Incorrect syntax near the keyword 'Alter'"

Even when I try to set "Select tblmytable.Alter as Age" I get the same error.

To change the fieldname is almost impossible, as there is to much code to change.

View 3 Replies View Related

Modules & VBA :: Change Value Of A Field In Append Query Using SQL?

Jan 25, 2015

I have a main form which does nothing except filter subforms through a cbo.

On the main form are two subforms. One shows top line data, the other shows a breakdown of the top line data, and are linked by an unbound textbox (it's how it works, and does so perfectly)

I'm running an append query to duplicate a record in the second form using vba/sql BUT... need to have one of the fields values changed based on a field on the parent table.

If Forms!frmmain!frmPost.Form.RecordsetClone.RecordCo unt > 0 Then
strSql = "INSERT INTO [tblposts] ( TopLineID, AccountID, TransDate, Cat, SubCat, Debit, Credit ) " & _
"SELECT " & lngID & " As NewID, AccountID, TransDate, Cat, SubCat, Credit, Debit " & _
"FROM [tblposts] WHERE TopLineID = " & Me.TopLineID & ";"
DBEngine(0)(0).Execute strSql, dbFailOnError
Else
MsgBox "Main record duplicated, but there were no related records."
End If

Credit and Debits are reversed as I want one to zero out the other.

In regards to the AccountID, I've tried allsorts and it's just not working... to the point I'm almost giving up and finding an alternative.

Whats the "Correct" syntax to attach a "WHERE" statement to the highlighted [AccountID]'s (which needs to be the value on AccountID on the Parent table)

View 1 Replies View Related

Change Text To Date/Time Field In A Query

Feb 7, 2008

I have created a table called - "Test"
The properties of the table is listed below

Table Name: Test
Field Name: ADMDAT2 (Text)
DISDAT2 (Text)
Operation Date (Date/Time)

I have written a query to populate a field where the Operation Date is between the ADMDAT2 and DISDAT2

Expr1: IIf([ADMDAT2] Is Null,"",IIf([Operation Date]>=[ADMDAT2] And [Operation Date]<=[DISDAT2],"Match"))

Unfornately it returns and ERROR message... I believe this may be because, the data type of the field, matching a Text with a Date/Time, I have rerun the query using a sample table where all the fields are Date/Time, and it work perfectly.

What i need help with how do i convert a text field into a Date/Time in a query?

So i can place that in the query before i populate the Test table. therefore it all should be date/time

thanks in advance

View 1 Replies View Related

Modules & VBA :: Dynamic Query Change Field Selections

Jun 25, 2014

I am trying to build a function that will create a dynamic query for a chart on a Subreport.I am not exactly sure I am going about this the right way, but I need the user to be able to change selected fields for use in the query. I have a form with 3 combobox controls for selecting options to change the SQL statement. So far my code only deals with one of these comboboxes for simplicity. There is a button to call my function. Currently, the function is setting hidden text box values based on the combo controls, but I'm not sure if this is redundant.

I am using this as my guide for building the sql, but I am having trouble picking up the values in my text boxes for use in the SQL. [URL] .....

Code:
Option Compare Database
Option Explicit

[code]...

how do I get a value from an unbound textbox on an unbound form into a string to use as sql? The value in the textbox is a number.

View 3 Replies View Related

Queries :: Change Query Field Based On A Form

Oct 29, 2013

I need a query's field to change to what is selected in a combo box.

Example,

I select product 1 in the combo box on a continuous form, after the update there is a requery. The form now displays the data from product 1.

I have tried putting [forms]![form1]![combo1] into the field box of the query. But this only makes the form display "product 1" in every row, it does not take any data from the table.

View 3 Replies View Related

Queries :: Create View (Query) With Only Change In Value For First Field

Jul 16, 2014

Need sorted Query to be used in a Listbox. The attachment shows a made-up view of the objective.

The first column shows the change in value, then blanks for the first column's next rows, until there is a change of value again.

I do something like this in Excel where the first change in value is bolded. This is to make a list box more readable.

View 13 Replies View Related

Update Query To Change Display Value Part Of Hyperlink Field

Nov 11, 2007

Given a table field that is a hyperlink type.
I need an Update Query to set all records of that table so that the Displayed Value part of the hyperlink field (not the Address part) is set to a particular value.
Any ideas how?
Thanks.

View 1 Replies View Related

Queries :: Field In Table Doesn't Change When Edited From Query

Mar 29, 2013

Users are viewing a record on a form that gets it's data from a query. I want to make it so that if they edit that record from that form, a last updated field will timestamp the date/time that the record was edited. I added a lastupdated to the source table which of course adds it to the query and so it's on the form.

But whenever i access the form and change something, the lastupdated only shows the time the record was created (which is from a different form based directly on the table). Whenever I edit any other field data in that query based form it changes in the table. Why not the Lastupdated?

View 2 Replies View Related

Change Field Background Color Based On Query Results

Jun 24, 2014

I would like to change the background color of a field that is the result of a Unique Values query. I am trying to get a list of invoices where all the line items are approved. I can't seem to get it to work the way I want because if even one invoice line item is approved it will show up as approved.

Is there a way to change the background color of the invoice field to red if ANY of the Approved line items are = False

View 7 Replies View Related

Create A Query With Derived Field Based On Each Change In Recordset

May 5, 2013

I need to come up with a way to derive field X (see below) in a query.

For each change in field A, Set X=1
For each change in field B, X=X+1

The below table shows what the query results should look like.

A
X
B

[code]....

View 2 Replies View Related

Queries :: Union (all) Does Not Like Multi-Value Field?

May 14, 2013

I have got a db and that has 10+ ref tables, and i need to show everything from those tables in one, and these tables includes Attachment field. I have tried union but did not work.

How can i achieve this?

View 4 Replies View Related

Queries :: Change Time Portion Of Datetime Field In Update Query?

Sep 12, 2013

I have some incorrect time entries in a column that I need to fix with an update query.

So, 04/11/2013 08:00:00 needs to be changed to 04/11/2013 09:00:00

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved