DMax to generate a new Unique ID - vba

I have a form in access that adds a new record into a table. I use DMax + 1 to get a new ID but is there a way to differentiate the different types of ID in a table?
I want to add the words PHS before the number. For example I want the ID to look like PHS001 etc.
Me.txtPATS.Value = DMax("PATS_ActionID", "tblPAT3") + 1

That could be like:
Me!txtPATS.Value = "PHS" & CStr(DMax("PATS_ActionID", "tblPAT3", "[PATS_ActionID] Like 'PHS*'") + 1))

Related

How to use second table to assign dummy variable?

I have two data sets. One is a multiple-variables one including an id variable (more than one entry have the same id), the second is vector of distinct id numbers.
I want to update the first data set assigning value 1 if the entry has an id that is in the second dataset.
Is merging the two the best way to do this?
Or is there a way such as
UPDATE `directory.dataset_1`
SET dummy = IF(id IN dataset_2.id =1,1, 0)
WHERE TRUE;
If the problem would have been solved in R a toy example would be:
dataset_1 <-
data.frame(c("000","001","010","011","000"),c("a","b","c","d","e"))
names(dataset_1) <- c("id","other")
dataset_2 <- data.frame(c("000","001"))
names(dataset_2) <- c("id")
result <- data.frame(c("000","001","010","011","000"),c("a","b","c","d","e"),
c(1,1,0,0,1))
names(result) <- c("id","other","dummy")
Logic need to go in the WHERE not in the SET, try the below
(Note: I'm unsure from your script if the ID needs to be =1 in dataset_2, so remove the WHERE from the sub query if not);
UPDATE dataset_1
SET dummy = 1
WHERE ID IN (SELECT ID
FROM dataset_2
WHERE ID = 1)

Multiple entries in crystal reportviewer after adding a SQL expression field

I am using Visual Studio 2017 and I installed the latest crystal reportviewer (22)
What I want is to click a button and create a report from the customer that is selected in the datagridview and the addresses that are shown in the second datagridview.
I managed to do all that but the problem is that a few fields contain numbers which need to be converted to text. An SQL query I would use to do this would be like:
SELECT c.customer_nr, c.status, s.rename FROM CUSTOMERS c INNER JOIN SETUP s on s.id = c.status WHERE s.afk = 'STA'
In my SETUP database I have the columns ID,AFK and RENAME so if the status would be 1 it would convert to text: "ACTIVE", if status = 2 it would convert to "INACTIVE" for example.
I could do something with a formula field like this:
IF ({c.status} = 1) THEN "ACTIVE" ELSE
IF ({c.status}) = 2 THEN "INACTIVE"
but that is not good because i could add another status or change the name in the database etc.
So then I tried with an SQL expression field and I put something like this:
(
SELECT "SETUP"."RENAME" FROM SETUP
WHERE "SETUP"."AFK" = 'STA' AND "SETUP"."ID" = "CUSTOMERS"."STATUS"
)
There must be something wrong because I get the correct conversion but there is only one address in the database but I get 7 pages all with the same address. There should only be one address like I get when I remove the SQL expression field. Where does it go wrong?
* EDIT *
I found the problem. When I create a new database that contains only unique id's then it works. In my original database I have multiple times the id's 1,2,3,4,5 but with different abbreviations in column AFK. Somehow the query looks for the id value and every time it finds this id no matter the AFK value it generates an entry for the address value.
Maybe in the future I will find out how this exactly works for now I have a workaround.
Create a new table for example CrRepSta and add the following entries:
ID,AFK,RENAME
1,STA,Active
2,STA,Inactive
etc
The new query:
(
SELECT "CrRepSta"."RENAME" FROM CrRepSta
WHERE "CrRepSta"."AFK" = 'STA' AND "CrRepSta"."ID" = "CUSTOMERS"."STATUS"
)
And by the way the statement "CrRepSta"."AFK" = 'STA' is not really needed.

How do I get a field of a datatable when I know column name and a row ID, without looping?

How do I get a field of a datatable when I know column name and a row ID, without looping?
For instance; I want the “Total Sold” value when Region = City and Product = Legos. This is something I have struggled with for a long time, probably because I think in SQL so looping through everything all the time doesn’t always seem like the correct way to go. BUT if I just need to learn to think like a VB developer and always loop to get something out of a list or table, please let me know.
I have a series of asserts comparing two datatables, one that has one row with a named column for each cell (from XML produced by SSRS), and another datatable that has one row for each unique value in a region (produced from a SQL query).
Table from SQL (mockup dataset):
Region Prod Total Sold
City Legos 68
State Legos 90
Nat. Legos 200
City ToyB 20
State ToyB 30
Nat. ToyB 40
City ToyC 450
State ToyC 600
Nat. ToyC 900
Table from XML (dataset returned from SSRS):
City_Legos State_Legos Nat_Legos City_ToyB State_ToyB Nat_ToyB City_ToyC State_ToyC
68 90 200 20 30 40 450 600
The part of the assert statement that gets data from the XML based datatable is easy, because there is only one row (index 0), and I can just name the column I want:
Dim xmlRow As DataRow = xmlDatatable.Rows(0)
Assert.AreEqual(“my SQL cell goes here”, xmlRow.Field(Of Integer)(“City_Legos”))
And I can do one assert for each of the specified columns. Many of the columns won’t be tested, and they have specific names, so I can’t simply loop through the columns. So what do I put in “my SQL cell goes here” to return the Total Sold for City and Legos? Obviously in SQL it would be easy: SELECT TOP 1 Total_Sold WHERE Region = City and Prod = Legos.
I am currently looping through the SQL datatable and testing each cell for my criteria, but that logic gets huge because I have to wrap each like in If…Then, like this:
For Each m As DataRow In mySqlTable.Rows
If m.Field(Of String)("Prod") = "Legos" Then
If m.Field(Of String)("Region") = "City" Then
Assert.AreEqual(m.Field(Of Integer)("TotalSold"), xmlRow.Field(Of Integer)(“City_Legos”))
End If
If m.Field(Of String)("Region") = "State" Then
Assert.AreEqual(m.Field(Of Integer)("TotalSold"), xmlRow.Field(Of Integer)(“State_Legos”))
End If
If m.Field(Of String)("Region") = "Nat" Then
Assert.AreEqual(m.Field(Of Integer)("TotalSold"), xmlRow.Field(Of Integer)(“Nat_Legos”))
End If
End If
Next
I’m hoping I can do something like a select or LINQ or Function?
Something like this would be nice:
Dim result as Integer = mySqlTable.Select.First(“Region = City and Prod = Legos“)
Or:
Assert.AreEqual(mySqlTable.Select.First(“Region = City and Prod = Legos“), xmlRow.Field(Of Integer)(“City_Legos”))
This is a unit test, so I will always know the column and field names returned by SSRS.
Getting the cell by Column name and row identifier has always been something I’ve struggled with, so hopefully I can finally get this solved.
Thanks!
You could use the Datatable Compute method. Replace (“my SQL cell goes here” with
CInt(mySqlTable.Compute("SUM([Total Sold])", "[Region] = 'City' and [Prod] = 'Legos'"))
This is assuming that the rows are unique and no null values.

Create an specific index

Basically i would like to add a new column in a table entitled product (mainly clothes) which contains 2 columns :
- ProductID (int)
- path (varchar) which is like something |x|y|z| (for example |10|300|5| with 10 indicated that the product is a woman cloth, 300 a t-shirt, and 5 yellow).
So i would like to add a new colum called index in order to allocate in my website a specific place for each products (for example the first picture will be a jean, the second one a coat, the 3rd a t-shirt, etc...). I have 8 zones with the possibility to extend this zone to 8 more, and so on.
I am trying to write an SQL update function such like this :
UPDATE productsTable
SET indexproducts =
SELECT CASE path WHEN path LIKE %|1 for example|%
THEN my index (this index should indicate that the product can be included in the first zone or the 9th zone etc... of my website)
ELSE WHEN path LIKE %|200 for example|%
THEN my index ( for the second zone) ELSE etc?...
END
FROM productsTable.
Any suggestions?
Example syntax:
update YourTable
set idx =
case
when path like '%least travelled%' then 1
when path like '%highway%' then 2
else 3
end
I did not understand your logic on what would be in the indexproducts column, but why not create a computed column:
alter table productsTable add indexproducts as
case path
when '1' then 'include on the first zone'
when '200' then 'second zone'
else '...'
end
then you can create an index on the indexproducts column

Help with a complex join query

Keep in mind I am using SQL 2000
I have two tables.
tblAutoPolicyList contains a field called PolicyIDList.
tblLossClaims contains two fields called LossPolicyID & PolicyReview.
I am writing a stored proc that will get the distinct PolicyID from PolicyIDList field, and loop through LossPolicyID field (if match is found, set PolicyReview to 'Y').
Sample table layout:
PolicyIDList LossPolicyID
9651XVB19 5021WWA85, 4421WWA20, 3314WWA31, 1121WAW11, 2221WLL99 Y
5021WWA85 3326WAC35, 1221AXA10, 9863AAA44, 5541RTY33, 9651XVB19 Y
0151ZVB19 4004WMN63, 1001WGA42, 8587ABA56, 8541RWW12, 9329KKB08 N
How would I go about writing the stored proc (looking for logic more than syntax)?
Keep in mind I am using SQL 2000.
Select LossPolicyID, * from tableName where charindex('PolicyID',LossPolicyID,1)>0
Basically, the idea is this:
'Unroll' tblLossClaims and return two columns: a tblLossClaims key (you didn't mention any, so I guess it's going to be LossPolicyID) and Item = a single item from LossPolicyID.
Find matches of unrolled.Item in tblAutoPolicyList.PolicyIDList.
Find matches of distinct matched.LossPolicyID in tblLossClaims.LossPolicyID.
Update tblLossClaims.PolicyReview accordingly.
The main UPDATE can look like this:
UPDATE claims
SET PolicyReview = 'Y'
FROM tblLossClaims claims
JOIN (
SELECT DISTINCT unrolled.LossPolicyID
FROM (
SELECT LossPolicyID, Item = itemof(LossPolicyID)
FROM unrolling_join
) unrolled
JOIN tblAutoPolicyList
ON unrolled.ID = tblAutoPolicyList.PolicyIDList
) matched
ON matched.LossPolicyID = claims.LossPolicyID
You can take advantage of the fixed item width and the fixed list format and thus easily split LossPolicyID without a UDF. I can see this done with the help of a number table and SUBSTRING(). unrolling_join in the above query is actually tblLossClaims joined with the number table.
Here's the definition of unrolled 'zoomed in':
...
(
SELECT LossPolicyID,
Item = SUBSTRING(LossPolicyID,
(v.number - 1) * #ItemLength + 1,
#ItemLength)
FROM tblLossClaims c
JOIN master..spt_values v ON v.type = 'P'
AND v.number BETWEEN 1 AND (LEN(c.LossPolicyID) + 2) / (#ItemLength + 2)
) unrolled
...
master..spt_values is a system table that is used here as the number table. Filter v.type = 'P' gives us a rowset with number values from 0 to 2047, which is narrowed down to the list of numbers from 1 to the number of items in LossPolicyID. Eventually v.number serves as an array index and is used to cut out single items.
#ItemLength is of course simply LEN(tblAutoPolicyList.PolicyIDList). I would probably also declared #ItemLength2 = #ItemLength + 2 so it wasn't calculated every time when applying the filter.
Basically, that's it, if I haven't missed anything.
If the PolicyIDList field is a delimited list, you have to first separate the individual policy IDs and create a temporary table with all of the results. Next up, use an update query on the tblLossClaims with 'where exists (select * from #temptable tt where tt.PolicyID = LossPolicyID).
Depending on the size of the table/data, you might wish to add an index to your temporary table.