?
Solved

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

Posted on 2007-11-20
5
Medium Priority
?
143 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
[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
  • 2
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 1000 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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 …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
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…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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