Private Function Gravar() As Boolean
Dim cmdGravar As Command
Dim sSQL As String
On Error GoTo erro
sSQL = "Insert into Cad_Cargo(dsc_cargo) " & _
" values('" & Replace(txtDescricao.Text, "'", "") & "')"
conn1.BeginTrans
Set cmdGravar = New Command
With cmdGravar
.ActiveConnection = conn1
.CommandText = sSQL
.Execute
End With
conn1.CommitTrans
Gravar = True
Exit Function
erro:
MsgBox "Erro de Gravao!" & vbCr & Err.Number & " - " & Err.Description, vbCritical, Me.Caption
conn1.RollbackTrans
End Function
Private Function Excluir() As Boolean
Dim cmdExcluir As Command
Dim sSQL As String
On Error GoTo erro
sSQL = " delete from Cad_Cargo " & _
" where cod_cargo = " & lblCodigo.Caption
Set cmdExcluir = New Command
With cmdExcluir
.ActiveConnection = conn1
.CommandText = sSQL
.Execute
End With
Excluir = True
Exit Function
erro:
If Err.Number = -2147467259 Then
MsgBox "Esse Registro j foi usado em outra parte do sistema!" & vbCr & "No pode ser excluido.", vbCritical, Me.Caption
Else
MsgBox "Erro na Excluso!" & vbCr & Err.Number & " - " & Err.Description, vbCritical, Me.Caption
End If
End Function
Private Function GravarAlteracao() As Boolean
Dim cmdGravar As Command
Dim sSQL As String
On Error GoTo erro
sSQL = "Update Cad_Cargo " & _
" set dsc_cargo = '" & Replace(txtDescricao.Text, "'", "") & "'" & _
" where cod_cargo = " & lblCodigo.Caption
conn1.BeginTrans
Set cmdGravar = New Command
With cmdGravar
.ActiveConnection = conn1
.CommandText = sSQL
.Execute
End With
conn1.CommitTrans
GravarAlteracao = True
Exit Function
erro:
MsgBox "Erro na Alterao!" & vbCr & Err.Number & " - " & Err.Description, vbCritical, Me.Caption
conn1.RollbackTrans
End Function
' Quando vc abre o Projeto adiciona esta função no modgeral e chame a função abaixo que é a conexão com o Banco
Public conn1 As New ADODB.Connection
Sub Abre_Conexao()
On Error GoTo rErro
conn1.ConnectionString = "DSN=ConectoAoBanco;UID=admin;PWD=senha123"
conn1.Open
Exit Sub
rErro:
MsgBox "Problemas na conexo de dados: " & Chr(13) & _
"tente novamente e, caso o problema persista, " & Chr(13) & _
"contate o Fabricante ", 16, "Erro " & Err
End
End Sub
'Obs: Espero ter ajudado.