Trying to attempt the following in MS Access.
Convert data in one field to an 18 digit number starting with 01 in another field.
There are also some conditions that have to be met:
the first dash should become double zeros
the second dash should be removed
the third and fourth dash should be a single zero
the decimal must also be replaced with a zero
My query works fine until the decimal is the 15th character in the data.
Here is the query:
SELECT MasterVacant.ParcelIdNumber,
"01" + Mid([ParcelIdNumber],1,2) + "00" + Mid([ParcelIdNumber],4,2) + Mid([ParcelIdNumber],7,1)
+ IIf(Mid([ParcelIDNumber],11,1) = "", "0"+Mid([ParcelIDNumber],9,2), Mid([ParcelIDNumber],9,3))
+ IIf(Mid([ParcelIDNumber],14,1) = ".", "0"+Mid([ParcelIDNumber],12,2), Mid([ParcelIDNumber],12,3))
+ Mid([ParcelIDNumber],15,3) AS ParcelNumber
FROM MasterVacant;
Here is a start and finish example...
'12-06-1-00-50.000-RR' should become '011200061000050000'
'12-06-3-07-09.000-RR' should become '011200063007009000'
'13-35-1-01-129.000-RR' should become '011300035100112900'
However, instead of getting `0113000351001129000' I get '013000351001129.00'.
The issue is how do I remove the decimal when the decimal is the 15th character like in the third set of example?
I receive the data as a single column. Some of it is below....
1. 13-35-1-07-29.000-RR
2. 13-35-1-01-112.000-RR (Removing the decimal when the data is like this is the issue)
3. 13-35-4-01-01.000-RR
4. 13-35-4-02-04.000-RR
5. 13-35-1-13-17.000-RR
The output for the above data should be
1. 011300351007029000
2. 011300351001112000
3. 011300354001001000
4. 011300354002004000
5. 011300351013017000
Use a custom function:
Public Function Make18(ByVal Value As String) As String
Const Head As String = "01"
Const Tail As String = "000"
Const Lead As String = "00"
Dim Parts As Variant
Dim Part As Integer
Dim Result As String
Parts = Split(Split(Value, ".")(0), "-")
For Part = LBound(Parts) To UBound(Parts)
Select Case Part
Case 0
Parts(Part) = Head & Parts(Part)
Case 1
Parts(Part) = Lead & Parts(Part)
Case 3, 4
Parts(Part) = Right(Lead & Parts(Part), 3)
End Select
Next
Result = Join(Parts, "") & Tail
Make18 = Result
End Function
and your query becomes:
SELECT
MasterVacant.ParcelIdNumber,
Make18([ParcelIdNumber]) AS ParcelNumber
FROM
MasterVacant;
I am assuming you meant the opposite where:
12-06-1-00-50.000-RR should become 011200061000050000
12-06-3-07-09.000-RR should become 011200063007009000
13-35-1-01-129.000-RR should become 0113000351001129.00
I would recommend the REPLACE() in MSACCESS to strip the dashes out. Once you have the dashes out you can MID()
Unfortunately your attempted code does something different with the 3rd row because 3 zeros are being put in when there should be only two in my opinion.
Try in a text box:
=Replace("13-35-1-01-129.000-RR","-","")
will return 1335101129.000RR
and see if that assists you in making your code.
Maybe go one step further and put it in a function.
Related
I am from Bangladesh and in Bangladesh comma (,) is used as thousand separator. We use comma after 3 digit, 5 digit, 7 digit from right to left like 9,999, 99,999, 9,99,999,99,99,999,9,99,99,999 & 99,99,99,999. I was trying to accomplish this format by textbox format property. When I use #,##0 as format then it only format till 5 digit. When number is 6 digit or higher then it only shows one comma like 456,456 while expected is 4,56,456. I have tried to use #,##,##,##0 but it automatically goes to #,##0. So, how can I format the text box to get my desired result as below?
You can't. You'll have to run a custom format like this:
TextValue = Format(Fix(Value), Left("##\,##\,##\,##\,##\,##\,", -Int(-(Len(Abs(Fix(Value))) - 2) \ 2) * 4) & "##0")
Note, the negative values will be formatted correctly as well while decimals will be cut off.
If you have decimals, append these:
TextValue = Format(Fix(Value), Left("##\,##\,##\,##\,##\,##\,", -Int(-(Len(Abs(Fix(Value))) - 2) \ 2) * 4) & "##0") & LTrim(Str(Abs(CCur(Value)-Fix(Value))))
As used as ControlSource (read-only) in a form or a report:
=Format(Fix([Amount]),Left("##\,##\,##\,##\,##\,##\,",-Int(-(Len(Abs(Fix([Amount])))-2)\2)*4) & "##0")
Addendum:
To cover any situation with values:
Larger than 1, with or without decimals
Smaller than 1, a positive decimal value
Zero
Larger than -1, a negative decimal value
Smaller than -1, with or without decimals
an extended expression is needed:
TextValue = Format(Value, ";-") & _
Format(Abs(Fix(Value)), Left("##\,##\,##\,##\,##\,##\,", -Int(-(Len(CStr(Abs(Fix(Value)))) - 2) \ 2) * 4) & "##0") & _
IIf(Value - Fix(Value), LTrim(Str(Abs(Value - Fix(Value)))), "")
First part controls the sign
Second part controls the integer value
Third part controls decimals
This will output correctly for any value within the entire range of Currency.
I have two fields for current version number and previous version number in a form. What I want to do is when I enter the current version number (which is written like this 18.04.15), the previous version number on the next text box to automatically fill itself with 18.04.14.
I tried:
=[txtCurrentVersion]-1 in the control source, but obviously because I'm not decrementing by one, it didn't work.
Would appreciate some guidance, thanks :)
Below code splits the text based on dot and subracts one from last item.
Private Sub txtCurrentVersion_AfterUpdate()
If Nz(Me.txtCurrentVersion, "") <> "" Then
Me.txtPrevVersion.Value = Split(Me.txtCurrentVersion, ".")(0) & "." & Split(Me.txtCurrentVersion, ".")(1) & "." & Split(Me.txtCurrentVersion, ".")(2) - 1
End If
End Sub
I would suggest creating a function where you give the function 3 parameters, the 1st is the current version number string, the 2nd is the version number level (0 for major number, 1 for subversion number and 2 for minor version number) and the value to increase or decrease.
for example:
Function ModifyVersion(VersionNumber, NumberLevel, Number)
If VersionNumber <> "" AND NumberLevel >= 0 AND NumberLevel < 3 Then
dim VersionArray
VersionArray = Split(VersionNumber, ".")
Select Case NumberLevel
Case 0
VersionArray(0) = VersionArray(0) + Number
Case 1
VersionArray(1) = VersionArray(1) + Number
Case 2
VersionArray(2) = VersionArray(2) + Number
End Select
ModifyVersion = VersionArray(0) & "." & VersionArray(1) & "." & VersionArray(2)
End If
End Function
Then to decrease one from the minor version number use:
VersionNumber = [txtCurrentVersion]
Dim UpdateVersion
UpdateVersion = ModifyVersion(VersionNumber, 2, -1)
I am having an issue where a field is stored in our database as '##ABC' with no space between the number and letters. The number can be anything from 1-100 and the letters can be any combination, so no consistency of beginning letter or numeric length.
I am trying to find a way to insert a space between the number and letters.
For example, '1DRM' would transform to '1 DRM'
'35PLT' would transform to '35 PLT'
Does anyone know of a way to accomplish this?
You can use regular expressions like the one below (assuming your pattern is digits-characters)
= System.Text.RegularExpressions.Regex.Replace( Fields!txt.Value, "(\d)(\D)", "$1 $2")
Unfortunately, there's no built in function to do this.
Fortunately, Visual Studio lets you create functions to help with things like this.
You can add Visual BASIC custom code by going to the Report Properties and going to the Custom Code tab.
You would just need to write some code to go through some text input character by character. If it finds a number and a letter in the next character, add a space.
Here's what I wrote in a few minutes that seems to work:
Function SpaceNumberLetter(ByVal Text1 AS String) AS String
DIM F AS INTEGER
IF LEN(Text1) < 2 THEN GOTO EndFunction
F = 1
CheckCharacter:
IF ASC(MID(Text1, F, 1)) >= 48 AND ASC(MID(Text1, F, 1)) <=57 AND ASC(MID(Text1, F + 1, 1)) >= 65 AND ASC(MID(Text1, F + 1, 1)) <=90 THEN Text1 = LEFT(Text1, F) + " " + MID(Text1, F+1, LEN(Text1))
F = F + 1
IF F < LEN(Text1) THEN GOTO CheckCharacter
EndFunction:
SpaceNumberLetter = Text1
End Function
Then you call the function from your text box expression:
=CODE.SpaceNumberLetter("56EF78GH12AB34CD")
Result:
I used text to test but you'd use your field.
I have some VBA code in Access that scans the body of an email for a specific serial number. The serial number previously started with a 2 followed by three letters, and then 5 Numbers. I was using something like the posted code to search for the most common prefixes. Now they have changed the serial numbers to eliminate the 2 and with my current method this makes grabbing incorrect text much more likely. EMText is a string containing the email body.
The new format is ABC12345D1234 this can pretty much be any combination of letters or numbers, but the letters are always letters and the numbers are always numbers. Is there a quick way to search for something with this specfic length and number of letters and numbers or the specific format. I am having trouble coming up with something that is not overly complicated on my own and can't track down an example that matches what I am trying to do.
Function GetUnitNumber(ByVal EMText) As String
unit = ""
If InStr(1, EMText, "2ABC") Then
vItem = Split(EMText, "2ABC")
unit = "2ABC" & Left(vItem(1), 5)
ElseIf (InStr(1, EMText, "2CA")) Then
vItem = Split(EMText, "2CA")
unit = "2CA" & Left(vItem(1), 6)
ElseIf (InStr(1, EMText, "2DFS")) Then.......
This is a typical scenario for Regular Expressions.
Link to Microsoft VBScrpt Regular Expressions
And use this:
Function GetUnitNumber(ByVal EMText)
Dim regEx As New RegExp
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = True
'This matches the pattern: i.e. ABC12345D1234
.Pattern = "[A-Z]{3}[0-9]{5}[A-Z]{1}[0-9]{4}"
End With
If regEx.Test(EMText) Then
GetUnitNumber = regEx.Execute(EMText)(0)
End If
End Function
i'm using the following code to compare two recordsets:
For i = 1 To (recordsetA.Fields.Count - 1)
If recordsetA.Fields(i).Value <> recordsetB.Fields(i).Value Then
stringFieldList = stringFieldList & ", " & recordsetA.Fields(i).Name
End If
Next i
However in the stringFieldList there are a couple of fields which have the same values (like 1339.5). Why?
Since it sounds like your dealing with double datatypes, the proper way for the test would be to set a limit, then test the absolute difference. You will also need to think about handling a null value.
Const epsilon as double = 0.00001
If Abs(recordsetA.Fields(i).Value - recordsetB.Fields(i).Value) < epsilon Then
'do stuff here
End If