3

I am very new to VBA in Access 2010. I am trying to change a value in a Field Name of a Table. Here are the details:

Table Name: WeeklySaleT
Field Name: SitusPreDirection

Possible values in SitusPreDirection: "E" "N" "S" "W"

Would Like to Change every occurance of the following "E" = "East, "N" = "North", "S" = "South" and "W" = "West"

How would I do that in VBA?

2 Answers 2

3

It would be best to use a query if you are not familiar with VBA. First, copy your table. Then add the table to the query design window, select Update Query from the toolbar and add SitusPreDirection to the design grid by double-clicking or dragging. Set the Criteria to one of "E" "N" "S" "W" and Update To to the matching value, "east", for example.

If you switch to SQL View, you will see something like:

UPDATE WeeklySaleT SET SitusPreDirection = "East" 
WHERE SitusPreDirection = "E"

You can use this in VBA like so:

Dim db As Database
Set db = CurrentDB

sSQL = "UPDATE WeeklySaleT SET " _ 
      & "SitusPreDirection = ""East"" WHERE SitusPreDirection = ""E"""

db.Execute sSQL, dbFailOnError
Sign up to request clarification or add additional context in comments.

Comments

2

With a lookup table for the directions, you could use a single UPDATE query.

SitusPreDirection_lookup:

SitusPreDirection long_name
E                 East
N                 North
S                 South
W                 West

Then create a new query, switch to SQL View and paste in this UPDATE statement. You can switch between SQL, Design, and Datasheet views to experiment with changes to the query design and view the results of those changes.

UPDATE WeeklySaleT
SET SitusPreDirection = DLookup("long_name",
    "SitusPreDirection_lookup", 
    "SitusPreDirection = '" & SitusPreDirection & "'")

If you don't want to create a lookup table, you could build a query using the Switch Function. Then without the separate table, you would essentially embed the lookup pairs within your query. But I think a table is easier to maintain. Consider what would happen if you need to extend the directions to include NE, SE, SW, and NW. With a table, you would only need to add a row for each and the same query would continue to work seamlessly. But when embedding the lookup data in the query, you would need to revise the query's SQL.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.