7 Replies - 269 Views - Last Post: 21 May 2019 - 05:36 AM Rate Topic: -----

#1 pigkrosano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-May 19

Trying to add data to the database from another form.

Posted 20 May 2019 - 01:14 PM

This is button to add information..this will pop up another form.
Private Sub CmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click
        Dim retVal As Boolean
        retVal = addEdit(True)



This is a fuction bc I have two button that will make the user to go to the same form.
 Function addEdit(q As crudOptions) As Boolean
        Dim f As New addEditPessoa

        Select Case q
            Case crudOptions.addNew
                f.lblID.Text = ""
            Case crudOptions.edit
                f.lblID.Text = lblID.Text
            Case crudOptions.delete

        End Select

        'f.cmdPrepall()

        f.ShowDialog()
        If f.optOk.Checked Then
            cmdPrepall.PerformClick()

        End If
        ' f.Close()
        Return True

    End Function



And this is a module that I have
Public Enum crudOptions
    addNew = 1
    edit = 2
    delete = 3

End Enum
Module trick

    Public Function getRS(ByVal sql As String, rs As ADODB.Recordset, ByVal RO As Boolean, ByRef sErro As String) As Boolean
        Dim Con As New ADODB.Connection

        On Error Resume Next
        Con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Doty\Desktop\Teste\testePessoa2.mdb;Persist Security Info=true")
        rs.Open(sql, Con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
        If Err.Number = 0 Then
            Return True
        Else

            sErro = Err.Description
            Return False

        End If
        On Error GoTo 0
        'Con.Close()

    End Function



My problem, is that the new form pops out. I type the information I want, and then when I click the button ok to add data to database, it does not save the information to the database. Could anyone help me please? Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to add data to the database from another form.

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15116
  • View blog
  • Posts: 60,494
  • Joined: 12-June 08

Re: Trying to add data to the database from another form.

Posted 20 May 2019 - 01:19 PM

I am not following. Is your function for deleting in one form and you are trying to call it from another? If that is the case why not make the function in it's own utility class that either can call?

I spy a 'goto' in there.. do not have those. Remove them. They are bad programming practice.
Was This Post Helpful? 0
  • +
  • -

#3 pigkrosano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-May 19

Re: Trying to add data to the database from another form.

Posted 20 May 2019 - 01:33 PM

The function is to add or edit some information. The new form which will pop out is with textboxes to fill. I forgot to put the other button in the new form to confirm the information.

And why is not good to use "goto"

Thanks

Here is the code in the second form

Private Sub CmdOk_Click(sender As Object, e As EventArgs) Handles cmdOk.Click
        Dim rs As New ADODB.Recordset
        Dim s As String = "Select * from table1 where ID = '" & Trim(lblID.Text) & "'"
        Dim sErro As String = ""
        Dim retVal As Boolean
        retVal = getRS(s, rs, False, sErro)
        If retVal Then
            If Trim(lblID.Text) = "" Then
                rs.AddNew()

            End If
            rs.Fields("Nome").Value = txtNome.Text
            rs.Fields("Sobrenome").Value = txtSobrenome.Text
            rs.Fields("Data de nascimento").Value = txtDDN.Text
            rs.Update()
            Close()

        End If
        rs = Nothing
        optOk.Checked = True
        Me.Hide()
    End Sub


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15116
  • View blog
  • Posts: 60,494
  • Joined: 12-June 08

Re: Trying to add data to the database from another form.

Posted 20 May 2019 - 01:43 PM

I am not sure if what was said was to indicate the issue is resolved or not.

Quote

And why is not good to use "goto"

Chiefly - spaghetti code. Code that jumps willy-nilly all around breaking normal function and scope life cycles. It makes it hard to read, difficult to debug, and spits in the eye of how functions and methods should operate.
Was This Post Helpful? 0
  • +
  • -

#5 pigkrosano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-May 19

Re: Trying to add data to the database from another form.

Posted 20 May 2019 - 01:46 PM

Not resolved yet. It is not saving to database the information typed in the textbox once i hit the CmdOk, which was supposed to save it.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15116
  • View blog
  • Posts: 60,494
  • Joined: 12-June 08

Re: Trying to add data to the database from another form.

Posted 20 May 2019 - 01:51 PM

Sorry - the code you provided does not provide a coherent picture of which bit is from which form or how things are going.
Was This Post Helpful? 0
  • +
  • -

#7 pigkrosano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-May 19

Re: Trying to add data to the database from another form.

Posted 20 May 2019 - 02:03 PM

The first 2 are from the main form, the third is from a module, and the last one is from the second form. Maybe still not clear? Sorry
Was This Post Helpful? 0
  • +
  • -

#8 DarenR   User is offline

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,113
  • Joined: 12-January 10

Re: Trying to add data to the database from another form.

Posted 21 May 2019 - 05:36 AM

are these winforms, asp.net, mvc, etc?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1