[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Update query to insert leading zero in certain records

Posted on 2006-04-30
5
Medium Priority
?
435 Views
Last Modified: 2012-06-27
I need to update a table named Keynotes with a field named Keys.  The problem is I need to add or insert a leading zero in the each record that has the following condition - 12345.A1  - that is I need the record to read 12345.A01 because there will be records that have 12345.A10.  So I need each record to have length of 9 characters. The records 12345.A1 thru 12345.A9 only have 8 characters.  Also the first five numbers can be any combination and the letter will be A thur Z.  
I would like the syntax to use a Query in the Design mode with the "Update to" row.  However, an SQL statement will be fine.  Help - I need this for tomorrow.
0
Comment
Question by:Cad Coder
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 16573846
Try adding this VBA function to a module:

Public Function AddZero(str as string) as string
    if len(str) = 9  then
       AddZero = str
       exit Function
    else
       AddZero = Left(str,7) & "0" & right(str,1)
    end if
end function

And use this update query:

Update KeyNotes
Set NewKeys = AddZero(Keys)
Where nz(keys,"") <> ""

0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 16573888
"Help - I need this for tomorrow"
It's a work day there tomorrow?  It's a holiday here... (apparently :-S )

Anyway...
Just to supplement to Miriam's answer... (as opposed to replace it)

You could look to limit the number of rows upon which you perform the update.
This is a good idea mostly for performance.  However - you'd lose a lot of the point of it if you used a function in the WHERE clause to determine the records to update (functions can't be optimized).

If you ran something like this - then only relevant rows would be checked - indeed, Miriam's function could even be made a little simpler - not having to check - and just creating the new string :-)

Update KeyNotes
Set NewKeys = AddZero(Keys)
Where keys Like "*.??"
0
 
LVL 44

Accepted Solution

by:
GRayL earned 1000 total points
ID: 16573889
Update Keynotes Set Keys=Left(Keys,7) & "0" & Right(Keys,1) where Len(Keys)=8;
0
 

Author Comment

by:Cad Coder
ID: 16574002
To all,
Thanks for the quick responses.  I have added the VBA function to a module and then made a new query and pasted the following into the SQL view:

Update KeyNotes
Set NewKeys = AddZero(Keys)
Where nz(keys,"") <> ""

When I run the query it prompts for "Enter Parameter value: Keys "  What do I enter or am I not setting up the query properly?
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 1000 total points
ID: 16574113
Is "Keys" the name of the field that needs to be updated?  If not replace "Keys" with the name of the field.  Also, you'll need to add a column titled "NewKeys" to your table.  I set up the code like that so it would put the new values in a seperate column instead of overwriting your existing data.  If you want to overwrite the existing column with the corrected values, revise the code like this:

Update KeyNotes
Set Keys = AddZero(Keys)
Where nz(keys,"") <> ""

Make a backup first!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

834 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