Free Information Technology Magazines and eBooks

Friday, February 06, 2009

VB.NET: Use Enter key to move focus to next control like a TAB key

In Vb6, to mimic tab key we use the command SendKeys "{TAB}". For example if we want return or enter key to use like a TAB key then we do the following:

//set forms KEYPREVIEW = TRUE
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
SendKeys "{TAB}"
End If
End Sub


But on VB.NET this won't work and not practical. Instead we can utilize the SelectNextControl of the current active control as shown below:

Private Sub nametxt_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles nametxt.KeyDown, gendertxt.KeyDown, agetxt.KeyDown
If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
End If
End Sub


You can use the same event on all textboxes by adding the control.Keydown event from the Handles keyword of that event.

Private Sub nametxt_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles nametxt.KeyDown, gendertxt.KeyDown, agetxt.KeyDown //add any textbox here to use this event


That's all! your form's Enter key should now mimic the TAB key. Hope this help anyone.

Thursday, February 05, 2009

SQL: SELECT DISTINCT COUNT IN MS Access Database

One of Microsoft Access limitation is it doesn't support a very useful query SELECT COUNT with DISTINCT column like this one:

SELECT COUNT(DISTINCT columnname) FROM tablename


However the is a workaround. You can use the ability of the FROM statement to retrive from a SELECT query statement as shown below:

SELECT Count(Colname) FROM (SELECT DISTINCT Colname FROM myTable)
SELECT Count(Colname) FROM (SELECT DISTINCT Colname FROM myTable WHERE colfind=findvalue)


Now you have it. A SELECT DISTINCT COUNT for Microsoft Access. Until next time!