Solved

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

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

Technology Partners: 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!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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