Visual Basic 6.0

0

Hello, I'm new to this language. I am creating a student enrollment form with SQL Server 2008 and .ADO connection. When inserting and saving the registration data, the register button inserted two repeated lines.

Here is the code for the sign up 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
    
asked by anonymous 16.08.2016 / 15:26

1 answer

0

You are entering twice because there are two statements in your code that execute the SQL command:

rs.Open SQL, cn, adOpenKeyset, adLockReadOnly

and

cn.Execute SQL

It is not necessary to create a Recordset object if its purpose is to execute a statement that does not return records. To do this, use the command Execute only.

    
17.08.2016 / 17:57