Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create a function of this SUB?

Posted on 2007-11-21
8
Medium Priority
?
207 Views
Last Modified: 2010-04-23
I have a SUB like the code below

I want this SUB to be created as a function, becouse i need the listview to have diffrent columns on each button i press

And i need the string "setning" to be a new SQL query for each button

How do i create a function of this, and then call this function in each button?
Public Sub statistikken(ByVal setning As String)
 
        Dim forbindelse As New System.Data.OracleClient.OracleConnection
        'Dim lststatistikk As ListViewItem
 
        forbindelse.ConnectionString = "user id=***;password=***;data source=****"
        forbindelse.Open()
 
        Dim kommando As New System.Data.OracleClient.OracleCommand(setning, forbindelse)
 
        Dim dataleser As OracleClient.OracleDataReader
        dataleser = kommando.ExecuteReader()
 
        Me.Dgvstatistikk.DataSource = dataleser
 
 
        Dim lstItem As ListViewItem
        While dataleser.Read()
            lstItem = New ListViewItem()
            lstItem.Text = dataleser.Item("column 0")
            lstItem.SubItems.Add(dataleser.Item("column1"))
            ListView1.Items.Add(lstItem)
        End While
 
        forbindelse.Close()
        dataleser.Close()
        dataleser = Nothing
 
    End Sub

Open in new window

0
Comment
Question by:leeds2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 10

Expert Comment

by:vadim63
ID: 20331334
What this function supposed to return?
0
 
LVL 3

Author Comment

by:leeds2000
ID: 20331345
I need diffrent listview columns on each button, and i need a diffrent SQL query on each button
0
 
LVL 10

Expert Comment

by:vadim63
ID: 20331392
The "function" itself have to return some value. That's what I'm asking. What value?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:SZachmann
ID: 20331414
I am not sure exactly what you are refering to but it appears that you want to be able to add items to a list box that have a dynamic amount of columns based on the query your run.  I have changed the part of your code where you add the items to the list box.
 Dim lstItem As ListViewItem
        While dataleser.Read()
            lstItem = New ListViewItem()
            For col As Integer = 0 To dataleser.FieldCount - 1
                If col = 0 Then
                    lstItem.Text = dataleser.Item(col)
                Else
                    lstItem.SubItems.Add(dataleser.Item(col))
                End If
            Next
            ListView1.Items.Add(lstItem)
        End While

Open in new window

0
 
LVL 10

Accepted Solution

by:
vadim63 earned 2000 total points
ID: 20331417
Maybe something like this:
    Private Sub YourButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
        Select Case sender.Name.toLower
            Case "button1"
                statistikken("your connection string for button1 here", "column name for button1 here")
            Case "button2"
                statistikken("your connection string for button2 here", "column name for button2 here")
        End Select
    End Sub
 
    Public Sub statistikken(ByVal setning As String, ByVal col As String)
 
        Dim forbindelse As New System.Data.OracleClient.OracleConnection
        'Dim lststatistikk As ListViewItem
 
        forbindelse.ConnectionString = "user id=***;password=***;data source=****"
        forbindelse.Open()
 
        Dim kommando As New System.Data.OracleClient.OracleCommand(setning, forbindelse)
 
        Dim dataleser As OracleClient.OracleDataReader
        dataleser = kommando.ExecuteReader()
 
        Me.Dgvstatistikk.DataSource = dataleser
 
 
        Dim lstItem As ListViewItem
        While dataleser.Read()
            lstItem = New ListViewItem()
            lstItem.Text = dataleser.Item("column 0")
            lstItem.SubItems.Add(dataleser.Item(col))
            ListView1.Items.Add(lstItem)
        End While
 
        forbindelse.Close()
        dataleser.Close()
        dataleser = Nothing
 
    End Sub

Open in new window

0
 
LVL 3

Author Comment

by:leeds2000
ID: 20331496
Hmm yes, the function need to return values for the SQL query

now the values to put in the listview

a value for "column 0"
a value for "column 1"
a value for "column 2" <-- not needed on some buttons

is this possible?

0
 
LVL 3

Author Comment

by:leeds2000
ID: 20331505
no sorry, the function need these values from the button, values about the columns and values about the SQL query from the button to the function, not from the function to the button.

If you understand?
0
 
LVL 10

Expert Comment

by:vadim63
ID: 20331538
So, as I said, use the "sender" object.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question