|
-
Jan 6th, 2002, 03:04 AM
#1
Thread Starter
Hyperactive Member
Little programming help
The below code give me a compile Error : Arugement not optional on EMptyboth FrmPoliCreate. I know it is my coding problem how do i solve this?
I have another problem is I have to call EmptyWs in mskNCD textbox but would like to SetFocus on txtSumInsured textbox Until TxtSumInsured is True.
If i Include txtSumInsured.SetFocus The cursor will goes off to textbox1 where i click on the textbox1 even the condition iS TRUE.
VB Code:
'This is in the Module.
Public Sub EMptyWS(FormName As Form, Cancel As Boolean)
If Len(FormName.txtSumInsured.Text) = 0 Then
MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
Else
FormName.txtWs.Text = CDbl(FormName.txtperWs.Text) * CDbl(FormName.txtSumInsured.Text) / 100
KeepFocus = True
End If
End Sub
'This is in the Form
Private Sub mskNCD_Validate(Cancel As Boolean)
EMptyWs FrmPoliCreate ' Arugement Not Optional ???
End Sub
The reason that i implement in module and call in form because i have many forms which need this code.
thank you in at advance
-
Jan 6th, 2002, 03:11 AM
#2
PowerPoster
Hi
The problem is that u have a required parameter in your sub
Public Sub EMptyWS(FormName As Form, Cancel As Boolean)
that you dont reference in your call to the sub
EMptyWs FrmPoliCreate , nothing here
Since u dont use the cancel property in your sub i guess the easiest thing to do is to remove it
Public Sub EMptyWS(FormName As Form)
Regards
Stuart
-
Jan 6th, 2002, 03:19 AM
#3
Thread Starter
Hyperactive Member
thank beachbum, Sorry i made a mistake on Copy and paste on this forum It should be this way that cause me error
VB Code:
'This is in the Module.
Public Sub EMptyWS(FormName As Form, Cancel As Boolean)
If Len(FormName.txtSumInsured.Text) = 0 Then
MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
Else
FormName.txtWs.Text = CDbl(FormName.txtperWs.Text) * CDbl(FormName.txtSumInsured.Text) / 100
Cancel = True 'this is to setfocus on mskNCD textbox until
'Len(FormName.txtSumInsured.Text) > 0
'But i would like to set focus txtSumInsured until the condition is Len(FormName.txtSumInsured.Text) > 0.
'How? if i choose to set txtSumInsured.Setfocus the cursor would not be there But another textbox
'where i click on another textbox
End If
End Sub
'This is in the Form
Private Sub mskNCD_Validate(Cancel As Boolean)
EMptyWs FrmPoliCreate ' Arugement Not Optional ???
End Sub
Last edited by CutePretty; Jan 6th, 2002 at 03:23 AM.
-
Jan 6th, 2002, 03:27 AM
#4
PowerPoster
Hi again
I see that u now have Cancel = True in the top sub but I still cant see what it is doing. Do u want to return a True or False result to the Validate routine? If so then you should be using a function something like this.
VB Code:
'This is in the Module.
Public Function EMptyWS(FormName As Form) As Boolean
'Set EmptyWS to True or False depending on ur conditions
'eg
EmptyWS = True 'If focus is to stay on textbox
'and
EmptyWS = False 'if ok to move
'................
[edit] removed rest of function in case it caused confusion[/edit]
End Function
'This is in the Form
Private Sub mskNCD_Validate(Cancel As Boolean)
Cancel = EMptyWs (FrmPoliCreate)
End Sub
Regards
Stuart
-
Jan 6th, 2002, 04:23 AM
#5
Lively Member
I notice that you have this function down as a sub routine :
Public Sub EMptyWS(FormName As Form, Cancel As Boolean)
This Sub has to be called with the Call Command
in example:
[vb_code]
Call EMptyWs(FrmPoliCreate, Cancel)
'the second parameter is very important, as well as the first.
-
Jan 6th, 2002, 04:35 AM
#6
PowerPoster
hi DrBios
U dont need to use Call when 'calling' a subroutine. If u do use Call u enclose the parameters in parentheses. If u dont use call, u dont use parentheses
eg These are the same
1-> Call MySubroutine(MyParameter, MyOtherParameter)
2-> MySubroutine MyParameter, MyOtherParameter
The problem is the use of a sub instead of a function and as u said the second parameter is missing (although it turns out that it isnt required in the new function)
regards
Stuart
-
Jan 6th, 2002, 04:40 AM
#7
i wanted to answer you just like beachbum did but he did first
anyway i have another way to do it ( since i think that the code you pasted may not be the full code )
you can add optional before Cancel when declaring the sub
it's something like that
Code:
Public Sub EMptyWS(FormName As Form, Optional Cancel As Boolean)
i didn't test it but i think it works
-
Jan 6th, 2002, 04:49 AM
#8
PowerPoster
Hi staifour
yes u are right that u could make the parameter optional but again I stress that this is in effect the 'wrong' problem. Ie The parameter is not something that had to be passed at all (whether optional or required) but something that had to be returned from a function. I didnt include CP's full code becos I wasnt quite sure of her conditions for setting True or False.. .So i just threw together this little example to show what i mean.
Regards
Stuart
VB Code:
'This is in the Module.
Private Function EMptyWS(FormName As Form) As Boolean
If Len(FormName.Text1.Text) = 0 Then
MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
EMptyWS = True
Else
FormName.Text2.Text = "Made it"
EMptyWS = False
End If
End Function
'This is in the Form
Private Sub text1_Validate(Cancel As Boolean)
Cancel = EMptyWS(Me)
End Sub
-
Jan 6th, 2002, 05:38 AM
#9
Thread Starter
Hyperactive Member
Thanks everyone for the interest of my problem. I am appreciate with the time spent of this post. I am sorry if the question was unclear. Let me write down in words to clarify my problem again. Here it goes
There are 3 textBox (txtSumInsured, txtperWs, txtWs )
I am trying to get the amount of txtWs. In order to get the amount i need to have txtSumInsured and txtperWs
Here is the calculation
txtWs = txtperWs * txtSumInsured / 100
I am trying to Validate txtSumInsured(textbox) in txtperWs(textBox) whether txtSumInsured(textBox) is Empty.
If txtSumInsured(textBox) is Empty. A Message Will Prompt and the cursor will be in txtSumInsured(textbox) Until txtSumInsured(textbox) is NOT EMPTY.
The Reason that i want to validate txtSumInsured in txtperWs because I need to have txtSumInsured to calculate an amount in txtWs(textBox)
VB Code:
'This is in the Module.
Private Function EMptyWS(FormName As Form) As Boolean
If Len(FormName.txtSumInsured.Text) = 0 Then
MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
' EmptyWs = True
FormName.TxtSumInsured.SetFocus
Else
FormName.txtWs = FormName.txtperWs * FormName.txtSumInsured / 100
EMptyWS = False
End If
End Function
'This is in the Form
Private Sub txtperWs_Validate(Cancel As Boolean)
Cancel = EMptyWS(Me)
End Sub
If you notice, i have changed EMptyWS = True to FormName.TxtSumInsured.SetFocus BUT the cursor does not keepFocus until TxtSumInsured is NOT EMPTY. I want to be there until user has enter an amount in txtSumInsured.
THank you in at advance.
-
Jan 6th, 2002, 05:55 AM
#10
PowerPoster
Hi again CP
Ok i get ya now but u cant put focus back to a different textbox just by setting cancel in the validate event of another textbox... so i think u should forget about setting Cancel... and also dont set focus from a module it can only lead to probs.
VB Code:
Private Function EMptyWS(FormName As Form) As Boolean
If Len(FormName.txtSumInsured.Text) = 0 Then
MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
[b]EMptyWS = True[/b]
Else
FormName.txtWs = FormName.txtperWs * FormName.txtSumInsured / 100
EMptyWS = False
End If
End Function
'This is in the Form
Private Sub txtperWs_Validate(Cancel As Boolean)
[b]If EMptyWS(Me) then txtsuminsured.setfocus[/b]
End Sub
secondly, have u considered a state machine approach rather than using message boxes.
VB Code:
'PS This is very basic example and state machines can do much more than this
Private Sub Text1_Validate(Cancel As Boolean)
Statemachine Text1, Text3
End Sub
Private Sub Text2_Validate(Cancel As Boolean)
Statemachine Text1, Text3
End Sub
Private Sub Statemachine(BoxToTest As TextBox, BoxToCalc As TextBox)
If Len(BoxToTest.Text) = 0 Then
BoxToCalc.Enabled = False
BoxToTest.SetFocus
Else
BoxToCalc.Enabled = True
BoxToCalc = BoxToTest * 2
End If
End Sub
-
Jan 6th, 2002, 06:08 AM
#11
Thread Starter
Hyperactive Member
tx beachbum for your suggestion. i am very touch with ur kindly help too.I would test what you have suggested on statemachine. You must be very good in vb programming and as for me i am just a few month old on vb programming. I wonder, where can i start to learn vb programming? So far, i have basic on programming like IF else, do while, simple sql to interact with db but not passing parameter, call function and etc.
-
Jan 6th, 2002, 06:14 AM
#12
Lively Member
Couldn't all of this be simplified with a series of if statements?
in theory:
3 TextBoxes on Form1 Labeled TextBox1, TextBox2, TextBox3
and a command button named CalculateCMD_Click
Private Sub CalculateCMD_Click()
If Form1.TextBox1.Text = "" Then MsgBox "TextBox1 is Empty, Please provide input for TextBox1", vbExclamation, "Wind Screen": Exit Sub
If Form1.TextBox2.Text = "" Then MsgBox "TextBox2 is Empty, Please provide input for TextBox2", vbExclamation, "Wind Screen": Exit Sub
TextBox3.Text = TextBox1.Text * TextBox2.Text / 100
End Sub
-
Jan 6th, 2002, 06:22 AM
#13
Thread Starter
Hyperactive Member
Sorry to bother everyone.
Drbios_Xviiii, of coz that would be done but my main problem is the Setfocus.
VB Code:
'This is in the Form
Private Sub txtperWs_Validate(Cancel As Boolean)
If EMptyWS(Me) then txtsuminsured.setfocus
End Sub
hi beachbum,
Despite txtSumInsured.SetFocus when i click on TAB, the cursor cannot stand put on txtsuminsured instead the cursor goes to the next textbox.
I want to make sure that txtSumInsured is Fill up then only can proceed to another textbox
-
Jan 6th, 2002, 06:30 AM
#14
Lively Member
try using the same method I listed before except modify it to look like this
Private Sub CalculateCMD_Click()
If Form1.TextBox1.Text = "" Then MsgBox "TextBox1 is Empty, Please provide input for TextBox1", vbExclamation, "Wind Screen": Exit Sub
If Form1.TextBox2.Text = "" Then MsgBox "TextBox2 is Empty, Please provide input for TextBox2", vbExclamation, "Wind Screen": Exit Sub
TextBox3.Text = (TextBox1.Text * TextBox2.Text) / 100
End Sub
Private Sub TextBox1_LostFocus()
If Form1.TextBox1.Text = "" Then MsgBox "TextBox1 Is manditory", vbExclamation, "Wind Screen": Form1.TextBox1.SetFocus
End Sub
-
Jan 6th, 2002, 06:35 AM
#15
PowerPoster
Hi CP
Yeah the problem is that the Tab key moves the focus regardless of the setfocus set in the validate event. So, if u end up allowing the user to press enter to move between textboxes keep the code in the validate event but also repeat it in the gotfocus event of the next textbox in taborder.
regards
VB Code:
'Remove this if only using Tab to move between text boxes
Private Sub txtperWs_Validate(Cancel As Boolean)
If EMptyWS(Me) then txtsuminsured.setfocus
End Sub
'Add this
Private Sub [b]txtWs[/b]_GotFocus()
If EMptyWS(Me) then txtsuminsured.setfocus
End Sub
-
Jan 6th, 2002, 06:41 AM
#16
Lively Member
yes but in the last example I posted...when the tab key or enter key is pressed and textbox1 is empty, it sends a msgbox up stating that textbox 1 needs to be manditorally filled, and then sets the focus back to textbox 1......meaning nothing else can be done until textbox1 is filled
once textbox1 contains a number, then textbox 2 can be entered
Textbox1 needs to have a tab index of 0
so that it's first to receive the focus
test it out
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|