Jump to content

Pesquisar num DB Access


security
 Share

Recommended Posts

Ola.

Espero que alguem me possa ajudar a resolver este grave problema com que estou.

Tenho um formulário que consiste na pesquisa de Alunos ou Professores, em que a pesquisa e feita por nome, e se encontrar o registo preenche as labels com os respectivos dados, como  mostar a imagem

C:\Users\Miguel\Desktop\Nova pasta\Imagem1.png

Acontece que da-me erro :

Dim leitor As OleDb.OleDbDataReader = cmd.ExecuteReader

por favor ajudem-me. Preciso de ter isto pronto para Quarta feira... para apresentar para nota.

Aqui está o código:

Imports System.Data.OleDb
Imports System.Windows.Forms.Application
Public Class frmPesq
    Dim cmd As New OleDb.OleDbCommand
    Dim conn As New OleDbConnection
    Dim guarda As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "\DBCSG.mdb"
        If RadioButton1.Checked Then
            Dim strSQL As String = "select * From alunos where Nome=" & TextBox1.Text
            Dim cmd As OleDb.OleDbCommand = conn.CreateCommand

            cmd.CommandText = strSQL
            conn.Open()
            Dim leitor As OleDb.OleDbDataReader = cmd.ExecuteReader
            leitor.Read()
            Label11.Text = IIf(leitor("cod_int") Is DBNull.Value, "", leitor("cod_int"))
            Label12.Text = IIf(leitor("Nome") Is DBNull.Value, "", leitor("Nome"))
            Label13.Text = IIf(leitor("n_BI") Is DBNull.Value, "", leitor("n_BI"))
            Label14.Text = IIf(leitor("n_fiscal") Is DBNull.Value, "", leitor("n_fiscal"))
            Label19.Text = IIf(leitor("dataNasc") Is DBNull.Value, "", leitor("dataNasc"))
            Label20 = IIf(leitor("sexo") Is DBNull.Value, "", leitor("sexo"))
            Label21.Text = IIf(leitor("cp1") Is DBNull.Value, "", leitor("cp1"))
            Label22.Text = IIf(leitor("cp2") Is DBNull.Value, "", leitor("cp2"))
            Label23.Text = IIf(leitor("morada") Is DBNull.Value, "", leitor("morada"))
            Label24.Text = IIf(leitor("freguesia") Is DBNull.Value, "", leitor("freguesia"))
            Label25.Text = IIf(leitor("concelho") Is DBNull.Value, "", leitor("concelho"))
            Label26.Text = IIf(leitor("distrito") Is DBNull.Value, "", leitor("distrito"))
            Label27.Text = IIf(leitor("nacionalidade") Is DBNull.Value, "", leitor("nacionalidade"))

        ElseIf RadioButton2.Checked Then

            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "\DBCSG.mdb"
            guarda = TextBox1.Text
            Dim strSQL As String = "select * From profs where Nome=" & guarda
            Dim cmd As OleDb.OleDbCommand = conn.CreateCommand

            cmd.CommandText = strSQL
            conn.Open()
            Dim leitor As OleDb.OleDbDataReader = cmd.ExecuteReader()
            MsgBox(guarda)
            leitor.Read()
            Label11.Text = IIf(leitor("cod_int") Is DBNull.Value, "", leitor("cod_int"))
            Label12.Text = IIf(leitor("Nome") Is DBNull.Value, "", leitor("Nome"))
            Label13.Text = IIf(leitor("n_BI") Is DBNull.Value, "", leitor("n_BI"))
            Label14.Text = IIf(leitor("n_fiscal") Is DBNull.Value, "", leitor("n_fiscal"))
            Label19.Text = IIf(leitor("dataNasc") Is DBNull.Value, "", leitor("dataNasc"))
            Label20 = IIf(leitor("sexo") Is DBNull.Value, "", leitor("sexo"))
            Label21.Text = IIf(leitor("cp1") Is DBNull.Value, "", leitor("cp1"))
            Label22.Text = IIf(leitor("cp2") Is DBNull.Value, "", leitor("cp2"))
            Label23.Text = IIf(leitor("morada") Is DBNull.Value, "", leitor("morada"))
            Label24.Text = IIf(leitor("freguesia") Is DBNull.Value, "", leitor("freguesia"))
            Label25.Text = IIf(leitor("concelho") Is DBNull.Value, "", leitor("concelho"))
            Label26.Text = IIf(leitor("distrito") Is DBNull.Value, "", leitor("distrito"))
            Label27.Text = IIf(leitor("nacionalidade") Is DBNull.Value, "", leitor("nacionalidade"))
            conn.Close()
        Else
            MsgBox("Não foi encontrado o registo. Deseja Cria-lo?", MsgBoxStyle.Exclamation, MsgBoxStyle.YesNoCancel)
        End If
    End Sub
End Class

Obrigado  :wallbash:

"Innovation distinguishes between a leader and a follower." Steve jobs.

Link to comment
Share on other sites

o erro que da é: Erro de sintaxe (operador em falta) na expressão de consulta 'Nome=Rui Manuel Casimiro De Santos Sousa''.

O nome Rui Manuel Casimiro De Santos Sousa é o nome pela qual a pesquisa deve ser feita.

O erro da-me na linha que referi no postagem inicial

"Innovation distinguishes between a leader and a follower." Steve jobs.

Link to comment
Share on other sites

uma hipótese é teres de enviar o nome em ' '

Tipo:

Dim strSQL As String = "select * From alunos where Nome='" & TextBox1.Text & "'"

Atenção: não se vê bem, mas tem ' ' junto com as aspas

Experimenta

Oracle Certified Professional - AdministraçãoOracle Certified Professional - Pl/sqlMCPD - Microsoft Certified Professional DeveloperMCTS - Microsoft Certified Technology Specialist

Link to comment
Share on other sites

obrigado... o problema foi resolvido mas surgiu-me um outro erro que apenas diz "cod_int" (é o nome de um campo da minha base de dados),  onde deveria listar os valores nas labels, ou seja no codigo seguinte:

Label11.Text = IIf(leitor("cod_int") Is DBNull.Value, "", leitor("cod_int"))
                Label12.Text = IIf(leitor("Nome") Is DBNull.Value, "", leitor("Nome"))
                Label13.Text = IIf(leitor("n_BI") Is DBNull.Value, "", leitor("n_BI"))
                Label14.Text = IIf(leitor("n_fiscal") Is DBNull.Value, "", leitor("n_fiscal"))
                Label19.Text = IIf(leitor("dataNasc") Is DBNull.Value, "", leitor("dataNasc"))
                Label20 = IIf(leitor("sexo") Is DBNull.Value, "", leitor("sexo"))
                Label21.Text = IIf(leitor("cp1") Is DBNull.Value, "", leitor("cp1"))
                Label22.Text = IIf(leitor("cp2") Is DBNull.Value, "", leitor("cp2"))
                Label23.Text = IIf(leitor("morada") Is DBNull.Value, "", leitor("morada"))
                Label24.Text = IIf(leitor("freguesia") Is DBNull.Value, "", leitor("freguesia"))
                Label25.Text = IIf(leitor("concelho") Is DBNull.Value, "", leitor("concelho"))
                Label26.Text = IIf(leitor("distrito") Is DBNull.Value, "", leitor("distrito"))
                Label27.Text = IIf(leitor("nacionalidade") Is DBNull.Value, "", leitor("nacionalidade"))

"Innovation distinguishes between a leader and a follower." Steve jobs.

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
 Share

×
×
  • 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.