Results 1 to 16 of 16

Thread: Little programming help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    296

    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:
    1. 'This is in the Module.
    2. Public Sub EMptyWS(FormName As Form, Cancel As Boolean)
    3. If Len(FormName.txtSumInsured.Text) = 0 Then
    4. MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
    5. Else
    6. FormName.txtWs.Text = CDbl(FormName.txtperWs.Text) * CDbl(FormName.txtSumInsured.Text) / 100
    7. KeepFocus = True
    8. End If
    9. End Sub
    10.  
    11. 'This is in the Form
    12. Private Sub mskNCD_Validate(Cancel As Boolean)
    13. EMptyWs FrmPoliCreate ' Arugement Not Optional ???
    14. 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

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    296
    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:
    1. 'This is in the Module.
    2. Public Sub EMptyWS(FormName As Form, Cancel As Boolean)
    3. If Len(FormName.txtSumInsured.Text) = 0 Then
    4. MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
    5. Else
    6. FormName.txtWs.Text = CDbl(FormName.txtperWs.Text) * CDbl(FormName.txtSumInsured.Text) / 100
    7. Cancel = True 'this is to setfocus on mskNCD textbox until  
    8. 'Len(FormName.txtSumInsured.Text)  > 0
    9. 'But i would like to set focus txtSumInsured  until the condition is  Len(FormName.txtSumInsured.Text)  > 0.
    10. 'How? if i choose to set txtSumInsured.Setfocus the cursor would not be there But another textbox
    11. 'where i click on another textbox
    12. End If
    13. End Sub
    14.  
    15. 'This is in the Form
    16. Private Sub mskNCD_Validate(Cancel As Boolean)
    17. EMptyWs FrmPoliCreate ' Arugement Not Optional ???
    18. End Sub
    Last edited by CutePretty; Jan 6th, 2002 at 03:23 AM.

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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:
    1. 'This is in the Module.
    2. Public Function EMptyWS(FormName As Form) As Boolean
    3.  
    4. 'Set EmptyWS to True or False depending on ur conditions
    5. 'eg
    6. EmptyWS = True 'If focus is to stay on textbox
    7. 'and
    8. EmptyWS = False 'if ok to move
    9. '................
    10.  
    11. [edit] removed rest of function in case it caused confusion[/edit]
    12.  
    13. End Function
    14.  
    15. 'This is in the Form
    16. Private Sub mskNCD_Validate(Cancel As Boolean)
    17.     Cancel =  EMptyWs (FrmPoliCreate)
    18. End Sub
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5
    Lively Member
    Join Date
    Jan 2002
    Posts
    108
    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.

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  7. #7
    Staifour
    Guest
    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

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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:
    1. 'This is in the Module.
    2. Private Function EMptyWS(FormName As Form) As Boolean
    3.     If Len(FormName.Text1.Text) = 0 Then
    4.         MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
    5.         EMptyWS = True
    6.     Else
    7.         FormName.Text2.Text = "Made it"
    8.         EMptyWS = False
    9.     End If
    10. End Function
    11.  
    12. 'This is in the Form
    13. Private Sub text1_Validate(Cancel As Boolean)
    14.     Cancel = EMptyWS(Me)
    15. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    296
    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:
    1. 'This is in the Module.
    2. Private Function EMptyWS(FormName As Form) As Boolean
    3.     If Len(FormName.txtSumInsured.Text) = 0 Then
    4.         MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
    5.        ' EmptyWs = True      
    6.         FormName.TxtSumInsured.SetFocus
    7. Else
    8.         FormName.txtWs = FormName.txtperWs  * FormName.txtSumInsured / 100
    9.         EMptyWS = False
    10.     End If
    11. End Function
    12.  
    13. 'This is in the Form
    14. Private Sub txtperWs_Validate(Cancel As Boolean)
    15.     Cancel = EMptyWS(Me)
    16. 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.

  10. #10
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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:
    1. Private Function EMptyWS(FormName As Form) As Boolean
    2.     If Len(FormName.txtSumInsured.Text) = 0 Then
    3.         MsgBox "Please enter a Sum Insured amount first", vbExclamation, "Wind Screen"
    4.         [b]EMptyWS = True[/b]
    5. Else
    6.         FormName.txtWs = FormName.txtperWs  * FormName.txtSumInsured / 100
    7.         EMptyWS = False
    8.     End If
    9. End Function
    10.  
    11. 'This is in the Form
    12. Private Sub txtperWs_Validate(Cancel As Boolean)
    13.     [b]If EMptyWS(Me) then txtsuminsured.setfocus[/b]
    14. End Sub

    secondly, have u considered a state machine approach rather than using message boxes.
    VB Code:
    1. 'PS This is very basic example and state machines can do much more than this
    2. Private Sub Text1_Validate(Cancel As Boolean)
    3.     Statemachine Text1, Text3
    4. End Sub
    5.  
    6. Private Sub Text2_Validate(Cancel As Boolean)
    7.     Statemachine Text1, Text3
    8. End Sub
    9.  
    10. Private Sub Statemachine(BoxToTest As TextBox, BoxToCalc As TextBox)
    11.     If Len(BoxToTest.Text) = 0 Then
    12.         BoxToCalc.Enabled = False
    13.         BoxToTest.SetFocus
    14.     Else
    15.         BoxToCalc.Enabled = True
    16.         BoxToCalc = BoxToTest * 2
    17.     End If
    18. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    296
    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.

  12. #12
    Lively Member
    Join Date
    Jan 2002
    Posts
    108
    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

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    296
    Sorry to bother everyone.

    Drbios_Xviiii, of coz that would be done but my main problem is the Setfocus.


    VB Code:
    1. 'This is in the Form
    2. Private Sub txtperWs_Validate(Cancel As Boolean)
    3.     If EMptyWS(Me) then txtsuminsured.setfocus
    4. 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

  14. #14
    Lively Member
    Join Date
    Jan 2002
    Posts
    108
    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

  15. #15
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    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:
    1. 'Remove this if only using Tab to move between text boxes
    2. Private Sub txtperWs_Validate(Cancel As Boolean)
    3.     If EMptyWS(Me) then txtsuminsured.setfocus
    4. End Sub
    5.  
    6. 'Add this
    7. Private Sub [b]txtWs[/b]_GotFocus()
    8.     If EMptyWS(Me) then txtsuminsured.setfocus
    9. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  16. #16
    Lively Member
    Join Date
    Jan 2002
    Posts
    108
    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
  •  



Click Here to Expand Forum to Full Width