I want to append an alphabet incrementally like for variable say
I pass JohnBox then it should be JohnBox_a then the next time it would be:
JohnBox_b
.
.
JohnBox_z
JohnBox_aa
.
.
JohnBox_zz
Can someone please help regarding solving this issue? This is what I have tried so far but Case 2 is where I am having problems:
Public Function fCalcNextID(strID As String) As Variant
Dim strName As String
'Extract Numeric Component
strName = Left(strID, InStr(strID, "_"))
If Len(Nz(strName, "")) = 0 Then
strName = strID
Else
strName = strName
End If
Select Case Len(Right(strID, (Len(strID) - (InStr(strID, "_")))))
Case 1 'single alpha (a)
If Right$(strID, 1) = "z" Then
fCalcNextID = strName & "aa"
Else
fCalcNextID = strName & Chr$(Asc(Right$(strID, 1)) + 1)
End If
Case 2 'double alpha (bd)
If Right$(strID, 1) = "z" Then
If Mid$(strID, 4, 1) = "z" Then
fCalcNextID = CStr(strName + 1) & "a"
Else
fCalcNextID = CStr(strName) & Chr$(Asc(Mid$(strID, 4)) + 1) & "a"
End If
Else '101bd, 102tx, etc.
'Increment last character, 101bd ==> 101be
fCalcNextID = Left$(strName, 4) & Chr$(Asc(Right$(strID, 1)) + 1)
End If
Case Else
fCalcNextID = strName & "_a"
End Select
End Function
The solution to your problem can be found in this LINK already. Credits to UtterAccess Wiki
The link presents 2 functions: Base10ToBaseLetter and BaseLetterToBase10. The functions are shown below just in case the link changes or become unavailable.
Public Function Base10ToBaseLetter(ByVal lngNumber As Long) As String
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
' ================================================================================
' Concept:
' Base10: Decimal 123 => (1 * 10 ^ 2) + (2 * 10 ^ 1) + (3 * 10 ^ 0)
' Base26: Decimal 123 => ( 4 * 26 ^ 1) + (19 * 26 ^ 0)
' Representing 4 and 19 with letters: "DS"
' MSD = Most Significant Digit
' LSD = Least Significant Digit
' ================================================================================
' Returns ZLS for input values less than 1
' Error handling not critical. Input limited to Long so should not normally fail.
' ================================================================================
Dim intBase26() As Integer 'Array of Base26 digits LSD (Index = 0) to MSD
Dim intMSD As Integer 'Most Significant Digit Index
Dim n As Integer 'Counter
If lngNumber > 0 Then
' Calculate MSD position (Integer part of Log to Base26 of lngNumber)
' Log of X to Base Y = Log(X) / Log(Y) for any Base used in calculation.
' (VBA Log function uses the Natural Number as the Base)
intMSD = Int(Log(lngNumber) / Log(26))
ReDim intBase26(0 To intMSD)
For n = intMSD To 0 Step -1
' Calculate value of nth digit in Base26
intBase26(n) = Int(lngNumber / 26 ^ n)
' Reduce lngNumber by value of nth digit
lngNumber = lngNumber - ((26 ^ n) * intBase26(n))
Next
' Base Letter doesn't have a zero equivalent.
' Rescale 0 to 26 (digital representation of "Z")
' and "borrow" by decrementing next higher MSD.
' Digit can be -1 from previous borrow onto an already zero digit
' Rescale to 25 (digital representation of "Y")
' Looping from LSD toward MSD
' MSD not processed because it cannot be zero and
' avoids potential out of range intBase26(n + 1)
For n = 0 To intMSD - 1
If intBase26(n) < 1 Then
intBase26(n) = 26 + intBase26(n) ' Rescale value
intBase26(n + 1) = intBase26(n + 1) - 1 ' Decrement next higher MSD
End If
Next
' Ignore MSD if reduced to zero by "borrow"
If intBase26(intMSD) = 0 Then intMSD = intMSD - 1
' Convert Base26 array to string
For n = intMSD To 0 Step -1
Base10ToBaseLetter = Base10ToBaseLetter & Chr((intBase26(n) + 64))
Next
End If
End Function
Public Function BaseLetterToBase10(ByVal strInput As String) As Long
' Upper or lower case characters accepted as input
' ZLS returns 0
' Negative return value indicates error:
' Unaceptable character or Overflow (string value exceeds "FXSHRXW")
' Digit indicates character position where error encountered
' MSD = Most Significant Digit
Dim intMSD As Integer 'MSD Position
Dim intChar As Integer 'Character Position in String
Dim intValue As Integer 'Value from single character
Dim n As Integer 'Counter
On Error GoTo ErrorHandler
' Convert String to UpperCase
strInput = UCase(strInput)
' Calculate Base26 magnitude of MSD
intMSD = Len(strInput) - 1
For n = intMSD To 0 Step -1
intChar = intMSD - n + 1
intValue = Asc(Mid(strInput, intChar, 1)) - 64
' Test for character A to Z
If intValue < 0 Or intValue > 26 Then
BaseLetterToBase10 = -intChar
Exit For
Else
' Add Base26 value to output
BaseLetterToBase10 = BaseLetterToBase10 + intValue * 26 ^ n
End If
Next
Exit Function
ErrorHandler:
BaseLetterToBase10 = -intChar: Exit Function
End Function
Now to apply it to your needs, you simple call those functions:
Public Function fCalcNextID(strID As String) As String
Dim CurIdx As String, n As Integer, x As Long
On Error Resume Next
CurIdx = UCase(Split(strID, "_")(1))
On Error GoTo 0
If CurIdx <> "" Then
x = BaseLetterToBase10(CurIdx) + 1
fCalcNextID = Split(strID, "_")(0) & "_" & LCase(Base10ToBaseLetter(x))
Else
fCalcNextID = strID & "_a"
End If
End Function
This is not me. It is them. What I did is just ask Google to find it for me.
Nonetheless hope this helps and is actually what you need.
Important: Don't remove the comments. That is the only request of the author.
What you have is essentially a base26 count. You can implement that with a modulus function instead of your current code. You will have to create the VBA code yourself, just getting you the algoritm:
For example:
Create an array with a-z
Input a value:
cde
Convert to numeric: 3*26*26+4*26+5
Add 1: 3*26*26+4*26+5+1
input=3*26*26+4*26+6
LOOP until input equals 0:
Mod(input,26) returns remnant (first loop:6, 2nd loop: 4, 3rd loop: 3) => look up in array => f (first loop) (2nd loop d, third loop c).
returnval=lookup value+returnval;
input=Divide (input - mod output (input))/26
END LOOP
Related
I’m using this code and I’m trying to get it so that the row number resets whenever a certain field changes in a qry. I have the below to functions and then I use it in the qry. I export the query to use elsewhere.
Any Ideas and or samples?
For example, if field A is orange, orange, orange, banana, apple, apple, grapefruit.
Then I’m looking to have this in field B: 001, 002, 003, 001, 001, 002, 001
This is what I need | This is what I get
Field A Field B | Field A Field B
orange 001 | orange 1
orange 002 | orange 2
orange 003 | orange 3
banana 001 | banana 4
apple 001 | apple 5
apple 002 | apple 6
grapefruit 001 | grapefruit 7
Option Compare Database
Private lngRowNumber As Long
Public Function RowNumber(UniqueKeyVariant As Variant) As Long
lngRowNumber = lngRowNumber + 1
RowNumber = lngRowNumber
End Function
Public Function ResetRowNumber() As Boolean
lngRowNumber = 0
ResetRowNumber = True
End Function
Query
SELECT
TBL_Test.RowID,
TBL_Test.Cust_Number,
TBL_Test.Loan_Number,
RowNumber(TBL_Test.RowID) AS RowNum
FROM TBL_Test
WHERE (((ResetRowNumber())<>False))
ORDER BY TBL_Test.Cust_Number, TBL_Test.Loan_Number;
First, you miss is a unique ID, then an expanded function that takes a group key.
So, add an AutoNumber field to table as the first step.
Next, at my project VBA.RowNumbers you can find my function RowNumber which has the option for a group key:
' Builds consecutive row numbers in a select, append, or create query
' with the option of a initial automatic reset.
' Optionally, a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query having an ID with an index):
' SELECT RowNumber(CStr([ID])) AS RowID, *
' FROM SomeTable
' WHERE (RowNumber(CStr([ID])) <> RowNumber("","",True));
'
' Usage (typical select query having an ID without an index):
' SELECT RowNumber(CStr([ID])) AS RowID, *
' FROM SomeTable
' WHERE (RowNumber("","",True)=0);
'
' Usage (with group key):
' SELECT RowNumber(CStr([ID]), CStr[GroupID])) AS RowID, *
' FROM SomeTable
' WHERE (RowNumber(CStr([ID])) <> RowNumber("","",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
' Call RowNumber(vbNullString, True)
' 2. Run query:
' INSERT INTO TempTable ( [RowID] )
' SELECT RowNumber(CStr([ID])) AS RowID, *
' FROM SomeTable;
'
' Usage (typical append query, automatic reset):
' INSERT INTO TempTable ( [RowID] )
' SELECT RowNumber(CStr([ID])) AS RowID, *
' FROM SomeTable
' WHERE (RowNumber("","",True)=0);
'
' 2020-05-29. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RowNumber( _
ByVal Key As String, _
Optional ByVal GroupKey As String, _
Optional ByVal Reset As Boolean) _
As Long
' Uncommon character string to assemble GroupKey and Key as a compound key.
Const KeySeparator As String = "¤§¤"
' Expected error codes to accept.
Const CannotAddKey As Long = 457
Const CannotRemoveKey As Long = 5
Static Keys As New Collection
Static GroupKeys As New Collection
Dim Count As Long
Dim CompoundKey As String
On Error GoTo Err_RowNumber
If Reset = True Then
' Erase the collection of keys and group key counts.
Set Keys = Nothing
Set GroupKeys = Nothing
Else
' Create a compound key to uniquely identify GroupKey and its Key.
' Note: If GroupKey is not used, only one element will be added.
CompoundKey = GroupKey & KeySeparator & Key
Count = Keys(CompoundKey)
If Count = 0 Then
' This record has not been enumerated.
'
' Will either fail if the group key is new, leaving Count as zero,
' or retrieve the count of already enumerated records with this group key.
Count = GroupKeys(GroupKey) + 1
If Count > 0 Then
' The group key has been recorded.
' Remove it to allow it to be recreated holding the new count.
GroupKeys.Remove (GroupKey)
Else
' This record is the first having this group key.
' Thus, the count is 1.
Count = 1
End If
' (Re)create the group key item with the value of the count of keys.
GroupKeys.Add Count, GroupKey
End If
' Add the key and its enumeration.
' This will be:
' Using no group key: Relative to the full recordset.
' Using a group key: Relative to the group key.
' Will fail if the key already has been created.
Keys.Add Count, CompoundKey
End If
' Return the key value as this is the row counter.
RowNumber = Count
Exit_RowNumber:
Exit Function
Err_RowNumber:
Select Case Err
Case CannotAddKey
' Key is present, thus cannot be added again.
Resume Next
Case CannotRemoveKey
' GroupKey is not present, thus cannot be removed.
Resume Next
Case Else
' Some other error. Ignore.
Resume Exit_RowNumber
End Select
End Function
Then you can build this query:
SELECT
Fruit.[Field A],
Format(RowNumber(CStr([Id]),[Field A]),"000") AS [Field B]
FROM
Fruit
WHERE
RowNumber(CStr([Id]))<>RowNumber("","",True);
which will output:
Is there a way for a report's field to take into account the format of a field in a query?
In example:
I have a StudentPercent field in a query. Values of the field are between 0 to 1, but since it is formatted to percent, they appear from 0% to 100% . When I run the report, it doesn't consider the format of the field and the values are between 0 to 1. Why is that?
Edit 1: I'm using Microsoft Access 2016.
Also, datas are populated dynamically, so I can't just set the format of the fields manually.
Edit 2:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
'Exit Sub
' Place values in text boxes and hide unused text boxes.
Dim intX As Integer
' Verify that not at end of recordset.
If Not rstReport.EOF Then
' If FormatCount is 1, place values from recordset into text boxes
' in detail section.
If Me.FormatCount = 1 Then
Me("Col" + Format(intColumnCount + 1)) = 0
For intX = 1 To intColumnCount
' Convert Null values to 0.
Me("Col" + Format(intX)) = Nz(rstReport(intX - 1))
If intX < intColumnCount Then
Me("Col" + Format(intColumnCount + 1)) = _
Me("Col" + Format(intColumnCount + 1)) + Nz(rstReport(intX))
End If
Next intX
' Hide unused text boxes in detail section.
'For intX = intColumnCount + 2 To conTotalColumns
'Me("Col" + Format(intX)).Visible = False
'Next intX
For intX = 2 To intColumnCount + 1
Me("Tot" + Format(intX)) = Nz(Me("Tot" + Format(intX))) + Nz(Me("Col" + Format(intX)))
Next intX
' Move to next record in recordset.
rstReport.MoveNext
End If
End If
End Sub
^ is the code of the detail part of my report.
I'm getting the error '13' - Type mismatch when I run the report after casting my field with Format(FieldName, "Percent") and the following code is highlighted:
Me("Col" + Format(intColumnCount + 1)) = _
Me("Col" + Format(intColumnCount + 1)) + Nz(rstReport(intX))
Set the Format property of the textbox in the report to: Percent
Or, expand the source query to have a field returning the formatted value as text:
StudentPercentText: Format([StudentPercent],"Percent")
Then use this field in your report and not the StudentPercent field. However, this is text, so you cannot use such a field in a calculation in the report.
I have a function I am trying to do for a database I am working on for my job. I'm not the most proficient with Access so I apologize if I am not wording this in the best way.
What I am trying to do is create a query/macro that will mimic the behavior as shown
and result into this:
The logic is as follows
1) for each record - take the LEN of the string in StdName. Take that number of characters and UPDATE that to the Name field. The remaining characters after the LEN is moved to the 'SuffixString' Field
2)for each record - count the number of occurrences of the string in the 'StdName' field for any records ON OR BEFORE the index number and UPDATE the 'Name' field with whatever is in there already and concatenate with "_n" where n is the occurence
example: index 1 - has one occurrence of 'Car1' in the StdName Field between record 1 and record 1. index 1 'Name' is changed to Car1_1
example: index 2 - has two occurrences of 'Car1' in the StdName Field between record 1 and record 2. index 2 'Name' is changed to Car1_2
example: index 6 - has one occurrence of 'Car3" in the StdName Field between record 1 and record 6. index 6 'Name' is changed to Car3_1
Can something like this be done with an access query? I've never developed in Access before and my boss really wants to see this function kept inside access instead of being moved in an out of excel.
(I have step 1 setup this way to later put in logic where StdName does not match Name. example: "Car1_1" for Name and StdName "Car2". I realize I could just Concatenate StdName with the function in step 2 in this example i described, but I have a real world purpose of doing it this way)
This will be done on an MDB format
Thank you
You can use my RowCounter function:
SELECT RowCounter(CStr([Index]),False,[StdName])) AS RowID, *
FROM YourTable
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));
or:
SELECT [StdName] & "_" & CStr(RowCounter(CStr([Index]),False,[StdName]))) AS RankedName, *
FROM YourTable
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));
Edit - to update:
UPDATE s_before
SET [Name] = [StdName] & "_" & CStr(RowCounter(CStr([Index]),False,[StdName]))
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));
Code:
Public Function RowCounter( _
ByVal strKey As String, _
ByVal booReset As Boolean, _
Optional ByVal strGroupKey As String) _
As Long
' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
' Optionally a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' Usage (with group key):
' SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
' Call RowCounter(vbNullString, False)
' 2. Run query:
' INSERT INTO tblTemp ( RowID )
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
' INSERT INTO tblTemp ( RowID )
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
' 2008-02-27. Optional group parameter added.
' 2010-08-04. Corrected that group key missed first row in group.
Static col As New Collection
Static strGroup As String
On Error GoTo Err_RowCounter
If booReset = True Then
Set col = Nothing
ElseIf strGroup <> strGroupKey Then
Set col = Nothing
strGroup = strGroupKey
col.Add 1, strKey
Else
col.Add col.Count + 1, strKey
End If
RowCounter = col(strKey)
Exit_RowCounter:
Exit Function
Err_RowCounter:
Select Case Err
Case 457
' Key is present.
Resume Next
Case Else
' Some other error.
Resume Exit_RowCounter
End Select
End Function
I am having an issue where I am trying to calculate the time difference in seconds and then in a report (Access reports) I will sum those seconds and format it into hh:nn:ss.
However, my calculated field that gathers the time difference between the two fields sometimes exceeds 24 hours and thus throws off the time difference.
I am using the DateDiff function --- DateDiff("s",[BeginningTime],[EndingTime])
What should I do when it comes to circumstances where the time exceeds 24 hours?
The two fields, BeginningTime and EndingTime, are stored in the AM/PM format. I don't think that should matter though.
You can use a function like this:
Public Function FormatHourMinute( _
ByVal datTime As Date, _
Optional ByVal strSeparator As String = ":") _
As String
' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
' datTime: #10:03# + #20:01#
' returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.
Dim strHour As String
Dim strMinute As String
Dim strHourMinute As String
strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
' Add leading zero to minute count when needed.
strMinute = Right("0" & CStr(Minute(datTime)), 2)
strHourMinute = strHour & strSeparator & strMinute
FormatHourMinute = strHourMinute
End Function
and this expression as ControlSource for your textbox:
=FormatHourMinute([EndingTime]-[BeginningTime])
However (see comments) this simple expression is only valid for dates of positive numeric value which are dates after 1899-12-30.
To cover all dates, you will need a proper method for calculating a timespan, and that can be done using this function:
' Converts a date value to a timespan value.
' Useful only for date values prior to 1899-12-30 as
' these have a negative numeric value.
'
' 2015-12-15. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateToTimespan( _
ByVal Value As Date) _
As Date
ConvDateToTimespan Value
DateToTimespan = Value
End Function
' Converts a date value by reference to a linear timespan value.
' Example:
'
' Date Time Timespan Date
' 19000101 0000 2 2
'
' 18991231 1800 1,75 1,75
' 18991231 1200 1,5 1,5
' 18991231 0600 1,25 1,25
' 18991231 0000 1 1
'
' 18991230 1800 0,75 0,75
' 18991230 1200 0,5 0,5
' 18991230 0600 0,25 0,25
' 18991230 0000 0 0
'
' 18991229 1800 -0,25 -1,75
' 18991229 1200 -0,5 -1,5
' 18991229 0600 -0,75 -1,25
' 18991229 0000 -1 -1
'
' 18991228 1800 -1,25 -2,75
' 18991228 1200 -1,5 -2,5
' 18991228 0600 -1,75 -2,25
' 18991228 0000 -2 -2
'
' 2015-12-15. Gustav Brock, Cactus Data ApS, CPH.
'
Public Sub ConvDateToTimespan( _
ByRef Value As Date)
Dim DatePart As Double
Dim TimePart As Double
If Value < 0 Then
' Get date (integer) part of Value shifted one day
' if a time part is present as -Int() rounds up.
DatePart = -Int(-Value)
' Retrieve and reverse time (decimal) part.
TimePart = DatePart - Value
' Assemble date and time part to return a timespan value.
Value = CDate(DatePart + TimePart)
Else
' Positive date values are identical to timespan values by design.
End If
End Sub
Then your expression will look like:
=FormatHourMinute(DateToTimespan([EndingTime])-DateToTimespan([BeginningTime]))
which for Gord's example values, #1899-12-28 01:00:00# and #1899-12-27 23:00:00#, will return 2:00.
I wish to not include the page number (in the page footer) for the first 10 pages of the report (i.e. page 1-10). Page 1 should read i, page 2 should read ii and page 3 should read iii and so on (in roman numerals).... When it gets to page 11, this should reset the page numbers
Does anyone know of the expression I can use to achieve this. So if GlobalPage number = 1,2,3,4,5,6,7,8,9,10 do not display, or compensate the globals page number for something else.....Is this possible.
You'll have to manually change the value i.e. putting something similar to the following in the footer:
IIf(Globals!PageNumber=1, "i", ...
Alternativally you could use a user function try VBA for number to roman numeral
We'll do ths with some custom code to keep flexibility. Microsoft have some code to do the Roman numeral conversion so we'll adapt this.
Let's add the custom code we need: one function to convert an integer to a Roman numeral and one function to work out what sort of numeral to provide.
Function PageNumber(page As Integer, startArabic As Integer) As String
If page <= startArabic Then
PageNumber = IntegerToRoman(page)
Else
PageNumber = (page - startArabic).ToString()
End If
End Function
Function IntegerToRoman (ByVal N As Integer) As String
Const Digits = "ivxlcdm"
Dim I As Integer
Dim Digit As Integer
Dim Temp As String
I = 1
Temp = ""
Do While N > 0
Digit = N Mod 10
N = N \ 10
Select Case Digit
Case 1
Temp = Mid(Digits, I, 1) & Temp
Case 2
Temp = Mid(Digits, I, 1) & Mid(Digits, I, 1) & Temp
Case 3
Temp = Mid(Digits, I, 1) & Mid(Digits, I, 1) & Mid(Digits, I, 1) & Temp
Case 4
Temp = Mid(Digits, I, 2) & Temp
Case 5
Temp = Mid(Digits, I + 1, 1) & Temp
Case 6
Temp = Mid(Digits, I + 1, 1) & Mid(Digits, I, 1) & Temp
Case 7
Temp = Mid(Digits, I + 1, 1) & Mid(Digits, I, 1) & Mid(Digits, I, 1) & Temp
Case 8
Temp = Mid(Digits, I + 1, 1) & Mid(Digits, I, 1) & Mid(Digits, I, 1) & Mid(Digits, I, 1) & Temp
Case 9
Temp = Mid(Digits, I, 1) & Mid(Digits, I + 2, 1) & Temp
End Select
I = I + 2
Loop
IntegerToRoman = Temp
End Function
To make the report more flexible, we'll add a parameter for when to revert to Arabic numerals (in case we need more than ten Roman numerals at some stage when the report gets longer). Let's call that #StartArabic and it will be an integer with the default value of 10. So now our page number expression is simply:
="Page " & Code.PageNumber(Globals!PageNumber, Parameters!StartArabic.Value)