INSERT INTO X CAMPOS NULOS
NO ERRO DE sintaxe VC ESTA COM CANPOS DE MAIS OU TEM ALGUNS NOMES DE CAMPOS Digitado errado de uma olhada bem no seu codigo
Private Sub Cmd_SALVAR_Click()
Dim cnncomando As New ADODB.Command
Dim rs_Pronaf As New ADODB.Recordset
With cnncomando
.ActiveConnection = cnnProjetos
.CommandType = adCmdText
.CommandText = " insert into projetos " & _
"(cod, data, cpf, nome, projetista, valor, atividade, municipio, porte)values ( " & _
"'" & Text_COD.Text & "','" & _
"'" & Text_DATA.Text & "','" & _
"'" & Text_CPF.Text & "', '" & _
"'" & Text_NOME.Text & "','" & _
"'" & Combo_PROJ.Text & "','" & _
"'" & Text_VALOR.Text & "','" & _
"'" & Combo_ATIV.Text & "','" & _
"'" & Combo_MUN.Text & "','" & _
"'" & Combo_PORTE.Text & "');"
.Execute
End With
Set rs_Pronaf = Nothing
Set cnncommando = Nothing
Dim vOK As Integer
vOK = MsgBox("Projeto salvo com sucesso", vbOKOnly + vbInformation, "Salvar Projeto")
End Sub
tenta assim garoto.
Dim cnncomando As New ADODB.Command
Dim rs_Pronaf As New ADODB.Recordset
With cnncomando
.ActiveConnection = cnnProjetos
.CommandType = adCmdText
.CommandText = " insert into projetos " & _
"(cod, data, cpf, nome, projetista, valor, atividade, municipio, porte)values ( " & _
"'" & Text_COD.Text & "','" & _
"'" & Text_DATA.Text & "','" & _
"'" & Text_CPF.Text & "', '" & _
"'" & Text_NOME.Text & "','" & _
"'" & Combo_PROJ.Text & "','" & _
"'" & Text_VALOR.Text & "','" & _
"'" & Combo_ATIV.Text & "','" & _
"'" & Combo_MUN.Text & "','" & _
"'" & Combo_PORTE.Text & "');"
.Execute
End With
Set rs_Pronaf = Nothing
Set cnncommando = Nothing
Dim vOK As Integer
vOK = MsgBox("Projeto salvo com sucesso", vbOKOnly + vbInformation, "Salvar Projeto")
End Sub
tenta assim garoto.
Caro amigo, vc fez tres perguntas respondendo a Segunda: "vc deve sempre que abrir um recordset fecha-lo, isso evita a utilização da memoria para coisas que vc nem esta utilizando mais, porem se não fechar não ira dar erro. Terceira: vc pode sim passar um valor que não esta no formulário, para isso basta vc passa-lo entre aspas ex. ...','em analise','....
Quanto ao seu código, retire o espaço do inicio do insert e verifique se na sua tabela o campo "data" é do tipo data, se for ao inves de utilizar aspas utilize # #.
Falow
With cnncomando
.ActiveConnection = cnnProjetos
.CommandType = adCmdText
.CommandText = " insert into projetos " & _
"(cod, data, cpf, nome, projetista, valor, atividade, municipio, porte)values ('" & _
Text_COD.Text & "','" & _
Text_DATA.Text & "','" & _
Text_CPF.Text & "', '" & _
Text_NOME.Text & "','" & _
Combo_PROJ.Text & "','" & _
Text_VALOR.Text & "','" & _
Combo_ATIV.Text & "','" & _
Combo_MUN.Text & "','" & _
Combo_PORTE.Text & "');"
.Execute
End With
Quanto ao seu código, retire o espaço do inicio do insert e verifique se na sua tabela o campo "data" é do tipo data, se for ao inves de utilizar aspas utilize # #.
Falow
With cnncomando
.ActiveConnection = cnnProjetos
.CommandType = adCmdText
.CommandText = " insert into projetos " & _
"(cod, data, cpf, nome, projetista, valor, atividade, municipio, porte)values ('" & _
Text_COD.Text & "','" & _
Text_DATA.Text & "','" & _
Text_CPF.Text & "', '" & _
Text_NOME.Text & "','" & _
Combo_PROJ.Text & "','" & _
Text_VALOR.Text & "','" & _
Combo_ATIV.Text & "','" & _
Combo_MUN.Text & "','" & _
Combo_PORTE.Text & "');"
.Execute
End With
valeu galera, meu trauma é que não possuo o access por isso fica dificil alterar as configurações do BD, pois faço eles usando o Visual Data Manager.
achei interessante colocar um IF para só salvar com o campo preenchido, eu tava errando nisto também porque eu usava a expressão IF de forma errada, e logo após avisar sobre o campo obrigatório o código continuava a ser lido e parava no erro, mas colocando o IF no inicio e após o ELSE ele não continua a ler o código, evitando o erro, vou por o código pra vc's verem como ficou:
achei interessante colocar um IF para só salvar com o campo preenchido, eu tava errando nisto também porque eu usava a expressão IF de forma errada, e logo após avisar sobre o campo obrigatório o código continuava a ser lido e parava no erro, mas colocando o IF no inicio e após o ELSE ele não continua a ler o código, evitando o erro, vou por o código pra vc's verem como ficou:
Private Sub Cmd_SALVAR_Click()
If Text_COD.Text = "" Then
Dim vinfo As Integer
vinfo = MsgBox("Não existe referência de Projeto para gravar dados no Gerenciador de Projetos, verifique.", vbOKOnly + vbCritical, "ATENÇÃO")
Else
If Text_VALOR.Text = "" Then
Dim vcampo As Integer
vcampo = MsgBox("O campo valor do Projeto não pode conter valor nulo.", vbOKOnly + vbInformation, "Campo obrigatório")
Else
Dim cnncomando As New ADODB.Command
Dim rs_Pronaf As New ADODB.Recordset
With cnncomando
.ActiveConnection = cnnProjetos
.CommandType = adCmdText
.CommandText = " select * from projetos "
Set rs_Pronaf = .Execute
End With
With rs_Pronaf
rs_Pronaf.Filter = "cod = " & Text_COD.Text
If .BOF And .EOF Then
With cnncomando
.ActiveConnection = cnnProjetos
.CommandType = adCmdText
.CommandText = " insert into projetos " & _
"(cod, data, cpf, nome, projetista, valor, atividade, municipio, porte)values ('" & _
Text_COD.Text & "','" & _
Text_DATA.Text & "','" & _
Text_CPF.Text & "', '" & _
Text_NOME.Text & "','" & _
Combo_PROJ.Text & "','" & _
Text_VALOR.Text & "','" & _
Combo_ATIV.Text & "','" & _
Combo_MUN.Text & "','" & _
Combo_PORTE.Text & "');"
.Execute
End With
Dim vOK As Integer
vOK = MsgBox("Projeto salvo com sucesso", vbOKOnly + vbInformation, "Salvar Projeto")
Limpar_dados
Else
With cnncomando
.ActiveConnection = cnnProjetos
.CommandType = adCmdText
.CommandText = " update projetos set " & _
"cod = '" & Text_COD.Text & "'," & _
"data = '" & Text_DATA.Text & "'," & _
"cpf = '" & Text_CPF.Text & "', " & _
" nome = '" & Text_NOME.Text & "'," & _
" projetista = '" & Combo_PROJ.Text & "'," & _
" valor = '" & Text_VALOR.Text & "'," & _
" atividade = '" & Combo_ATIV.Text & "'," & _
" municipio = '" & Combo_MUN.Text & "'," & _
" porte = '" & Combo_PORTE.Text & "'" & _
"where cod = " & Text_COD.Text & ";"
.Execute
End With
vOK = MsgBox("Projeto salvo com sucesso", vbOKOnly + vbInformation, "Salvar Projeto")
Limpar_dados
Set rs_Pronaf = Nothing
Set cnncommando = Nothing
End If
End With
End If
End If
End Sub
Opa! beleza, não tinha visto a segunda página, obrigado CEL_SUB7 pela explicação da segunda pergunta e também pela resposta da terceira que acabo de colocar e gravou beleza, logo após do ultimo campo do insert, pus o campo que faltava:
"(cod, data, cpf, nome, projetista, valor, atividade, municipio, porte, situacao)values
e no código pus, no final do insert into, como vc deu a dica:
Combo_PORTE.Text & "','EM ANÃÂÂLISE');"
ficou FILET!
Obrigado a todos e até a proxima, pois, com certeza virá...
"(cod, data, cpf, nome, projetista, valor, atividade, municipio, porte, situacao)values
e no código pus, no final do insert into, como vc deu a dica:
Combo_PORTE.Text & "','EM ANÃÂÂLISE');"
ficou FILET!
Obrigado a todos e até a proxima, pois, com certeza virá...
Tópico encerrado , respostas não são mais permitidas