hypz Posted June 13, 2012 Report Share Posted June 13, 2012 (edited) Boas, alguém me pode dizer como faço esta query sem dar erro na sintaxe ? Não sei se devo usar apenas "&" para junta-las ou usar mesmo outra função sem ser o "And" Dim sql As String = "Update Carregamento set Valor_Base=" & valor_base & "And Cod_Cliente=" & cod_cliente & "And Valor_IVA=" & valor_iva & "And Valor_Total=" & valor_total Edited June 13, 2012 by Caça GeSHi Link to comment Share on other sites More sharing options...
jpaulino Posted June 13, 2012 Report Share Posted June 13, 2012 Repara na concatenação que estás a fazer, no excerto: ... valor_base & "And Cod_Cliente=" & cod_cliente & "And ... se a variável valor_base for 500 e o cod_cliente 50, o resultador será: 500And Cod_Cliente=50And O que dará obviamente erro! Tens de dar uns espaços: Dim sql As String = "Update Carregamento set Valor_Base=" & valor_base & " And Cod_Cliente=" & cod_cliente & " And Valor_IVA=" & valor_iva & " And Valor_Total=" & valor_total Link to comment Share on other sites More sharing options...
petvetbr Posted June 13, 2012 Report Share Posted June 13, 2012 (edited) Procure verificar se sintaxe do comando Update que esta utilizando é compatível com o seu banco de dados. Isto pode ser feito rodando o comando separadamente direto no banco de dados. Edited June 13, 2012 by petvetbr Fernando Lage Bastos - MCP/MCTS/MCPD Link to comment Share on other sites More sharing options...
jpaulino Posted June 13, 2012 Report Share Posted June 13, 2012 Procure estudar como escrever o Update em SQL. Algumas coisas que notei: Nem reparei que era um UPDATE 😛 (só olhei para os espaços) Link to comment Share on other sites More sharing options...
hypz Posted June 13, 2012 Author Report Share Posted June 13, 2012 (edited) Pois, eu não sei se a query está correta mas ele está na mesma a fazer-me as contas certas, só não está é a executar direito a query. Continua a dar-me erro na query 🙂 Edited June 13, 2012 by hypz Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 Tenta assim Dim sql As String = "Update Carregamento set Valor_Base=" & valor_base & ", Cod_Cliente=" & cod_cliente & ", Valor_IVA=" & valor_iva & ", Valor_Total=" & valor_total & " WHERE IDCarregamento = Teu_Carregamento Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 14, 2012 Author Report Share Posted June 14, 2012 O que é isso do IDCarregamento = TeuCarregamento ? Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 É só para actualizar o carregamento em que estás a trabalhar, caso contrario vai actualizar a tabela toda com os mesmos dados. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 14, 2012 Author Report Share Posted June 14, 2012 Dá-me erro é neste update "Dim sql As String = "Update Utilizador set Montante_Disponivel=" & valor_total & "WHERE Cod_Cartao =" & cod_cliente" mas eu uso este query noutra form e ele funciona perfeitamente. Eu aqui estou a inserir dados numa tabela chamada "Carregamento" e depois quero atualizar a tabela "Utilizador" mal ele calcule os valores e insira lá, é possivel ? Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 Para inserir dados a instrução utilizada é a INSERT. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 14, 2012 Author Report Share Posted June 14, 2012 (edited) Sim, isso eu sei, pera. TextBox2.Text = TextBox3.Text * 0.23 TextBox1.Text = TextBox3.Text - TextBox2.Text TextBox2.Visible = True TextBox1.Visible = True Label1.Visible = True Label2.Visible = True Dim dr As DialogResult dr = MsgBox("Deseja Adicionar ?", MessageBoxButtons.YesNo, "Adicionar Carregamento") If dr = DialogResult.Yes Then Dim provider As String provider = "provider = Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\hypz\Ambiente de trabalho\pap_teste.accdb; persist security info = false;" Dim con As New OleDbConnection(provider) con.Open() Dim query As String = "INSERT INTO Carregamento([Cod_Recibo], [Cod_Cliente], [Nome_Cliente], [Data_Carregamento], [Valor_Base], [Valor_IVA], [Valor_Total]) VALUES (@Cod_Recibo, @Cod_Cliente, @Nome_Cliente, @Data_Carregamento, @Valor_Base, @Valor_IVA, @Valor_Total)" Dim sql As String = "Update Utilizador set Montante_Disponivel=" & valor_total & "WHERE Cod_Cartao =" & cod_cliente Dim sqlcom As New OleDbCommand(sql, con) Dim nr As Integer nr = sqlcom.ExecuteNonQuery() Dim command As New OleDbCommand(query, con) command.Parameters.Add("@Cod_Recibo", OleDbType.VarChar).Value = TextBox5.Text command.Parameters.Add("@Cod_Cliente", OleDbType.VarChar).Value = TextBox4.Text command.Parameters.Add("@Nome_Cliente", OleDbType.VarChar).Value = TextBox6.Text command.Parameters.Add("@Data_Carregamento", OleDbType.DBDate).Value = DateTimePicker1.MaxDate command.Parameters.Add("@Valor_Base", OleDbType.VarChar).Value = TextBox1.Text command.Parameters.Add("@Valor_IVA", OleDbType.VarChar).Value = TextBox2.Text command.Parameters.Add("@Valor_Total", OleDbType.VarChar).Value = TextBox3.Text Dim x As Integer = command.ExecuteNonQuery() If x < 1 Then MessageBox.Show("Erro ao inserir", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error) Else MessageBox.Show("Registo inserido com sucesso!", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information) Dim sql1 As String = "Select * from Carregamento" Dim da As OleDbDataAdapter da = New OleDbDataAdapter(sql1, con) Dim ds As DataSet ds = New DataSet() da.Fill(ds) End If con.Close() command = Nothing ElseIf dr = DialogResult.No Then MsgBox("Carregamento Não Adicionado.") End If Dá-me erro no Update do Utilizador. Edited June 14, 2012 by Caça Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 Que update? Não vejo ai update nenhum.. Uma coisa de cada vez, agora estamos com o Carregamento, ok? Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 14, 2012 Author Report Share Posted June 14, 2012 O Carregamento funciona com o inserir, ele insere. mas nisto Dim sql As String = "Update Utilizador set Montante_Disponivel=" & valor_total & "WHERE Cod_Cartao =" & cod_cliente Dim sqlcom As New OleDbCommand(sql, con) Dim nr As Integer nr = sqlcom.ExecuteNonQuery() Ele dá-me erro na sintaxe da query. Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 Uma coisa de cada vez, estávamos a ver isto Dim sql As String = "Update Carregamento set Valor_Base=" & valor_base & ", Cod_Cliente=" & cod_cliente & ", Valor_IVA=" & valor_iva & ", Valor_Total=" & valor_total & " WHERE IDCarregamento = Teu_Carregamento Já está resolvido? Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 14, 2012 Author Report Share Posted June 14, 2012 Sim, isso está. Agora o outro update é que não 🙂 Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 Tens que dar espaços Dim sql As String = "Update Utilizador set Montante_Disponivel = " & valor_total & " WHERE Cod_Cartao = " & cod_cliente Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 14, 2012 Author Report Share Posted June 14, 2012 (edited) Senão tiver espaços é aquele problema do tipo "CodCartao=1codcliente=2" e ele não consegue ler certo? Continua a dar-me erro na sintaxe da query update.. Edited June 14, 2012 by hypz Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 Certo, tenta assim Dim sql As String = "Update [utilizador] set [Montante_Disponivel] = " & valor_total & " WHERE [Cod_Cartao] = " & cod_cliente Se continuar a dar erro pega na query final e executa-a directamente no Access Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 14, 2012 Author Report Share Posted June 14, 2012 Será que tem haver com a posição da query ? Ser logo depois do Insert ? Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 Report Share Posted June 14, 2012 Não, não tem nada a haver, executa-a no Access. O que é que vai nas variáveis valor_total e cod_cliente? Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now