Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Database and Reporting > How to sumit a query from a different form window


Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2012, 05:12 PM
Anthei Anthei is offline
Newcomer
 
Join Date: May 2012
Posts: 1
Default How to sumit a query from a different form window


I'm creating an inventory application. It has a combo box for rooms, and I'm trying to code the "Display" button. I got it to open up the database in a new window, but I'm trying to get it to come up and search the database with the cmbRooms.Text information and compare it to the room data in the records. I'm wondering if that's even possible? If so, am I able to do it from the Main Form, or do I have to modify the code in the Database form instead, or in addition to the Main Form's code?

Here's the Main Form's Display button code so far. When I run the application and select a room, and click "Display", it comes up with an error on this line:
Call frmDatabase.Show(record)
I've tried to get it to work, but I can't find an example or help on how to do it anywhere!

Code:
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'displays the Database window with a search query of the specified room

        'Declarations
        Dim frmDatabase As New Database()
        Dim query As String
        query = "SELECT * FROM inventory WHERE room LIKE '%'" & cmbRooms.Text & "'%'"

            'If textbox for cmbRooms is occupied, and holds the value "All"
            'Then show the whole database
        If cmbRooms.Text = "All" Then
            frmDatabase.Show()
        ElseIf cmbRooms.Text > "0" Then
            'if textbox occupied, search database using query
            frmDatabase.Show(query)
        Else
            'if textbox empty, view error
            MessageBox.Show("Please select a room or type 'All' in the Room text field to view all entries.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End If

    End Sub


Here's the code for my database form:

Code:
Public Class Database

    Private Sub InventoryBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InventoryBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.InventoryBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.InventoryDataSet)

    End Sub

    Private Sub Database_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'InventoryDataSet.Inventory' table. You can move, or remove it, as needed.
        Me.InventoryTableAdapter.Fill(Me.InventoryDataSet.Inventory)

    End Sub
End Class
EDIT: I realized only now that I put this in the wrong area, as this is .NET, not 6 like I thought it was. I apologize.

Last edited by Anthei; 05-12-2012 at 05:42 PM.
Reply With Quote
  #2  
Old 05-14-2012, 07:08 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,278
Default

Firstly, you can't pass random stuff to the Form's Show method so that's not the way to go.

What you should probably be doing is using the Form's Constructor to pass the relevant information to the Database form.

The big question is what to send to the Database form? The best answer to this question would depend on how you might want to extend this further, but a simple example would pass the SQL Query that the database form has to run. That way you could have many other uses for the database form and you just have to send it the query to run. Also, all the Database form has to do is run the query that it is given and display the results.

Firstly there's the constructor for the Database form. Constructors are the Sub New for a class. Forms are a bit different that other classes in that a Form's Sub New must have InitializeComponent called in it. If you don't have InitializeComponent in a Form's Sub New then the IDE will moan about an error and won't compile the project.

So, The Database Form's class now includes...
Code:
Public Class Database
    
    ' A private variable to store the query in
    private queryToRun as string

    Public Sub New(byval query as String)
        ' This call is required by the Windows Form Designer.
        InitializeComponent

        ' Save the query to run
        me.queryToRun = query
    End Sub

End Class
Then, in the Form's load event, you'll use the query to set up a Command to run and display.

Using the Database class that now has a constructor, the following line in your code won't work...
Code:
 Dim frmDatabase As New Database()
... because you can't create a database form without passing it a query to run (i.e. there is no Sub New without any parameters). That's a good thing because in the Database form we don't have to worry about whether a query has been passed or not. It must have been because the form couldn't have been created without one.

So, the code to use it would now look something like...
Code:
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'displays the Database window with a search query of the specified room

        'Declarations
        ' No New here now in the declaration of the form. It will be created later when the query is known 
        Dim frmDatabase As Database()

            'If textbox for cmbRooms is occupied, and holds the value "All"
            'Then show the whole database
        If cmbRooms.Text = "All" Then
            frmDatabase = New Database("SELECT * FROM inventory")
            frmDatabase.Show
        ElseIf cmbRooms.Text > "0" Then
            frmDatabase = New Database("SELECT * FROM inventory WHERE room LIKE '%'" & cmbRooms.Text & "'%'")
            frmDatabase.Show
        Else
            'if textbox empty, view error
            MessageBox.Show("Please select a room or type 'All' in the Room text field to view all entries.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End If

    End Sub
__________________
There are no computers in heaven!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->