• 0

working with forms in VB


Question

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?

Link to comment
https://www.neowin.net/forum/topic/1135466-working-with-forms-in-vb/
Share on other sites

11 answers to this question

Recommended Posts

  • 0

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

  • 0

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

  • 0

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.

  • 0

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 [/CODE]

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:

[CODE]
Dim MainForm As New MainForm
MainForm.Show()
[/CODE]

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.

  • 0

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 [/CODE]

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:

[CODE]
Dim MainForm As New MainForm
MainForm.Show()
[/CODE]

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!

  • 0

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 [/CODE]

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:

[CODE]
Dim MainForm As New MainForm
MainForm.Show()
[/CODE]

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

[CODE]

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
[/CODE]

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

Thanks for the help :D

  • 0

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
[/CODE]

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

[CODE]

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

[/CODE]

Any Ideas guys?

  • 0

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
[/CODE]

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

[CODE]
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()
[/CODE]

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 ';'.

  • 0

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;"
[/CODE]

This topic is now closed to further replies.
  • Posts

    • I disagree; they come off very "bitchy" and "whiny". Make a great product and combine that with a great price (free) and people will come over to your side. Or build it and they will come as they say. Constantly trying to get attention by complaining all the time, will turn people off to your product.
    • It use to be a nightmare, with LibreOffice supporting a newer draft ODF standard by default, and Microsoft Office supporting the older non-draft standard. Now that they both support the same version of ODF, they should be interoperable.
    • Brave Browser 1.91.171 by Razvan Serea Brave Browser is a lightning-fast, secure web browser that stands out from the competition with its focus on privacy, security, and speed. With features like HTTPS Everywhere and built-in tracker blocking, Brave keeps your online activities safe from prying eyes. Brave is one of the safest browsers on the market today. It blocks third-party data storage. It protects from browser fingerprinting. And it does all this by default. Speed - Brave is built on Chromium, the same technology that powers Google Chrome, and is optimized for speed, providing a fast and responsive browsing experience. Brave Browser also features Brave Rewards, a system that rewards users with Basic Attention Tokens (BAT) for viewing opt-in ads. This innovative system provides an alternative revenue model for content creators and a way to support the Brave community. SlimBrave Neo takes all the good things about Brave and makes them even better by keeping everything clean, light, and privacy-focused. It removes the extra clutter, turns off features you might not need, and cuts down on anything that could slow you down or collect unnecessary data. Because it relies on simple settings and policies instead of modifying the browser itself, you still get full Brave compatibility—just in a smoother, lighter, and more privacy-friendly package. Brave Browser 1.91.171 changelog: General Fixed Cardano not being disabled on upgrade to Brave Origin. Upgraded Chromium to 149.0.7827.103. Origin Removed “Survey Panelist” setting from brave://settings/privacy. Fixed P3A and usage ping under brave://settings/privacy being displayed on first launch on Linux. Upgraded Chromium to 149.0.7827.103. Download: Brave Browser 64-bit | 1.2 MB (Freeware) Download: Brave Browser 32-bit View: Brave Homepage | Offline Installers | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Hi. As the title suggests, I can't access the forum on my phone. I'm using Edge on Android and when I try to navigate to the forum I get a "we value your privacy" popup and none of the buttons are clickable. It effectively stonewalls me from reading any forum content.
    • Honestly you're not wrong about AdGuard. Neowin frequently has lifetime license discounts for them and that's how I got my cheap family license a few years ago to run it on all my devices.
  • Recent Achievements

    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      PsYcHoKiLLa
      190
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!