Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

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

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
leeds2000
Asked:
leeds2000
  • 2
  • 2
2 Solutions
 
Ashish PatelCommented:
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
 
leeds2000Author Commented:
Ah, smart ;)

will test is as soon as my database is up and running again :)
0
 
jpaulinoCommented:
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
 
leeds2000Author Commented:
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
 
jpaulinoCommented:
thanks leeds2000!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now