Ordinal numbers in Microsoft Access Query - ms-access

I have a table with ranks. Now I want the ranks to have the suffixes 'St', 'nd', 'rd', 'th' in Microsoft Access. I want to know if there is a way to make that happen using access query. Thanks for the response in advance

This code from Chip Pearsons site will do the trick:
Public Function OrdinalSuffix(ByVal Num As Long) As String
Dim N As Long
Const cSfx = "stndrdthththththth" ' 2 char suffixes
N = Num Mod 100
If ((Abs(N) >= 10) And (Abs(N) <= 19)) _
Or ((Abs(N) Mod 10) = 0) Then
OrdinalSuffix = "th"
Else
OrdinalSuffix = Mid(cSfx, _
((Abs(N) Mod 10) * 2) - 1, 2)
End If
End Function
You'd write it into your query as:
SELECT MyField & OrdinalSuffix(MyField)
FROM MyTable
The formula only version is:
IIf(MyField-100*INT(MyField/100)>=10 And MyField-100*INT(MyField/100)<=14,"th",Choose(MyField-10*INT(MyField/10)+1,"th","st","nd","rd","th","th","th","th","th","th"))
Written as
SELECT MyField & IIf(MyField-100*INT(MyField/100)>=10 And MyField-100*INT(MyField/100)<=14,"th",Choose(MyField-10*INT(MyField/10)+1,"th","st","nd","rd","th","th","th","th","th","th"))
FROM MyTable

Related

Void out parameter of SQL query if empty cell selection from Excel - VBA

Instead of using Nested If statements, I was wondering if there is a way to void out parts of a string query if cell value is left blank.
Cell structure is as below:
Cell values from these parameters will get passed into vba code that queries a database.
Ideally I don't want to create an individual query for each selection type - and I have it dynamically querying from location already. I want to extend the query to include possible combinations of start, end, value >, value <, while also making it so that if cell value is left blank, then ignore that parameter. So say
SELECT *
from database
WHERE location = 'cell_loc'
AND Value >= 'cell_value'
AND Value <= 'cell_value'
AND Start >= 'cell_date'
AND End <= 'cell_date'
Now imagine that Start is left blank, meaning I want to query from first data point in the database:
I could write a nested if to handle this, but was wondering if there was a way to void out a query parameter so that I could just have a single query fed to database with different parameters changing based off cell data?
Something along the lines of:
SELECT *
from database
WHERE location = 'cell_loc'
AND Value >= 'cell_value'
AND Value <= 'cell_value'
AND Start >= 'cell_date' --> this would be voided out
AND End <= 'cell_date'
Using the coalesce() function you can put an equality condition in your WHERE clause. This is a common SQL trick to deal with null parameters or null values in the data.
SELECT *
from database
WHERE location = 'cell_loc'
AND Value >= 'cell_value'
AND Value <= 'cell_value'
AND (Start >= 'cell_date' OR Start = coalesce('cell date', Start))
AND End <= 'cell_date'
Here's a very basic example:
Sub Tester()
Dim sWhere As String, sql As String
sql = "Select * from myTable "
sWhere = ""
BuildWhere sWhere, "id = <v>", Range("A5")
BuildWhere sWhere, "pName = '<v>'", Range("B5")
BuildWhere sWhere, "pDate > '<v>'", Range("C5")
If Len(sWhere) > 0 Then
sql = sql & " where " & sWhere
Debug.Print sql
'run query
Else
'don't run if no criteria ?
End If
End Sub
'add a where clause only if `c` has a value
Sub BuildWhere(ByRef sWhere As String, test As String, c As Range)
Dim v
v = Trim(c.Value)
If Len(v) > 0 Then
If Len(sWhere) > 0 Then sWhere = sWhere & vbLf & " and "
sWhere = sWhere & Replace(test, "<v>", v)
End If
End Sub

How to add new Access record by checkbox and number of different date?

I have query 1 or table 1 have the below data
Number of employee/start date/end date/code/different date by Days/checkbox
Example
200/01-01-2021/15-01-2021/E/14/Yes
I need when checkbox=Yes to open 14 record automatically in new table 2 with code like below:
employee/date/code
200/01-01-2021/E
200/02-01-2021/E
200/03-01-2021/E
200/04-01-2021/E
200/05-01-2021/E
200/06-01-2021/E
200/07-01-2021/E
200/08-01-2021/E
200/09-01-2021/E
200/10-01-2021/E
200/11-01-2021/E
200/12-01-2021/E
200/13-01-2021/E
200/14-01-2021/E
Extract your parameters:
Data = "200/01-01-2021/15-01-2021/E/14/Yes"
ENo = Split(Data, "/")(0)
FirstDate = DateValue(Split(Data, "/")(1))
ECode = Split(Data, "/")(3)
Periods = Split(Data, "/")(4)
Then run this query passing it the parameters:
PARAMETERS
Periods Short,
FirstDate DateTime;
SELECT DISTINCT
10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10) + 1 AS Sequence,
ENo As Employee,
DateAdd("d", [Sequence] - 1, [FirstDate]) AS DateStart,
ECode As Code
FROM
MSysObjects AS Uno,
MSysObjects AS Deca
WHERE
(10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10)) < [Periods];
Output:
You can the use this query as source in an append query.
Or, alternatively, use DAO to directly append the records:
Dim Records As DAO.Recordset
Dim Index As Integer
Set Records = CurrentDb.OpenRecordset("Select * From Table2")
For Index = 0 To Periods - 1
Records.AddNew
Records!Employee.Value = Eno
Records!Date.Value = DateAdd("d", Index, FirstDate)
Records!Code.Value = ECode
Records.Update
Next
Records.Close

Is there any way to get alphabetical numbering of rows in ssrs instead of numerical numbering?

What I need (alphabetical numbering of rows-highlighted in bold(serial column)):
I have tried converting the output of rownumber function into string, But nothing seems to work as I don't have any idea.
Please help!
You can do this with a bit of custom code.
Go to the Report Properties, click the "Code" tab and paste the following code into the custom code window.
Public Function GetRowLetter(RowNum As Integer) As String
' stick the RowNum in a variable that we can reduce until it's zero
Dim r As Integer
Dim i As Integer
Dim s As String ' holds result
s = ""
r = RowNum
' we start at the right side so if the rownum is 28 we want to be back AB
' need to get 'B' first
Do While RowNum > 0
r = Int((RowNum - 1) / 26)
i = (RowNum - 1) Mod 26
s = Chr(i + 65) & s
RowNum = r
Loop
GetRowLetter = s
End Function
This will give "A" for 1, "B" for 2 etc, then it will give "AA" for 27, "AB" or 28 etc...
If you want to return lower case letters instead, swap the 65 for 98
In your report set the textbox value expression to
=Code.GetRowLetter(RowNumber("myDataSetName"))
swap out myDataSetName with the name of your dataset or scope you want to apply it to. Remember the dataset and scope names are case sensitive and must be surrounded by quotes ( " )

generate random alphanumeric key in Ms. Access

by having this code on VB Script
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ltr, rNum, AlphaLtrs, selLtr
If Not Intersect(Target, Me.Columns(2)) Is Nothing And _
Target.Cells.Count = 1 Then
If Target.Value = "" Then
AlphaLtrs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ"
selLtr = Application.RoundUp(Rnd() * 26, 0)
ltr = Mid(AlphaLtrs, selLtr, 1)
rNum = Application.RoundUp(Rnd() * 999999, 0)
Target.Value = Me.Range("A" & Target.Row) & "-" & ltr & rNum
End If
End If
End Sub
I need to create it through Ms. Access, Generally for Stock Keeping Products
Ms Access Table Contains this Fields
ID [Primary Key]
Product Title [Short text]
Type [Short text]
SKU [Short text]
Image Preview [Attachment]
Price [Number]
Availability [yes/No]
what i need is SKU will be auto Generate Keys for any new Product, Unique Key, may be by a button on form while inserting a new entry or may be by initially new entry , Key will be same as the Excel Code e.g. Z401374 (alpha numeric) and once Generated cant be change
Drop this into a module and run it. You should get what you need.
Sub test()
Dim s As String * 7 'fixed length string with 7 characters
Dim n As Integer
Dim ch As Integer 'the character
For n = 1 To Len(s) 'don't hardcode the length twice
Do
ch = Rnd() * 127 'This could be more efficient.
'48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
Next
Debug.Print s
End Sub
What you'll probably then have to do is do an INNER JOIN on your table (or SELECT SKU from tblProducts WHERE SKU = "the above generated string") to make sure it didn't randomly generate the same string twice. Eventually, with enough SKUs, that will happen. If it did, just re-run the generator and test again until you don't find a match, and then you know you have a unique SKU.

VBA data type error - bad syntax?

Hopefully someone can help me out with this. I have written a query in Access 2003 that combines a linked table "taxon_group_max_per_site" and a cross tab query "Distinct Species by group_Crosstab".
From the table I have the field "Taxonomic Group" and "Max", and from the cross tab the fields "Total_Of_Species_S".
The table and the cross tab are linked and the query works fine until I add in some VBA to give each Taxonomic group a score based on "Max" and "Total_Of_Species_S".
The code below brings up "Error 13: type mismatch"
Public Function Invert_Diversity_Score (Total_Of_Species_S As Integer) As Integer
If Total_Of_Species_S < Round("[Max]*0.5", 0) Then
Invert_Diversity_Score = 0
Else
If Total_Of_Species_S < Round("[Max] * 0.75", 0) Then
Invert_Diversity_Score = 1
Else
If Total_Of_Species_S < Round("[Max] * 0.875", 0) Then
Invert_Diversity_Score = 2
Else
Invert_Diversity_Score = 3
End If
End If
End If
End Function
The debugger shows that "[Max]*0.5" and the other multiplications do not produce a number it says "[Max] * 0.5"= "[Max] * 0.5", which I think is the source of the type mismatch. How do I get the field to multiple properly? It looks exactly like the format shown in the VBA help.
The round function is expecting a number as parameter, not a string! Assuming (max) is a number, you can then calculate:
Round([Max] * 0.75, 0)
But
Round("[Max] * 0.75", 0)
Will definitely not return anything viable
"[Max] * 0.875" is just a string, how is VBA supposed to know that you are referring to the column [Max] from one of your tables?
Shouldn't [Max] be passed into the function as a second integer parameter? Something like this:
Public Function Invert_Diversity_Score (Total_Of_Species_S As Integer,
MaxVal as Integer) As Integer
We need the code that shows how you are calling the function to really sort this out ...
For one, you should use the ElseIf keyword, there is no need to stack the Ifs here.
Second - what is "[Max]*0.5" supposed to mean? To VB, it is a string, which unsurprisingly you can't multiply with an integer.
Assuming Max is some kind of global constant:
Public Function Invert_Diversity_Score(Total_Of_Species_S As Integer) As Integer
If Total_Of_Species_S < Round(Max * 0.5, 0) Then
Invert_Diversity_Score = 0
ElseIf Total_Of_Species_S < Round(Max * 0.75, 0) Then
Invert_Diversity_Score = 1
ElseIf Total_Of_Species_S < Round(Max * 0.875, 0) Then
Invert_Diversity_Score = 2
Else
Invert_Diversity_Score = 3
End If
End Function
If it is not a constant, then you must pass it into the function as well:
Public Function Invert_Diversity_Score( _
Total_Of_Species_S As Integer, _
Max as Integer _
) As Integer