Solved

How to use vba to update a field based on the value of another field?

Posted on 2014-01-22
4
2,157 Views
Last Modified: 2014-01-22
Hi,

I have a field in a table named [Function] that contains what I believe a programmer would call a string.

It looks like the following with many variations:

--2-45--
1-34----

In a db, I added three additional fields named [ ShippingPort], [RailHub], [Airport].

How could I use vba to update each of these above fields based on the values within the [Function] field? i.e. If a 1 is found, then the corresponding [ShippingPort] field would be updated to Yes/No, if a 2 is found, then the corresponding [RailHub] field would be updated to Yes/No and so on.

Keys are as follows:

1 = Shipping Port
2 = Rail Hub
4 = Airport

Ideally, I would be able to run a procedure at any time that would update all the above three mentioned fields in my table, based on the contents of the string in the field [function].

I have enclosed a db file with the table as well if this helps.

Thanks
0
Comment
Question by:discogs
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 39801766
First off, you forgot to include your attachment.  Secondly, why do you want to update the table with those values when you could just create a query that can have calculated fields based on your table field.  Updating the table with data from the table would be redundant and is considered bad practice.

Create a query instead similar to this:
SELECT [YourField], IIF([YourField] Like "*1*", "Yes","No") AS ShippingPort,  IIF([YourField] Like "*2*", "Yes","No") AS RailHub, IIF([YourField] Like "*4*", "Yes","No") AS Airport FROM YourTable

Open in new window

Ron
0
 

Author Comment

by:discogs
ID: 39801797
Hi,

Thanks for your answer and sorry to not include the file. I am new here.

I have tried your proposed efforts with little joy and have enclosed the attachment with your suggested answer in it.

Ta
Database1.accdb
0
 
LVL 29

Accepted Solution

by:
IrogSinta earned 500 total points
ID: 39801851
All you needed to do was copy and paste the code I gave you into the SQL View of a query, then revise the field and table names to match yours.  Anyway here's your revised database.

Ron
Database1--1-.accdb
0
 

Author Closing Comment

by:discogs
ID: 39801886
Thanks. I had to tweak it a bit but got there in the end. Good work. Ta
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now