Sunday 22 September 2013

Accept Only Integer in specific DatagridView Column or Cell

Public Class Form1
 
    Private Sub dgv1_EditingControlShowing(ByVal sender As ObjectByVal e As DataGridViewEditingControlShowingEventArgsHandles dgv1.EditingControlShowing
        If Me.dgv1.CurrentCell.ColumnIndex = 1 Then
            AddHandler e.Control.KeyPressAddressOf Handel_Column1_KeyPress
        End If
    End Sub
 
    Private Sub Handel_Column1_KeyPress(ByVal sender As System.Windows.Forms.DataGridViewTextBoxEditingControlByVal e As KeyPressEventArgs)
        If dgv1.CurrentCell.ColumnIndex = 1
            Dim valid_entry = "0123456789" + ChrW(Keys.Back)
            If valid_entry.Contains(e.KeyChar) Then
                e.Handled = False
            Else
                e.Handled = True
            End If
        End If
        
    End Sub
 
End Class

Wednesday 24 July 2013

VB Helper: HowTo: Get the values of fields (variables) declared in a form by their names in Visual Basic .NET

VB Helper: HowTo: Get the values of fields (variables) declared in a form by their names in Visual Basic .NET

Private private_value1 As String = "This is private value 1"
Private private_value2 As String = "This is private value 2"
Public public_value1 As String = "This is public value 1"
Public public_value2 As String = "This is public value 2"
Public arr1() As String = {"A", "B", "C"}
Public arr2() As String = {"1", "2", "3"}

Private Sub cboFields_SelectedIndexChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    cboFields.SelectedIndexChanged
    Dim field_info As FieldInfo = _
        Me.GetType().GetField(cboFields.Text, _
        BindingFlags.Instance Or BindingFlags.NonPublic Or _
            BindingFlags.Public)
    If field_info Is Nothing Then
        lblValue.Text = ""
    ElseIf field_info.FieldType.IsArray() Then
        ' Join the array values into a string.
        lblValue.Text = Join(field_info.GetValue(Me))
    Else
        ' Just convert it into a string.
        lblValue.Text = field_info.GetValue(Me).ToString()
    End If
End Sub

Wednesday 26 June 2013

MS Access: DateDiff Function

MS Access: DateDiff Function

MS Access: DateDiff Function

In Microsoft Access, the DateDiff function returns the difference between two date values, based on the interval specified.

Syntax

The syntax for the DateDiff function is:
DateDiff ( interval, date1, date2, [firstdayofweek], [firstweekofyear])
interval is the interval of time to use to calculate the difference between date1 and date2. Below is a list of valid interval values.
IntervalExplanation
yyyyYear
qQuarter
mMonth
yDay of year
dDay
wWeekday
wwWeek
hHour
nMinute
sSecond
date1 and date2 are the two dates to calculate the difference between.
firstdayofweek is optional. It is a constant that specifies the first day of the week. If this parameter is omitted, Access assumes that Sunday is the first day of the week. This parameter can be one of the following values:
ConstantValueExplanation
vbUseSystem0Use the NLS API setting
vbSunday1Sunday (default)
vbMonday2Monday
vbTuesday3Tuesday
vbWednesday4Wednesday
vbThursday5Thursday
vbFriday6Friday
vbSaturday7Saturday
firstweekofyear is optional. It is a constant that specifies the first week of the year. If this parameter is omitted, Access assumes that the week containing Jan 1st is the first week of the year. This parameter can be one of the following values:
ConstantValueExplanation
vbUseSystem0Use the NSL API setting
vbFirstJan11Use the first week that includes Jan 1st (default)
vbFirstFourDays2Use the first week in the year that has at least 4 days
vbFirstFullWeek3Use the first full week of the year

Applies To

  • Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

For Example

DateDiff ("yyyy", #15/10/1998#, #22/11/2003#)would return 5
DateDiff ("m", #15/10/2003#, #22/11/2003#)would return 1
DateDiff ("d", #15/10/2003#, #22/11/2003#)would return 38

VBA Code

The DateDiff function can be used in VBA code. For example:
Dim LValue As Integer

LValue = DateDiff ("d", #15/10/2003#, #22/11/2003#)
In this example, the variable called LValue would now contain the value of 38.

SQL/Queries

You can also use the DateDiff function in a query.
Microsoft Access

Saturday 26 January 2013

Use IN Clause In LInqToSQL (vb.net)

Example #1 : The IN will be fetch from textbox against a field of Type Integer


Dim in_values = Me.TextBox1.Text.Split(vbCrLf).ToList.Select(Function (f) CInt(f)).ToList

Dim query = From t In db.table
            Where in_values.Contains(t.integer_field.Value) 
            Select t




Wednesday 23 January 2013

How to insert rows in datagridview which is placed on form2 in vb.net as add button is on form1

I had searched a lot for a decend solution to this but all I can find is (refill the datatable) which is ofcource an answer but that let me re-load all the rows again from the datasource (which I did not like).

So here is my way of doing it (using typed dataset)

Assume you had form1 which had a datagridview and and add button

Assume you had form2 which had the textboxes which will hold the new row data

Assume our table name is CUSTOMER with the following structure
(ID as integer,NAME as string ,BOD as Date)

Assume In form1 the datagridview is filled with data rows that already exist in CUSTOMER

Assume in form1 you had ta_customer (Customer Table Adapter)

Assume in form1 you had ds_database (dataset name)

Assume in form1 you had bs_customer  (Customer binding source)

In form 2, add any field from the datasource you have just to automatically create (Customer Table Adapter,Dataset, Customer binding source , TableAdapterManager)

in form2 Assume the names of the above are:
F2_CustomerTableAdapter, F2_dataset, F2_CustomerBindingSource, F2_TableAdapterManager

In form1 Add Button (btn_add) Code the below

Dim NewForm2 As New Form2(ds_database,bs_customer)
frm.Show()
In form2 
Public Class form2
 
    Private Sub btn_save_ClickByVal sender As System.Object,  ByVal e As System.EventArgsHandles btn_save.Click
        Try
            F2_CustomerBindingSource.AddNew
            F2_CustomerBindingSource.Current("id") = 1000
            F2_CustomerBindingSource.Current("name") = "student_name"
            F2_CustomerBindingSource.Current("dob") = Now
            bs_Farm.EndEdit
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Ta_farm.Update(Ds_farms.farm)
    End Sub
 
    Sub new (ByVal ds As DataSet,bs As BindingSource)
 
        ' This call is required by the designer.
        InitializeComponent()
        
        ' Add any initialization after the InitializeComponent() call.
  
        Me.F2_dataset                             = ds
        Me.F2_CustomerBindingSource.DataSource   = Me.Ds_farms
        Me.F2_CustomerBindingSource.Position     = bs.Position
 
    End Sub
 
    Private Sub frm_01_new_farm3_LoadByVal sender As System.Object,  ByVal e As System.EventArgsHandles MyBase.Load
        'TODO: This line of code loads data into the 'Ds_farms.farm' table. You can move, or remove it, as needed.    
    End Sub
 
End Class


Close Form2, and when you get back to form1 you will see the new inserted record are shown in the datagridview

As you can notice I used Me.f2_CustomerBindingSource.Position   = bs.Position, If you assign the textbox databinding with a value from the form2 binding source you can use it to edit a row as well.

I will explain more about this in my next blog about editing a row displayed in form1 datagridview in form2 using the same technique I used above.

Have fun.