Jump to content

Não entra em ciclo Where e não faz Insert


AnaGPSI

Recommended Posts

Tenho o seguinte código que não entra dentro do while, alguém me consegue explicar porque?

   Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    Const constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\GPSI\Documents\mydb.accdb"
    Dim mydb As New OleDbConnection(constr)
    mydb.Open()
    'MsgBox("State: " & mydb.State.ToString(),
    'MsgBoxStyle.Information)
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'saber id_momento e atribuir variavel
    Dim cod_momento As Integer
    Dim oledbCnn As OleDbConnection
    Dim oledbCmd As OleDbCommand
    Dim sql As String = "Select id_momento from momento where descri_momento='" & TextBox3.Text & "'"
    oledbCnn = New OleDbConnection(constr)
    Try
	    oledbCnn.Open()
	    oledbCmd = New OleDbCommand(sql, oledbCnn)
	    Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
	    While oledbReader.Read
		    cod_momento = oledbReader.Item(0)
		    MsgBox(cod_momento)
	    End While
    Finally
    End Try
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Atribuir variavel a numero de processo
    Dim numeroprocesso As Integer = TextBox2.Text
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'saber id_qualitativa e atribuir variavel

    Dim abc As Date = Format(datatime.Value.Date, "dd-MM-yyyy")
    MsgBox(abc)
    'Format(datatime.Value, "dd-MM-yyyy")
    Dim cod_qualitativa As Integer
    Dim oledbCnn1 As OleDbConnection
    Dim oledbCmd1 As OleDbCommand
    MsgBox(datatime.Value.Date)
    Dim sql1 As String = "Select id_qualitativa from avaliacao_qualitativa where validade = " & abc & ""
    'validade = " & datatime.Text & """
    'and n_processo=" & numeroprocesso & ""
    'MsgBox(da.Text)
    MsgBox("ola")
    oledbCnn1 = New OleDbConnection(constr)
    Try
	    oledbCnn1.Open()
	    oledbCmd1 = New OleDbCommand(sql1, oledbCnn1)
	    Dim oledbReader1 As OleDbDataReader = oledbCmd1.ExecuteReader()
	    MsgBox("ola1")
	    While oledbReader1.Read
		    MsgBox("ola2")
		    cod_qualitativa = oledbReader1.Item(0)
		    MsgBox(cod_qualitativa)
	    End While
    Finally
    End Try
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim sql3 As String = "Insert into apreciacao_global ([descr_apreci], [id_qualitativa]) values ('" & txtapreglobquali.Text & "', '" & cod_qualitativa & "') where  ((avaliacao_qualitativa.id_qualitativa LIKE '%" & cod_qualitativa & "%'))"
    Dim cmd3 As New OleDbCommand(sql3, mydb)
    cmd3.ExecuteNonQuery()
    MsgBox("Apreciação global introduzida!")

Não entra dentro deste while:

While oledbReader1.Read
		    MsgBox("ola2")
		    cod_qualitativa = oledbReader1.Item(0)
		    MsgBox(cod_qualitativa)
	    End While

alguém me consegue ajudar?

Help me please

Link to comment
Share on other sites

Vamos evitar títulos como "Help" de futuro, pode ser?

Uma boa descrição breve é meio caminho andado para que não seja ignorado em primeiras instâncias.

Quanto ao teu While, se não entra, é porque oledbReader1 não tem quaiseres resultados.

Experimentaste a query directamente no Access?

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Link to comment
Share on other sites

Peço desculpa.

Já agora como altero o nome do tópico?

Sobre esse problema já consegui resolver-me só que agora dá-me o seguinte erro:

A entrada da consulta tem que conter pelo menos uma tabela ou consulta

na linha:

cmd3.ExecuteNonQuery()

do seguinte excerto:

 Dim sql3 As String = "Insert into apreciacao_global ([descr_apreci], [id_qualitativa]) values ('" & txtapreglobquali.Text & "', '" & cod_qualitativa & "') where  ((avaliacao_qualitativa.id_qualitativa LIKE '%" & cod_qualitativa & "%'))"
    Dim cmd3 As New OleDbCommand(sql3, mydb)
    cmd3.ExecuteNonQuery()
    MsgBox("Apreciação global introduzida!")

Com as alterações o código ficou assim:

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
	Const constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\GPSI\Documents\mydb.accdb"
	Dim mydb As New OleDbConnection(constr)
	mydb.Open()
	'MsgBox("State: " & mydb.State.ToString(),
	'MsgBoxStyle.Information)
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'saber id_momento e atribuir variavel
	Dim cod_momento As Integer
	Dim oledbCnn As OleDbConnection
	Dim oledbCmd As OleDbCommand
	Dim sql As String = "Select id_momento from momento where descri_momento='" & TextBox3.Text & "'"
	oledbCnn = New OleDbConnection(constr)
	Try
		oledbCnn.Open()
		oledbCmd = New OleDbCommand(sql, oledbCnn)
		Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
		While oledbReader.Read
			cod_momento = oledbReader.Item(0)
			MsgBox(cod_momento)
		End While
	Finally
	End Try
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'Atribuir variavel a numero de processo
	Dim numeroprocesso As Integer = TextBox2.Text
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'saber id_qualitativa e atribuir variavel

	' Dim abc As Date = Format(datatime.Value.Date, "dd-MM-yyyy")
	Dim abc As String
	abc = Format(DateTimePicker1.Value, "dd-MM-yyyy") & "%"

	'Format(datatime.Value, "dd-MM-yyyy")
	Dim cod_qualitativa As Integer
	Dim oledbCnn1 As OleDbConnection
	Dim oledbCmd1 As OleDbCommand
	'If abc = "2/2/2012" Then
	'	MsgBox("São iguais")
	'Else
	'	MsgBox("Não são iguais")
	'End If
	MsgBox(DateTimePicker1.Value.Date)

	Dim sql1 As String = "Select id_qualitativa from avaliacao_qualitativa where validade like '" & abc & "' and n_processo=" & numeroprocesso & ""
	'validade = " & datatime.Text & """
	'and n_processo=" & numeroprocesso & ""
	'MsgBox(da.Text)
	oledbCnn1 = New OleDbConnection(constr)
	Try
		oledbCnn1.Open()
		oledbCmd1 = New OleDbCommand(sql1, oledbCnn1)
		Dim oledbReader1 As OleDbDataReader = oledbCmd1.ExecuteReader()
		MsgBox("ola1")
		While oledbReader1.Read
			MsgBox("ola2")
			cod_qualitativa = oledbReader1.Item(0)
			MsgBox(cod_qualitativa)
		End While
	Finally
	End Try
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Dim sql3 As String = "Insert into apreciacao_global ([descr_apreci], [id_qualitativa]) values ('" & txtapreglobquali.Text & "', '" & cod_qualitativa & "') where  ((avaliacao_qualitativa.id_qualitativa LIKE '%" & cod_qualitativa & "%'))"
	Dim cmd3 As New OleDbCommand(sql3, mydb)
	cmd3.ExecuteNonQuery()
	MsgBox("Apreciação global introduzida!")
End Sub
Edited by AnaGPSI
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.