I don't see how that addresses her requirements!
First, you seem to have a typo in your code:
Trim(Right(strOrig,len(strOrig-1))
should probably be
Trim(Right(strOrig,len(strOrig)-1))
As well, Trim(Mid(strOrig,1) is going to return the entire string, not just
the alpha characters. Amy's implied that the prefix may not always be a
single letter, though, so you can't simply change that to
Trim(Mid(strOrig,1,1)
However, the bigger problem is that It seems to be limited to removing the
first leading zero only. Even if we correct the second error mentioned
above, if the number is, say, W0032, you'll return W032, not W32.
A more generic approach would be:
Public Function MyNewNumber(strOrig As String) As String
Dim intLoop As Integer
' Find the first numeric value in the string
For intLoop = 1 To Len(strOrig)
If IsNumeric(Mid$(strOrig, intLoop, 1)) Then
Exit For
End If
Next intLoop
' If the first character is numeric, intLoop will be 1
' If there are no numeric characters, intLoop will be
' one more than the length of the string.
If intLoop = 1 Then
MyNewNumber = CStr(Val(strOrig))
Else
If intLoop > Len(strOrig) Then
MyNewNumber = Trim$(strOrig)
Else
MyNewNumber = Trim$(Mid$(strOrig, 1, intLoop - 1)) & _
CStr(Val(Mid$(strOrig, intLoop)))
End If
End If
End Function
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Sprinks said:
Hi, Amy.
This is more generic to handle both conditions:
Public Function MyNewNumber(strOrig As String) As String
If IsNumeric(Left(strOrig,1)) Then
If Left(strOrig,1) = "0" Then
MyNewNumber = Trim(Right(strOrig,len(strOrig-1))
Else
MyNewNumber = strOrig
End If
Else
MyNewNumber = Trim(Mid(strOrig,1) & Right(strOrig, len(strOrig)-2))
End If
Sprinks
Amy Baggott said:
Unfortunately, they are not always the same length. The booth number is
usually one letter followed by 4 or 5 numbers, although there are some
exceptions that can be filtered out with a fairly straightforward If/Then
statement. I only want it to shorten the booth number if the first number is
a zero.
:
Hi, Amy.
The phrase "have this field automatically populate" is a red flag that a
field probably is not required at all. Since the value will be calculated
from another field, simply calculate it on-the-fly as needed.
You don't say whether the booths are always four characters long, have one
letter followed by three numbers, etc., but assuming that they are, a custom
function placed in a global module could return the value you seek:
Public Function MyNewNumber(strOrig As String) As String
MyNewNumber = Trim(Mid(strOrig,1,1)& Mid(strOrig,3,3))
End Function
Whenever you need the number, pass the value of your field to the function:
=MyNewNumber([BoothNumber])
Hope that helps.
Sprinks
:
I have a table of booth numbers and move-in dates that is populated by
importing data from an outside source. However, I need to be able to use the
booth number in two separate ways. Internally, the booth numbers are stored
using leading zeros for the low-numbered booths (e.g. W0432), but in the
lists I export to our web site, these zeros are dropped for aesthetic reasons
(e.g. W432). This table has to link internally with the booth numbers having
the leading zeros (which is how they are imported), but when I export it to
the web site for the online Service Kit, it has to link with the booths
without any leading zeros. So I have added a field to the table called
"ListingBooth" which contains the booth numbers without listing booths. What
I'd like to do is to have this field automatically populate, dropping leading
zeros where appropriate. Any ideas how I can do this when I import
the data?