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
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
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.
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
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: