Jump to content



Photo

working with forms in VB

vb forms closing opening

  • Please log in to reply
11 replies to this topic

#1 puppet_master

puppet_master

    Neowinian

  • Joined: 06-January 13

Posted 08 February 2013 - 15:35

Hi guys, here is my problem, in vb I got a couple of forms and when I run the main form which has my buttons on it and when I want to click on any of the buttons the relevant form opens exactly how I want it, but on the forms that are open I have put a home button so when it is pressed it closes that page and goes back to main form..........well thats how I want it to work but it doesn't. This is what really happens....when a button is clicked it opens the relevant page which is ExhibitorForm.ShowDialog()and that sits on top of the main just how i want it and when I want to press the home button it closes that form and opens a new main form so I have 2 main forms.

Anybody got any ideas how I can resolve this?


#2 Haggis

Haggis

    Neowinian Senior

  • Tech Issues Solved: 14
  • Joined: 13-June 07
  • Location: Near Stirling, Scotland
  • OS: Debian 7
  • Phone: Samsung Galaxy S3 LTE (i9305)

Posted 08 February 2013 - 15:45

I do this in VBA should be the same

mainform
2ndform

so when you click 2ndform button you want


2ndform.show
mainform.hide

and when you click home you want

2ndform.hide
mainform.show

this will popup the same form

#3 OP puppet_master

puppet_master

    Neowinian

  • Joined: 06-January 13

Posted 08 February 2013 - 16:09

I've done that and it puts the blue squiggle line under it and so I put ME.hide() and it closed it and done the same thing open another main, not sure if i am doing something right/wrong in the main to do this.

here is my code for my main form....

Public Class Mainmenu

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ExhibitorForm As New ExhibitorForm
ExhibitorForm.ShowDialog()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim visitosForm As New VisitorsForm
VisitorsForm.ShowDialog()


End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim PermanentStaff As New PermanentStaff
PermanentStaff.ShowDialog()





End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim AgencyStaff As New AgencySatff
AgencyStaff.ShowDialog()

End Sub
End Class

#4 Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 08 February 2013 - 18:00

Can you please press "code" when pasting code. I don't read code without the pretty colors!

#5 Hardcore Til I Die

Hardcore Til I Die

    Neowinian Senior

  • Joined: 18-February 07
  • Location: England

Posted 08 February 2013 - 18:07

Just because you open a second form doesn't mean the main one has closed, so you don't actually have to do anything when you want to return to the first form - just close the second one.

Example:

You have "Form 1" open. You click "Button 1" on "Form 1" and this opens "Form 2." You click "Home" on "Form 2," which closes "Form 2." "Form 1" will still be open, so you need not do anything else.

#6 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 08 February 2013 - 20:10

Can you please press "code" when pasting code. I don't read code without the pretty colors!

Fixed that for you :)

Public Class Mainmenu

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim ExhibitorForm As New ExhibitorForm
		ExhibitorForm.ShowDialog()
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim visitosForm As New VisitorsForm
		VisitorsForm.ShowDialog()
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Dim PermanentStaff As New PermanentStaff
		PermanentStaff.ShowDialog()
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		Dim AgencyStaff As New AgencySatff
		AgencyStaff.ShowDialog()
	End Sub

End Class

I suppose the reason why you're seeing a new main form when you click the "Home" button on your child forms (which code you haven't posted), is because the handler for that button is something like:

Dim MainForm As New MainForm
MainForm.Show()

Obviously, this creates and shows a brand new main form, hence the observed behavior. What you could simply do in that handler is not create any form and close the current form, i.e. call Close(). This will let the parent form show once again.

#7 Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 08 February 2013 - 21:14

Fixed that for you :)

Public Class Mainmenu

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim ExhibitorForm As New ExhibitorForm
		ExhibitorForm.ShowDialog()
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim visitosForm As New VisitorsForm
		VisitorsForm.ShowDialog()
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Dim PermanentStaff As New PermanentStaff
		PermanentStaff.ShowDialog()
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		Dim AgencyStaff As New AgencySatff
		AgencyStaff.ShowDialog()
	End Sub

End Class

I suppose the reason why you're seeing a new main form when you click the "Home" button on your child forms (which code you haven't posted), is because the handler for that button is something like:

Dim MainForm As New MainForm
MainForm.Show()

Obviously, this creates and shows a brand new main form, hence the observed behavior. What you could simply do in that handler is not create any form and close the current form, i.e. call Close(). This will let the parent form show once again.


OHHH LOOK AT ALL THE COLORS!

#8 OP puppet_master

puppet_master

    Neowinian

  • Joined: 06-January 13

Posted 09 February 2013 - 14:24

Fixed that for you :)

Public Class Mainmenu

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim ExhibitorForm As New ExhibitorForm
		ExhibitorForm.ShowDialog()
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim visitosForm As New VisitorsForm
		VisitorsForm.ShowDialog()
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Dim PermanentStaff As New PermanentStaff
		PermanentStaff.ShowDialog()
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		Dim AgencyStaff As New AgencySatff
		AgencyStaff.ShowDialog()
	End Sub

End Class

I suppose the reason why you're seeing a new main form when you click the "Home" button on your child forms (which code you haven't posted), is because the handler for that button is something like:

Dim MainForm As New MainForm
MainForm.Show()

Obviously, this creates and shows a brand new main form, hence the observed behavior. What you could simply do in that handler is not create any form and close the current form, i.e. call Close(). This will let the parent form show once again.


Sorry about the CODE thing, will try to do it next time.

Forgot aboutposting the code about the other form so here it is...


Public Class ExhibitorForm


    Private Sub btnhome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhome.Click
        Dim MainMenu As New Mainmenu
        Me.Hide()
        MainMenu.Show()


    End Sub
End Class

I have just changed it to close() and works.

Thanks for the help :D

#9 OP puppet_master

puppet_master

    Neowinian

  • Joined: 06-January 13

Posted 12 February 2013 - 13:49

Hi guys, got another problem, I've put all my code in but it doesn't seem to work

This is the code for my button in the main menu.... once clicked it goes to my ExhibitorForm....Well it did before i put the code in on the ExhibitorForm now it throws up this message The 'Microsoft.ACE.OLEDB.12.0Data Source = Project.accdb' provider is not registered on the local machine. pointing at the con.open()


Public Class Mainmenu

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ExhibitorForm As New ExhibitorForm
        ExhibitorForm.ShowDialog()

    End Sub


here is the code for my ExhibitorForm.....

Public Class ExhibitorForm
    Dim inc As Integer
    Dim MaxRows As Integer
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim cb As New OleDb.OleDbCommandBuilder(da)



    Private Sub ExhibitorForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0"
        dbSource = "Data Source = Project.accdb"

        con.ConnectionString = dbProvider & dbSource
        con.Open()

        sql = "SELECT * FROM tblExhibitorCompanies"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Project")

        MaxRows = ds.Tables("Project").Rows.Count
        inc = -1
    End Sub

    Private Sub navigateRecords()
        TxtCompID.Text = ds.Tables("Project").Rows(inc).Item(0)
        TxtCompName.Text = ds.Tables("Project").Rows(inc).Item(1)
        TxtCompAddress.Text = ds.Tables("Project").Rows(inc).Item(2)
        TxtCompCity.Text = ds.Tables("Project").Rows(inc).Item(3)
        TxtCompPhoneNum.Text = ds.Tables("Project").Rows(inc).Item(4)
        TxtNumbStands.Text = ds.Tables("Project").Rows(inc).Item(5)
        TxtPriceStands.Text = ds.Tables("Project").Rows(inc).Item(6)

    End Sub


    Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevious.Click
        If inc > 0 Then
            inc = inc - 1
            navigateRecords()
        ElseIf inc = -1 Then
            MsgBox("No Records Yet")

        ElseIf inc = 0 Then

            MsgBox("First Record")
        End If
    End Sub

    Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
        If inc <> MaxRows - 1 Then
            inc = inc + 1
        Else
            MsgBox("No More Rows")
        End If
    End Sub

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("Project").Rows(inc).Item(0) = TxtCompID.Text
        ds.Tables("Project").Rows(inc).Item(1) = TxtCompName.Text
        ds.Tables("Project").Rows(inc).Item(2) = TxtCompAddress.Text
        ds.Tables("Project").Rows(inc).Item(3) = TxtCompCity.Text
        ds.Tables("Project").Rows(inc).Item(4) = TxtCompPhoneNum.Text
        ds.Tables("Project").Rows(inc).Item(5) = TxtNumbStands.Text
        ds.Tables("Project").Rows(inc).Item(6) = TxtPriceStands.Text



        da.Update(ds, "AddressBook")

        MsgBox("Data Updated")

        If inc <> -1 Then

        End If

        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("Project").NewRow()

        dsNewRow.Item("CompanyID") = TxtCompID.Text
        dsNewRow.Item("CompanyName") = TxtCompName.Text
        dsNewRow.Item("CompanyAddress") = TxtCompAddress.Text
        dsNewRow.Item("City") = TxtCompCity.Text
        dsNewRow.Item("TelephoneNumber") = TxtCompPhoneNum.Text
        dsNewRow.Item("NumberofStands") = TxtNumbStands.Text
        dsNewRow.Item("PriceofStands") = TxtPriceStands.Text



        ds.Tables("AddressBook").Rows.Add(dsNewRow)

        da.Update(ds, "AddressBook")

        MsgBox("New Record added to the Database")

        btnsave.Enabled = False
        btnadd.Enabled = True
        btnedit.Enabled = True
        btndelete.Enabled = True

    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        btnsave.Enabled = True
        btnadd.Enabled = False
        btnedit.Enabled = False
        btndelete.Enabled = False

        TxtCompID.Clear()
        TxtCompName.Clear()
        TxtCompAddress.Clear()
        TxtCompCity.Clear()
        TxtCompPhoneNum.Clear()
        TxtNumbStands.Clear()
        TxtPriceStands.Clear()


    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)

        If MessageBox.Show("Please confirm you want to Delete this Record?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
            MsgBox("Operation Canceled")
            Exit Sub

        End If
        ds.Tables("Project").Rows(inc).Delete()
        MaxRows = MaxRows - 1
        inc = 0
        navigateRecords()
        da.Update(ds, "Project")
    End Sub
End Class




Any Ideas guys?

#10 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 3
  • Joined: 28-July 08
  • Location: SandBox

Posted 13 February 2013 - 12:46

Hi guys, got another problem, I've put all my code in but it doesn't seem to work

This is the code for my button in the main menu.... once clicked it goes to my ExhibitorForm....Well it did before i put the code in on the ExhibitorForm now it throws up this message The 'Microsoft.ACE.OLEDB.12.0Data Source = Project.accdb' provider is not registered on the local machine. pointing at the con.open()


Public Class Mainmenu

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim ExhibitorForm As New ExhibitorForm
		ExhibitorForm.ShowDialog()

	End Sub


here is the code for my ExhibitorForm.....
 Private Sub ExhibitorForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0"
		dbSource = "Data Source = Project.accdb"

		con.ConnectionString = dbProvider & dbSource
		con.Open()

Any Ideas guys?

Yes... Quite simply when you're combining the strings the '12.0' and the 'Data' are being combined into '12.0Data' because you're neglecting a required separator ';'.

#11 OP puppet_master

puppet_master

    Neowinian

  • Joined: 06-January 13

Posted 13 February 2013 - 22:24

I dont know what you mean......do you mean i should write it as ..........ACE.OLEDB.12.0DATA?

#12 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 3
  • Joined: 28-July 08
  • Location: SandBox

Posted 14 February 2013 - 09:02

I dont know what you mean......do you mean i should write it as ..........ACE.OLEDB.12.0DATA?


No I mean you're missing the ';' separator between the provider and data source declarations when you combine them...
' as you can see below you have two variables one for provider, one for source  '
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0"
dbSource = "Data Source = Project.accdb"

'here you are combining them into a connection string, with the ; separator, you end up with an invalid connection string'
con.ConnectionString = dbProvider & dbSource
'Simply add ; to the end of the provider string as such'
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"