如果记录已存在于vb访问中,则更新记录

时间:2017-03-20 20:17:50

标签: vb.net ms-access

我试图检查一个客户端是否已经登录。如果是,那么该按钮将签出他而不是完全添加新记录,而我正在努力寻找解决方案。 这是我目前使用的代码:

        Private Sub btnSignIn_Click(sender As Object, e As EventArgs) Handles btnSignIn.Click
    Dim cmd As New OleDbCommand
    '     cnn = cnn
    Try
        If Not cnn.State = ConnectionState.Open Then 'open the database connection
            cnn.Open()
        End If

        If txtClientName.Text = Nothing Then            'check to see if the name field is empty
            MsgBox("Please enter a name to sign in")
            txtClientName.Focus()

        ElseIf txtDateTime.Text = Nothing Then          ' checks if timeslip is empty
            MsgBox("Please enter a valid time to sign in")
            txtDateTime.Focus()
        Else                                        'if no fields are empty proceed with code
            cmd.Connection = cnn
            cmd.CommandText = "INSERT INTO LogSheet (ClientName, SignInTime, CurrentDate)" &
                               "VALUES(?, ?, ?)"
            cmd.Parameters.AddWithValue("@p1", txtClientName.Text.ToString.ToUpper)
            cmd.Parameters.AddWithValue("@p2", txtDateTime.Text)
            cmd.Parameters.AddWithValue("@p3", Date.Now().ToShortDateString)

            cmd.ExecuteNonQuery()
            RefreshData()
            txtClientName.Clear()
            txtDateTime.Clear()

        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message & " - " & ex.Source)
        cnn.Close()
    End Try

End Sub

那里没有验证,但香港专业教育学院尝试了许多不同的代码,没有运气.. 我只是希望脚本检查,如果客户端已登录,然后给出错误"客户端已经登录"否则,如果他退出,只需更新注销字段 感谢

这是我的计划1

的图片

2 个答案:

答案 0 :(得分:0)

您应该插入 DateTime 值:

cmd.Parameters.AddWithValue("@p2", DateTime.Parse(txtDateTime.Text))
cmd.Parameters.AddWithValue("@p3", DateTime.Today)

答案 1 :(得分:0)

更新数据库表(LogSheet)的结构以包含以下字段。 ClientNameSignInTimeSignInRelationSignOutTimeSignOutRelationCurrentDate

如果客户已登录,那么今天的SignInTime(CurrentDate)将有一个值,您可以填充SignOutTime的值。

这是一种可以编码的方法:

此代码未经测试,但您应该明白这一点。

  Sub BtnSignIn_Click()
    If IsUserLoggedIn(txtClientName.Text) = True Then
        'Client has been logged in already. Do Action.
    Else

        'Client has not been logged in today. Do Action.
    End If
End Sub


Function IsUserLoggedIn(UserName As String) As Boolean
    Dim sql = "Select SignOutTime From LogSheet Where CurrentDate=@D AND ClientName=@C"

    Try
        Using cnn As New OleDbConnection
            Using cmd As New OleDbCommand(sql, cnn)
                cnn.Open()
                cmd.Parameters.AddWithValue("@D", Today.Date.ToShortDateString)
                cmd.Parameters.AddWithValue("@C", UserName)
                Dim result = cmd.ExecuteScalar

                If result Is Nothing OrElse result Is DBNull.Value OrElse String.IsNullOrEmpty(result.ToString) Then
                    Return False
                Else
                    Return True
                End If

            End Using
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error: " & System.Reflection.MethodBase.GetCurrentMethod.Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Function