Solved

How to make this into a function, and then run it in a button

Posted on 2007-11-20
5
137 Views
Last Modified: 2010-04-23
Im noob on functions, and need some help

I need my code to be made into a function, and then i need the ability to call this function inside any buttons i have

the part of the code that will be changed for every button is the "setning" variable
Dim forbindelse As New System.Data.OracleClient.OracleConnection
 

        forbindelse.ConnectionString = "user id=****;password=****;data source=oracle.*******"

        forbindelse.Open()
 

        Dim setning As String = "SELECT querys - changing on every button"
 
 

        Dim kommando As New System.Data.OracleClient.OracleCommand(setning, forbindelse)
 
 

        Dim dataleser As OracleClient.OracleDataReader

        dataleser = kommando.ExecuteReader()
 

        Me.Dgvstatistikk.DataSource = dataleser ''bind the results of the query to the listbox, make sure you have the display and value members of the listbox set to the correct column names in the query

        dataleser.Close()

Open in new window

0
Comment
Question by:leeds2000
  • 2
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 250 total points
ID: 20318409
You should create a sub rather than function as you dont want to return anything. And on any button click just say like below

Call MySub("SELECT querys - changing on every button")

Thats it.
Public Sub MySub (ByVal setning As String)

	Dim forbindelse As New System.Data.OracleClient.OracleConnection

 

        forbindelse.ConnectionString = "user id=****;password=****;data source=oracle.*******"

        forbindelse.Open()

 

        'Dim setning As String = "SELECT querys - changing on every button"

 

        Dim kommando As New System.Data.OracleClient.OracleCommand(setning, forbindelse)

 

 

        Dim dataleser As OracleClient.OracleDataReader

        dataleser = kommando.ExecuteReader()

 

        Me.Dgvstatistikk.DataSource = dataleser ''bind the results of the query to the listbox, make sure you have the display and value members of the listbox set to the correct column names in the query

        dataleser.Close()

End Sub

Open in new window

0
 
LVL 3

Author Comment

by:leeds2000
ID: 20318472
Ah, smart ;)

will test is as soon as my database is up and running again :)
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 250 total points
ID: 20318474
Just a small add to asvforce post (that is correct!). If you like to use a function to return a result you can do it using a boolean and improve your code handling.

Public Function MySub (ByVal setning As String) As Boolean
 

        MySub = True

       

     Try
 

        Dim forbindelse As New System.Data.OracleClient.OracleConnection

 

        forbindelse.ConnectionString = "user id=****;password=****;data source=oracle.*******"

        forbindelse.Open()

 

        'Dim setning As String = "SELECT querys - changing on every button"

 

        Dim kommando As New System.Data.OracleClient.OracleCommand(setning, forbindelse)

 

         Dim dataleser As OracleClient.OracleDataReader

        dataleser = kommando.ExecuteReader()

 

        Me.Dgvstatistikk.DataSource = dataleser ''bind the results of the query to the listbox, make sure you have the display and value members of the listbox set to the correct column names in the query

        dataleser.Close()
 

     Catch ex As Exception

        MySub = False

     End Try
 

End Function
 
 

' Then use it this way

If MySub("SELECT querys - changing on every button") Then

   Debug.Print "Code run it fine"

Else

   Debug.Print "Code as generate an error"

End If

Open in new window

0
 
LVL 3

Author Comment

by:leeds2000
ID: 20318487
Oki, 2 great suggestions here :), i guess these will work in any way, so i will split the points

Becouse asvforce had a good sub suggestion, and jpaulino had a good function suggestion :)

Thanks guys
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20318536
thanks leeds2000!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now