By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,777 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,777 IT Pros & Developers. It's quick & easy.

Problem calling one SUB from within another.

P: 8
Hi guys. I'm using VB express 2005 and im having trouble calling one sub function from inside another. Can you help?

I have written an event procedure for a button click. Now i want to call that sub in the ENTER event of a textbox.
I have tried simply copying the name of the sub into the ENTER sub but it wont work, i get the error message "expression expected" and an underline under the "ByVal" part of the argument.


Imports MySql.Data.MySqlClient
Imports System.Data

Public Class frmLogin
Dim conn As MySqlConnection

Private Sub Button_cancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_cancel.Click
Application.Exit() 'kills the application/process
End Sub

Private Sub Button_login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_login.Click
'an example connection string: server=localhost; user id=mike; password=12345; database=in_out
conn = New MySqlConnection
Dim Ser, Use, Pas As String
Ser = TextBox_server.Text & ";"
Use = TextBox_username.Text & ";"
Pas = TextBox_password.Text & ";"
'
conn.ConnectionString = "server=" & Ser & "user id=" & Use & "password=" & Pas & "database=nfenlon_TestDB01"
'
Try
conn.Open()
MessageBox.Show("Connection Opened Successfully")
conn.Close()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
Finally
conn.Dispose()
End Try
'
End Sub

Private Sub frmLogin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox_server.Focus()
End Sub

Private Sub TextBox_password_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_password.Enter
Button_login_Click(byval sender As Object, byval e As System.EventArgs) Handles Button_login.Click
End Sub
End Class
May 22 '08 #1
Share this Question
Share on Google+
10 Replies


100+
P: 121
Hi guys. I'm using VB express 2005 and im having trouble calling one sub function from inside another. Can you help?

I have written an event procedure for a button click. Now i want to call that sub in the ENTER event of a textbox.
I have tried simply copying the name of the sub into the ENTER sub but it wont work, i get the error message "expression expected" and an underline under the "ByVal" part of the argument.


Imports MySql.Data.MySqlClient
Imports System.Data

Public Class frmLogin
Dim conn As MySqlConnection

Private Sub Button_cancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_cancel.Click
Application.Exit() 'kills the application/process
End Sub

Private Sub Button_login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_login.Click
'an example connection string: server=localhost; user id=mike; password=12345; database=in_out
conn = New MySqlConnection
Dim Ser, Use, Pas As String
Ser = TextBox_server.Text & ";"
Use = TextBox_username.Text & ";"
Pas = TextBox_password.Text & ";"
'
conn.ConnectionString = "server=" & Ser & "user id=" & Use & "password=" & Pas & "database=nfenlon_TestDB01"
'
Try
conn.Open()
MessageBox.Show("Connection Opened Successfully")
conn.Close()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
Finally
conn.Dispose()
End Try
'
End Sub

Private Sub frmLogin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox_server.Focus()
End Sub

Private Sub TextBox_password_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox_password.Enter
Button_login_Click(byval sender As Object, byval e As System.EventArgs) Handles Button_login.Click
End Sub
End Class
I didn't bother reading all of you're code, but if you're trying to go to the code in the button click event when the user enters the text box, you could just have the click event handler handle the enter event as well.
May 22 '08 #2

P: 8
ok. great. how exactly do i do that???
May 22 '08 #3

100+
P: 121
ok. great. how exactly do i do that???
Notice that the handler methods all have the keyword "handles" at the end of them. You can supply a comma-separated list of events that you want it to handle. Now, I'm not sure if you're going to be able to do that, since it's likely that Enter and click probably have different signatures. If that's the case, you can either call the click event directly from the enter handler, supplying Nothing as the arguments, or else both events can call a common function.
May 22 '08 #4

P: 8
Ok, not bad. It kind of works.
But now it executes the code when i tab into or out of the text box. not when i hit enter...
May 22 '08 #5

100+
P: 121
Ok, not bad. It kind of works.
But now it executes the code when i tab into or out of the text box. not when i hit enter...
I thought you wanted the event to be handled when the control gained focus. If you want to handle the event for a user pressing the enter key, then handle the keypress event. Examine what key was pressed. If it was the enter key, then you'll want to call the method to deal with it.
May 22 '08 #6

P: 8
Thanks man.
Sorry, didn't realise that the enter event was the same as getfocus, my mistake.

One last thing; how to determine if the enter key was pressed??

cheers
May 22 '08 #7

100+
P: 121
Thanks man.
Sorry, didn't realise that the enter event was the same as getfocus, my mistake.

One last thing; how to determine if the enter key was pressed??

cheers
Check if e.KeyChar is equal to the newline character (ASCII #13)
May 22 '08 #8

P: 8
thanks again. that worked like a dream but i'm still gettin an error when i call the button sub in the keypress sub.
If i type the sub with no arguments i get an "expected argument" error and if i type it as it is below it doesn't seem to like the ByVal parts of the argument....

Button_login_Click(ByVal sender As Object, ByVal e As System.EventArgs)

thanks for your help
May 22 '08 #9

100+
P: 121
thanks again. that worked like a dream but i'm still gettin an error when i call the button sub in the keypress sub.
If i type the sub with no arguments i get an "expected argument" error and if i type it as it is below it doesn't seem to like the ByVal parts of the argument....

Button_login_Click(ByVal sender As Object, ByVal e As System.EventArgs)

thanks for your help
If you are calling the Button_login_Click method, you have to supply the necessary arguments (you don't need to put ByVal if you're calling the method). In your case, you would call it like:

Button_login_Click(Nothing, Nothing)
May 22 '08 #10

P: 8
sweet!!!

Thanks for the help dude
May 22 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.