Adding 1 to a text record

SeanDelere

Registered User.
Local time
Today, 10:54
Joined
Sep 7, 2004
Messages
51
I have setup a query that gives me the last product code I have used by using criteria, sort by descending and top1.

The resulting record is ABC0025

I want to be able to increment this by 1 in a calculated field to give ABC0026 so I know the next available product code to use.

The closest I have got is to use
Expr1: [ProductID] & 1 but this would give me ABC00261

Can anyone tell me what I need to do?
 
Will your product code always be 3 alpha's and 4 numerics?

kh
 
Yes

AB represents the supplier
C represents the type
1234 is the number of ABCs there has been (if you see what I mean)
 
Then how about a function:

Code:
f_myNewNumber(t_productID as a string) as string
    dim t_numericPart as integer
    dim t_numericPartLength as intger
    dim t_x as integer

    t_numericPart = val(right([productID],4) + 1
    t_numbericPartLength = len(cstr(val(right([productID],4) + 1)))

    For t_x = t_numbericPartLength to 4
       f_myNewNumber = 0 & f_myNewNumber
    next t_x
exit function

(untested, of course)
???
kh
 
I think your solution is beyond me :eek:

How do I enter a funtion like that into a query?

I click on build and can see "functions" in the list but that is about as far as I get.

Thanks for helping
 
I am sure someone will find a shorter way, but this works just fine.

Var1 is the field which contains ABC0029, or whatever.


Code:
Dim a, b, c, d, final

Var1.SetFocus

a = Var1.Text
b = Val(Right(a, 4))
b = b + 1
c = CStr(b)
d = Len(a)
a = Left(a, d - 4)

            For i = 1 To Len(c)
                    c = "0" + c
            Next

final = a + c
 
Hum... Seems I missed putting the alpha pc back on the front...

Oh well, if you can't get NoFrills suggestion to work, I guess you can repost and I'll try to walk you through creating a new function...

kh
 
Thanks Ken,

I am trying to split your solution into seperate fields so I get ABC in one field and 0025 in the other.

My thinking is that if I then add 1 to the second field the recombine them using & I should end up with the correct data.

NoFrills

It looks like your solution may be able to be run by clicking a button on a form to give me the next ProductID.
 
Do you want to try a simple function to see how they work then you'll get the jist of what I suggested?

kh
 
I now have it working thanks to the both of you with one problem.

The leading zeros are being stripped off.

I have 3 calculated fields :-

Letters: (Left([ProductID],3)) Returns the 3 letters

Digits: (Right([ProductID],4)) Returns the digits

NextCode: [Letters] & [Digits]+1 Brings the code back together.

The problem is that ABC0025 gets shortened to ABC26 instead of ABC0026 after the calculation

Any ideas?
 
Yes please Ken

As you have probably guessed I am still learning about Access in my 2 month long quest to build a stock control database for my shop/.
 
Cool. Ok - First, do a new form and put a text box on it where you can type in some text or a number. name it txtOne. Put a button on it and call it cmdOne. Name the form frmMain.

1. The first thing is to see how a simple function works and Access is full of pre built functions so I'll use one of them to show you how it works.

2. In the on click for the command button paste this:

Code:
msgbox forms!frmMain!txtOne

3. Save and open the form and in the text box type in something like "Hello!" and click the command button. I assume you see how this works.

4. Next modify the code in the command button click event to:

Code:
msgbox len(forms!frmMain!txtOne)

5. Save/Run it. This uses the built in len() function to return the length of the string you typed in the text box.

See how you send it the contents of the text box, it works a little and sends back the results.

Let me know when you get through this...

kh
 
Thanks for that Ken

No problems with that one and I think I understand how it works too!

I have found the different functions in the VB help file although it will be hard to discover which functions will be of use to me.
 
NoFrills said:
I am sure someone will find a shorter way, but this works just fine.

Var1 is the field which contains ABC0029, or whatever.


Code:
Dim a, b, c, d, final

Var1.SetFocus

a = Var1.Text
b = Val(Right(a, 4))
b = b + 1
c = CStr(b)
d = Len(a)
a = Left(a, d - 4)

            For i = 1 To Len(c)
                    c = "0" + c
            Next

final = a + c

The problem with this code is that it only works when there are 2 leading zeros.
If the code is ABC0008 the result is ABC09 instead of ABC0009
If the code is ABC0142 the result is ABC000143 instead of ABC0143

Can anyone suggest where this needs tweeking?
 
I appear to have finaly sorted it out by adding another variable f


a = Var1.Text
b = Val(Right(a, 4))
b = b + 1
c = CStr(b)
f = 4 - Len(b)
d = Len(a)
a = Left(a, d - 4)

For i = 1 To f
c = "0" + c
Next
 
Do you still want to see how to do a custom function?

kh
 
Yes Please Ken.

I have loads more work to do on the database and all the help I can get will be most welcome.

I have 4 major tasks left to do before it will be a in a working state.
 
Cool. I'll do you follow up to post 12 of this thread afterwhile...

kh
 
Sean,

1. Do a new module. It does not matter what you name it.
2. At the top of the module window, do Insert->Proceedure.
3. For the name of the procedure, call it 'f_helloWorld'.
4. For type select 'Function'.
5. Make sure Scope is set to public and click ok.
6. Modify the first line of the new function from this:

Code:
Public Function f_helloWorld()

To:

Code:
Public Function f_helloWorld(name As String) As String

7. Enter the following code so that the entire function looks like this:

Code:
Public Function f_helloWorld(name As String) As String

    f_helloWorld = "Hello " & name & "!"

End Function

8. Save and close the module. The function is done.
9. Open a new form name it frmMain. Create two text boxes name one testOne and the other textTwo. Then Create a command button, name it cmdOne and open the on click event and place the following code in it:

Code:
forms!frmMain!textTwo = f_helloWorld(forms!frmMain!textOne)

8. Save the form and open it. Put a name in textOne and click the command button. textTwo should be something like "Hello Thomas!"

See how when we passed the contents in forms!frmMain!textOne in the line in step 9:

Code:
f_helloWorld(forms!frmMain![COLOR=Red]textOne[/COLOR])

The f_helloWorld function took it here:

Code:
Public Function f_helloWorld([COLOR=Red]name[/COLOR] As String) As String

And used it:

Code:
f_helloWorld = "Hello " & [COLOR=Red]name[/COLOR] & "!"

Then assigned the entire 'Hello' string back to the function.

Code:
[COLOR=Red]f_helloWorld[/COLOR] = "Hello " & name & "!"

When the command button click event module got it back it put it in the textTwo text box...


???
kh
 
Thanks for that Ken,

So would I tend to use a custom function like this if it something I would want to use in more than one place?

Presumably with it having a public scope I could use this anywhere in the database project?

Sean
 

Users who are viewing this thread

Back
Top Bottom