sábado, 25 de maio de 2013

on

Imports System.Data.OleDb

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dados\Cadastro.mdb")
        Dim com As OleDbCommand = Nothing
        Dim queryResult As Integer

        con.Open()
        com = New OleDbCommand("SELECT COUNT(*) FROM clientes WHERE Nome ='" & Me.TextBox1.Text & "'", con)
        queryResult = com.ExecuteScalar()
        con.Close()

        If queryResult = 0 Then
            con.Open()
            com = New OleDbCommand("INSERT INTO clientes (Nome) VALUES (@Nome)", con)
            com.Parameters.Add(New OleDbParameter("@Nome", SqlDbType.VarChar)).Value = Me.TextBox1.Text
            com.ExecuteNonQuery()
            con.Close()
            MsgBox("OK")
        Else
            MsgBox("Este registo já existe")
        End If
    End Sub
End Class

Espero ter ajudado... Abraços!


0 comentários:

Postar um comentário