Sunday, January 10, 2016

Return no result set with Min / Max

Hi,

Just in case if you wondering how not to return NULL rows when aggregate functions couldnt find value, this is how you can do it!


If Object_Id('tempdb..#myTable') Is Not Null 
Drop Table #myTable;
Go

Create Table #myTable(col1 Integer Null, col2 Integer Null)
Insert #myTable(col1) values (1)

;With t_max(tmax, tmin) As
       (Select Max(col2), Min(col2)
       From #myTable)
Select tmax, tmin
From t_max
Where tmax Is Not Null And tmin Is Not Null




HTH

Monday, December 21, 2015

Error accessing the system registry

If you get above error, make sure you open your Visual Studio 6.0 with "Run as Administrator" option.

You can also set this option permanently by going to the file location of Visual Basic 6.0 shortcut.
On the file, right click and go to Properties.
In Properties, select Compatibility tab.
In this tab, you will see "Run this program as an administrator" check box.
Check it and click OK.

This should do the trick permanently and now you don't need to open project by Run as Administrator option anymore.

HTH.

Tuesday, December 15, 2015

Parse Date in VB6

VB6 is not smart to understand some usual date formats that we use daily.
So for example, if you were to put yyymmdd in date type, VB6 will give Type mismatch error.


You would need to parse it to store in date type field as below:


Dim strDate as string
Dim dBusinessDate as date


strDate = "20151211"
dBusinessDate = Mid(strDate, 5, 2) & "/" & Right(strDate, 2) & "/" & Left(strDate, 4)

For more info: See Dates on MS page




HTH

Friday, November 13, 2015

Windows 10 Search with - in file name

If you have "-" in the beginning of word that you want to use to search file, Windows 10 wont be able to search.
To use search, you would replace "-" with "*".



Example:
If file name is: Hello-saqib-7526.txt, and you want to search files with "saqib", you would use *saqib or *7526.


HTH

Wednesday, November 4, 2015

Cast to Int16 - Type Conversion

In VB.net, if you need to convert number/string (assuming its number) to Int16, there isn't any function as Cint16 as there is for CInt.

Instead you would write :
Convert.Int16(value)
or
CType(value, Int16)

Thursday, October 29, 2015

Share folder Permissions - Just basic stuff

Once the folder is shared on network, all previous permissions need to be revised on the folder. The admin of machine where the folder is shared from doesn't even have permission once the folder is shared.

So 2 things need to be set under Sharing and Security tabs of folder.
Right click on folder and you see both these folders:

Sharing tab: Click on Share button. A window will open. Add the user and then on right column give necessary permissions to this newly added user.

Security tab: Click on Edit button. A window will open. Add the user and then on bottom pane, give necessary permissions to this newly added user.

There are many other things you can control hierarchical wise which you welcome to search on your own.


HTH.

Wednesday, October 28, 2015

How to get distinct records from array?

Using linq, it's pretty easy to get distinct records from array.
Lets say you have file filled with record and you read the file into array.
Now on this array, you can use lambda/linq expression to filter the list.
The code will keep the first record it found and throws all duplicates after it found the first one.

For my work, it was matter because I need to process in the order
they receive.


' Read complete file.
Dim lines() As String = File.ReadAllLines("C\dummyrecord.txt")


' group array items and get the first record.
Dim distinctList = lines.GroupBy(Function(x) x.ToString).Select(Function(grp) grp.First).ToArray


That's it!!


This line will return an String Array as well. Depending on your choice, you can also get
different data structures instead of array.


Linq Explanation:

2 functions are used: GroupBy and Select. Both take function as an argument which let you reference the items of object (array/list).
GroupBy will take function as argument using which you can reference elements of array/object.
Then you using Select method, you reference the grouped items and get the first record of many duplicate records.


HTH!