user3522506
user3522506

Reputation: 55

Detecting With VB If Yes/No Field Is Checked in MSAccess

I need to detect if the "Timed_In" field is -1 or 0 according to the employee number in the parameter. I have only gotten thus far with this code.

Maybe I should make a Boolean function and return if it has detected "True" in that specific row but I don't know how to do it.

My deep gratitude in helping take a further step in VB.net programming!

Public Shared Function CheckTimeIn(ByVal empid As String)

    Dim dtoffenseinfo As New DataTable

    If Not DBConnection.State = ConnectionState.Open Then
        'open connection
        DBConnection.Open()
    Else

    End If


    Dim adapter As New OleDbDataAdapter("SELECT Timed_In from EmployeeRegistration  where EMployeeID='" & empid & "'", DBConnection)

    adapter.Fill(dtoffenseinfo)
    DBConnection.Close()

    Return dtoffenseinfo

End Function

Upvotes: 0

Views: 737

Answers (1)

JoelC
JoelC

Reputation: 3744

I have this function that I fall back on for nasty "Boolean" values that different data sources are not storing properly:

    ''' <summary>
''' Converts any string to a boolean value. Any non-boolean or unexpected value will
''' convert to False.
''' 
''' True values (case insensitive):
''' -1
''' 1
''' yes
''' t
''' true
''' y
''' </summary>
Public Function ToBoolean(value As String) As Boolean
    Select Case value.ToUpper().Trim()
        Case "-1", "1", "YES", "T", "TRUE", "Y"
            Return True
        Case Else
            Return False
    End Select
End Function

EDIT: As a side note, you will want to carefully check that your query isn't returning DBNull, since that will cause a casting error with strings in .NET. A routine like this will need to be called before you call ToBoolean:

    Public Function DBValueToString(value As Object, Optional defaultValue As String = "") As String
    If value Is Nothing Then
        Return defaultValue
    ElseIf IsDBNull(value) Then
        Return defaultValue
    Else
        Return Convert.ToString(value)
    End If
End Function

EDIT 2: Also, your routine is returning an entire data table when you appear to just want the value of a single record. Instead of using an adapter and filling a table you could just using a OleDbCommand and call ExecuteScalar on it. This will return you a single value, but your query must never return multiple columns or rows (yours appears to be fine in this respect).

The other option is to call something like ToBoolean(DBValueToString(dtoffenseinfo.Rows(0)("Timed_In"))) with your existing code. Of course, you will need to check that rows have been returned before you make this call.

Upvotes: 2

Related Questions