Solved

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

Posted on 2007-11-20
5
138 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 59
Check if number is currency 15 31
Hide Tab Page 3 34
how to use laptop or pad camera in vb.net windows application 2 55
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

22 Experts available now in Live!

Get 1:1 Help Now