Visual Basic 6.0

Asked

Viewed 160 times

0

Hi, I’m new to that language. I am creating a student registration form with SQL server 2008 and connection .ADO. At the time of entering and saving the registration data the sign up button inserted two repeated lines.

Here is the code of the registration button:

'------------Botão CADASTRAR--------------
Private Sub CmbCadastrar_Click()

     SQL = "INSERT INTO alunos(nome,idade,datanasc) VALUES ('" + TextNomeAluno + "', '" + TextIdade + "', '" + TextDataNasc + "')"


If TextNomeAluno = "" Then
    MsgBox "CAMPO NOME VAZIO!!! Por favor, insira os valores corretos", vbExclamation
    TextNomeAluno.SetFocus
 Exit Sub
End If

If TextIdade = "" Then
    MsgBox "CAMPO IDADE VAZIO!!! Por favor, insira os valores corretos", vbExclamation
    TextIdade.SetFocus
 Exit Sub
End If

If TextDataNasc = "" Then
    MsgBox "CAMPO DATA VAZIO!!! Por favor, insira os valores corretos", vbExclamation
    TextDataNasc.SetFocus
 Exit Sub
End If


     Set rs = New ADODB.Recordset
     rs.Open SQL, cn, adOpenKeyset, adLockReadOnly
     MsgBox "DADOS CADASTRADOS COM SUCESSO.", vbInformation

     cn.Execute SQL

End Sub

'-------- Botão LIMPAR LISTA-------------
Private Sub CmbLimpaLista_Click()
    LstvAlunos.ListItems.Clear
End Sub

'-------- Botão LIMPAR------------------
Private Sub CmbLimpar_Click()
    TextNomeAluno.Text = Empty
    TextIdade.Text = Empty
    TextDataNasc.Text = Empty
End Sub

'---------Botão LISTAR-------------------
Private Sub CmbListar_Click()

    Set cn = New ADODB.Connection

    cn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=hcac10;Initial Catalog=db_Escola;Data Source=. "

    Set rs = New ADODB.Recordset

    'SQL = "SELECT  codaluno,nome,idade,CONVERT(VARCHAR(12),datanasc,103) FROM dbo.alunos"

    SQL = "SELECT * FROM alunos"

    rs.Open SQL, cn, adOpenKeyset, adLockReadOnly

Do While Not rs.EOF

    Set item = LstvAlunos.ListItems.Add(, , rs.Fields("codaluno"))

    item.SubItems(1) = rs.Fields("nome")
    item.SubItems(2) = rs.Fields("idade")
    item.SubItems(3) = rs.Fields("datanasc")

    rs.MoveNext

Loop

    rs.Close
    Set rs = Nothing

End Sub
  • Very strange this, there must be something calling your button method twice and caching the information , but you can try to do this, "if(not exists(select * from alunos where nome = '" + TextNomeAluno + "' and idade = '" + TextIdade + "' and datanasc = '" + TextDataNasc + "'))
 INSERT INTO alunos(nome,idade,datanasc) VALUES ('" + TextNomeAluno + "', '" + TextIdade + "', '" + TextDataNasc + "')" and changes to stay after field validations.

  • This giving syntax error

  • age is an int field? if you remove the simple quotes from '" + Textidade + "'

  • Yes it is an int field.

  • How the change turned out and what error it made

  • Expected Expression

Show 1 more comment

1 answer

0

You are inserting twice because there are two instructions in your code that execute the SQL command:

rs.Open SQL, cn, adOpenKeyset, adLockReadOnly

and

cn.Execute SQL

No need to create an object Recordset if your goal is to execute an instruction that does not return records. For this use the command Execute only.

  • ok it worked right

  • I would like to create a function for a button to search for enrollment or student name and as well as return by clicking the button in the search textbox it would fill the fields with the data already registered and so could choose what to do: change, delete for example. I need urgent help I have 2 weeks to deliver this project here at the company if I could get ahead with the help of you I would be grateful.

  • Could be a generic example no problem, I just want to understand how it’s done

  • Its "Cmblistar_click" function already has almost all the necessary code. Just change SQL = "SELECT * FROM students" and add to the end "WHERE matricula = N" (N comes from the survey). Exchange WHILE for IF (since it is only to return one record) and within it direct the rs fields to the appropriate textbox.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.