run a between query using wildcards - ms-access

Using Microsoft Access 2010
I have a field for [box_no]. I need to run a query to get a list of all box numbers within a range. Here is my issue....several box numbers have a letter in front of them (typically the letter "T"), several do not. If I use *Like* '*'+[Search Box Number]+'*' in the query I have no problem searching for a box with or without a letter. I can use *Between [beginning box number] And [ending box number]* in the query to retrieve a range of box numbers, as long as I include the corresponding letter(s). Is there a string or something I can write to get the result I want?
EXAMPLE: I want to retrieve a report for box numbers 732913000 to 732914000. 732913000 through 73213055 do not have a letter in the beginning. 73213056 has the letter T in the beginning (T73213056). I need to make sure all box numbers appear in the report, regardless of the beginning character.
I hope this makes sense! :-)

You can set up a function in VBA to strip out the leading character if its not numeric and then use that function in your query.
The function would be;
Function StripChar(BoxNumber As String) As String
If IsNumeric(Left(BoxNumber, 1)) Then
StripChar = BoxNumber
Else
StripChar = Right(BoxNumber, Len(BoxNumber) - 1)
End If
End Function
You can then use the function in your query;
SELECT BoxNumber, StripChar([BoxNumber]) AS Stripped
FROM <YourTable> WHERE (StripChar([BoxNumber]) Between 100 And 200));
You could probably put the whole thing together using SQL but it's probably easier to work with this because you can easily amend the VBA function to do the adaptation.

Related

What is the SSRS Multi Value Data Type and how to use

I have a multi-select.
I think the underlying datatype is int || array(int). This is pretty frustrating that you have to do a check to see if a multi-value is present before jumping into an index. But how does this value get passed to SQL?
It's easy enough to use in a IN (#variable) statement. How else can it be used? Is it a string or a table. From my investigations it appears to be single table row with many un-named columns but I'm not really sure.
Finally, when you want to simulate a multi-select in a query inside visual studio, for example to "Refresh Fields" how do you do that? For example "1,2,3", {1,2,3} or #{1,2,3}. It's not (123) because that is -123.
It dpends what you are trying to do and in what context.
As you said, if you have a datset query that is a SQL script (as opposed to a stored proc) then you can use IN(#paramName). In this instance SSRS take the parameter values (not the labels) and injects them into the sql statement as a string e.g. '1,2,3'. The result would be IN(1,2,3). If you want to pass in a list of, say, countries then you would have to set the parameter values to be the same as the parameter labels So rather then Value =1, Label = Spain you would have Value = Spain and Label = Spain. Used in an IN() would generate something like IN('Spain', 'France').
If you try to do the same with a stored proc e.g. EXEC myProc #myParam, then the parameter values would be passed as a sing string which would then need to be split out by the proc.
If you just want to get a list of selected parmeter values or label shoing in your report then you can simply do something like
=JOIN(Parameters!myParam.Value, ",")
or
=JOIN(Parameters!myParam.Label, ",")
where "," is the delimiter
If you pop this expression in a text box, you'll get a list of the selected parmater values/labels
I think it's a kind of madness but I found a workaround to get a table of values from the results from SSRS. I query the IDs against a source table using IN(). I hope there is a better way of doing this?
SELECT [TblFeeBillingCycleID]
FROM [TblFeeBillingCycle]
WHERE [TblFeeBillingCycleID] IN(#intCycleId)

In SSRS, how to include first row from different dataset in tablix?

I am creating a report, the purpose of which is to print a letter to many different people. Obviously each person's name, email, etc. will be different. For this I am using a list, which I understand uses a tablix.
Now inside each letter I also need some global data that comes from a dataset. For example, the company email, telephone number, etc. This data will be the same for every letter. However, every time I try to use some expression to get this, I get an error such as:
The Value expression for the text box ‘Textbox11’ refers to the
field ‘URL’. Report item expressions can only refer to fields within
the current dataset scope or, if inside an aggregate, the specified
dataset scope. Letters in the names of fields must use the correct
case.
The expression I'm using to get the above error is
=LookupSet(true, true, Fields!URL, "SystemVars")
I've tried other things but I can't figure out what I need to make it word.
Is there an expression I can use to solve this problem? If not, what steps should I take to get my letters working?
You are missing the ".Value" portion in the expression. Try this:
=First(Fields!URL.Value, "SystemVars")

Remove string with wildcard in Notepad++

I'm trying to merge multiple JSON data sets into one large data set, due to a max limit of 100 on the server I'm pulling them from.
The easiest way to do this would be to eliminate the end of one set and the beginning of the next and replace it with "," so that there would be only one open and close to the entire large set. This is what appears between the last entry of one set and the first entry of the next currently:
],"version":"1.0"}{"error":"OK","limit":100,"offset":100,"number_of_page_results":100,
"number_of_total_results":20235,"status_code":1,"results":[
Again, I need that entire string replaced with just a comma, but the problem I'm encountering is that I had to change the offset between each data set to grab the next 100 entries, so the "offset":100, is different in each string ("offset":200, "offset":300, etc.). I can't seem to get wildcards to cooperate. I suspect it has something to do with all the brackets that are already in the string.
Any help would be appreciated. Thank you.
A regular expression that matches the whole input you provided (provided there's no new line characters) is:
\],"version":"1\.0"\}\{"error":"OK","limit":[0-9]+,"offset":[0-9]+,"number_of_page_results":[0-9]+,"number_of_total_results":[0-9]+,"status_code":[0-9]+,"results":\[
It will get any digits in place off all the numbers in your sample (except version).

How to AutoSearch from any part of a string and not from the beginning when user starts typing in a combobox?

How can I autosearch and find matches from any part of a string rather than from the beginning when user starts typing in a word in the combobox?
For eg. If user starts typing in "001007228" it should still display "GEM GRAVURECOINC - 001007228" as a narrowed down selection result. Else if user types "Gravure" it should still display "GEM GRAVURECOINC - 001007228" as a narrowed down selection from the list of values in a combobox.
It so happens that some users might remember part of the name or the numbers at the end at times.
Your sql statement would use the Like operator, something like this:
"SELECT * FROM tblSome WHERE FieldName Like '*" & Me!cboYourCombo & "*'"
The asterisks * are wildcard characters, which match any sequence of characters.
Me can only be used in the Form's Module, otherwise you would use Forms!frmYours!cboYourCombo, where Forms refers to all open forms.
You need to trap the KeyPress event using VBA code in your database.
Press ALT-F11 to bring up the code behind the database and use something like this ..
Private Sub Combo2_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 65 To 90, 97 To 122 'alphachars only
Combo2.ListIndex = FindIndex(Combo2.Value, KeyAscii)
Case vbKeyDecimal
Case vbKeyBack
Case vbKeyRight
Case vbKeyLeft
Case vbKeyInsert
Case vbKeyDelete
Case vbKeyShift
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub
Where 'cboYourCombo' is the actual object name of your actual combo-box control.
And inside the above subroutine trap the keystrokes that matter such as numbers and letters
This line ...
Combo2.ListIndex = FindIndex(Combo2.Value, KeyAscii)
... is saying that it's trying to run a FindIndex() function (you have to write it) and it returns the location in the string where the character starts and then set the index in the combobox to start at that location. Actually this won't work, but you get the idea.
Well actually, you need to pass the entire list in the combobox to a routine that finds the location of the data you want in the FindIndex() function and return back the characters prior to the location you sought. In essence, you are telling the combobox to look from the left of the field to the right, but you already found the location of the data in the above routine.
ie.
Say we have this list ...
ABC123
ABC456
AHJ738
BHS238
MKJ299
If you code the above so that when ever you press a key, it goes and looks for the FIRST item in the above list that has that character. Lets say in this case the number 7.
When you press '7', the routine found the 'AHJ738' item and this happens to be the 3rd item in the list.
So it returns for you 'AHJ7' (at least) and this will trick the combobox to find it from the left of the field, if you know what I mean.
In other words, there is no pre-built method in the combobox control to seek out keystrokes in the middle of the text box. Unless you Google for alternative combobox controls that can do this, which are commercial variants.

MYSQL — Number formatting issue

I'm working on a gig right now where the client wants the user to be able to search for a product by product code.
A product code is formatted like so: 123.4567.89
So, the search box should return that product whether the user enters the number with the periods, without the periods, or with spaces.
So, all of the following should return the product: 123.4567.89, 123456789, 123 4567 89.
My current query looks like so:
SELECT *
FROM products
WHERE product_code LIKE '%$search_code%'"
I'm at a loss as to how I would revise that to include all the different possibilities of how a user would input these numbers.
Thanks in advance for any help.
[Front End] Limit the characters the user can enter. Only allow periods and spaces. Don't allow any alpha characters (if all your product SKUs are numerical).
[Middle Tier] After the form is posted, double check the data for extraneous characters on the back end. If somehow the client managed to bipass the validation on the front end, you can catch it on the back end. Use a simple search and replace in your language of choice.
[Database/Back-End] Once the data is restricted to only numeric digits and you send the SKU to your database query, strip out all periods on your products table. If you know you only use periods to store the SKUs, just search excluding them, e.g.
SELECT *
FROM products
WHERE REPLACE(product_code,'.','') = #productCode
Avoid wildcard %% searches, they're expensive.
You have to normalize the number input by the user before doing the search, that is: make it have the same format as the number stored in the database.
For instance, if the numbers are stored in the database without the periods (like 123456789), you have to pre-process the number input by the user to also remove the periods, spaces and any other characters from it.
Edit: if the numbers are stored in the database with the periods, than you also need to normalize them by removing the periods as #HertzaHaeon pointed in his answer.
How about removing dots and spaces in both the database code and the searcg input, so you have just the digits? Something like this:
WHERE REPLACE(product_code, '.', '') LIKE '%formatted_search_code%'
For the search input, you can strip everything but digits form it with a regular expression or simple substring replacement.
I think the best solution to your problem is to format the search value so that is matches the format used in your database. But than you will only find the product, if the user fills in the whole product number. If this is not the desired solution and you want to be able to let the user fill in any part of a product code and find al the products that have a code containing that that I think you should filter out the periods in your database.
The fasted solution would be to do it actually in your database. Remove the dots from your product code or add an extra field containing the product code without dots. This will speed up the query when the dataset gets larger.
If you not want to do that you can always filter out the dots in the search query:
REPLACE(product_code,'.','') LIKE '%$search_code%'
This will do the thrick but can be very slow when the dataset gets bigger.
I work with this all the time with social-security numbers. My solution is to take your input string, strip out and characters that are not digits, make sure that the string is the proper length and then use the substring function to break the string up and then put it back together with the delimiters. If you're using PHP, the function might look like this this:
<?php
function FormatProductCode($String) {
$String = preg_replace("/[^0-9]/", "", $String);
if ($String == "") {
return null;
}
$String = str_pad($String, 9, "0", STR_PAD_LEFT);
return substr($String, 0, 3) . "." . substr($String, 3, 4) . "." . substr($String, 7, 2);
}
?>
Use this function any time that you need to input data into the database or compare data.