Consulting

Page 2 of 3 FirstFirst 1 2 3 LastLast
Results 21 to 40 of 58

Thread: Solved: Vba Newbie Require Help Please..

  1. #21
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    kaiser,
    try looking at this code by Malcolm...it deals with comboboxes but the principle is the same..it disables the continue button until fields are completed. Hope this helps get you started in the right direction and if you have more problems post back.

    Be sure you rename the buttons etc in the code as it has not been changed in any way to fit your file. just an example I thought would give you some tips.
    [VBA]
    Private Sub Userform_Initialize()
    ComboBox1.AddItem ("Joe")
    ComboBox2.AddItem ("is")
    ComboBox3.AddItem ("cool.")

    CommandButton1.Enabled = False
    End Sub
    Private Sub CommandButton2_Click()
    Me.Hide
    End Sub
    Private Sub ComboBox1_Change()
    SetButton
    End Sub

    Private Sub ComboBox2_Change()
    SetButton
    End Sub

    Private Sub ComboBox3_Change()
    SetButton
    End Sub
    Private Sub SetButton()
    If ComboBox1.Value <> "" And ComboBox2.Value <> "" And ComboBox3.Value <> "" Then
    CommandButton1.Enabled = True
    Else
    CommandButton1.Enabled = False
    End If
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  2. #22
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    As Gerry points out in post #20....your form could use some redesign but the code above will help you with the validation logic.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #23
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    but it keeps throwin errors, any help please?
    Please, when you get errors do not say to us that you are getting errors. It is useless information. You need to tell us you are getting errors.....then tell us what the errors ARE. How can we possibly know what the errors are unless YOU tell us what they are. Keep in mind that we can not see your monitor.[vba]If obmale.Value = False Then MsgBox ("Please Complete Question1")
    Else
    If obfemale.Value = False Then MsgBox ("Please Complete Question1")
    End If[/vba]If obMale is TRUE (Male is selected)...then the message will be displayed. If obMale is TRUE then the first statement is false, so the Else part will run. Since Male = True (and Female = False), then Else is TRUE (Female = False)...so message is displayed.

    If obMale IS False, the message is displayed.

    So not matter what, the message should be displayed. The LOGIC for the message (the question is not answered - that is, NEITHER Male or Female was selected) should be:

    If obMale.Value = False AND obFemale.Value = False Then
    Msgbox ("Please Complete Question1")
    End If

    Ther are a LOT of questions here. Validation can be done - and of course SHOULD be done. It is not difficult, but it is the most tedious part of programming. You have to do step-by-step logic.

  4. #24
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ahem......underscore character please.

  5. #25
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    see attached for your work.doc with the first 2 items set up for validation as per the code I posted earlier. Only the gender m or f and the how old drop down are done for you....I think you can take it from there if you look at it closely.

    you must choose male or female and the combobox must have a selection before the continue button is enabled.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #26
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And more further logic help. As Steve points out, if your logic requires that different things must be completed, then you will need ample use of AND.

    Remember, error-trapping is mostly trying to catch system errors, that is, run-time errors. Validation however, is always the programmer's issue. VBA does not care if a frame with option buttons has any selected, or not. The requirement for one to be selected is yours, and therefore it is YOUR logic to do.

    One of the advantages of NOT having textboxes in frames explaining what th eframe is for, is that it is easier to loop through the option buttons, getting the values.

    i am attaching a demo document. You can load the form by clicking "Show My Form" on the top toolbar. This will load the form.

    There are two frames with option buttons. There is a Continue commandbutton. Essentially it works like this:

    There is a Public string variable - strErrors.

    Continue commandbutton first clears the strError variable - to start fresh.

    It calls a validation procedure for the first frame - fraTest1.

    Sub validateFrame1 makes a Control object and then checks all the controls in the frame control collection. As the ONLY controls in the frame as Option buttons (no textboxes) there is no need to do a check of the type of controls. If ANY option button is selected, a local Boolean value is set to TRUE. At the end of the validation if this boolean is False, then NO option button has been selected, and a string error message is appended to the Pubic variable strErrors.

    Control is past back to the Continue commandbutton.

    It calls a validation procedure for the second frame - fraTest2. This does the same kind of validation. If no button selected and error message string to appended to strErrors.

    Control is past back to the Continue commandbutton.

    It checks to see if strErrors is NOT blank. If it IS blank (no errors), then it - in this demo - says everything is OK and unloads the form.

    If strErrors is NOT blank - there are errors, then it displays a message with the error messages, and exits the sub. This puts you back with the form.

    Now...you do have a lot of questions, therefore a lot of checking. But there is no other way, you have to check. And it is completely a question of logic.

    I strongly suggest you break your validation logic checking into separate Subs and make Calls to them. Do not try to do all your validation in one procedure. This will help greatly in any debugging you need to do...and you will.

    BTW: I notice you do NOT have any Unload instruction for your form, only .Hide. Hide does NOT unload the form. Hide retains the form in memory. The demo doc is attached, but here is the code anyway.[vba]Option Explicit
    Public strErrors As String

    Private Sub cmdContinue_Click()
    strErrors = ""
    Call validateFrame1
    Call validateFrame2
    If strErrors <> "" Then
    MsgBox strErrors
    Exit Sub
    Else
    MsgBox "All OK"
    Unload Me
    End If
    End Sub

    Sub validateFrame1()
    Dim oCtl As Control
    Dim BolOK_Local As Boolean
    For Each oCtl In fraTest1.Controls
    If fraTest1.Controls(oCtl.Name).Value = True Then
    BolOK_Local = True
    End If
    Next
    If BolOK_Local = False Then
    strErrors = strErrors & vbCrLf & _
    "Test frame1 is blank."
    End If
    End Sub

    Sub validateFrame2()
    Dim oCtl As Control
    Dim BolOK_Local As Boolean
    For Each oCtl In fraTest2.Controls
    If fraTest2.Controls(oCtl.Name).Value = True Then
    BolOK_Local = True
    End If
    Next
    If BolOK_Local = False Then
    strErrors = strErrors & vbCrLf & _
    "Test frame2 is blank."
    End If
    End Sub[/vba]

  7. #27
    Thank you so much both of you, you have helped me so much. Yeh I know I had to make the userform look much better, and that was just a basic view of what i wanted to do, the Layout was the last thing on my mind. I liked your demo version but its a little bit to complicated for me, thanx alot tho, i would of loved to work with yours but I only started working with vba since monday so I dont have that much knowledge and if i used your coding and was clueless my lecturer would not be very much impressed.

    Lucas i also downloaded yours and to be honest its great, its exactly what i was looking for, at first I was using fumels coding for the If statements on the radio button which threw message box's if no answer was selected.

    Im still working on this and thanx alot both of you.

  8. #28
    Personally to me fumel yours looks like another language, With lucas's coding i understand the concept. I can tell your an expert at this, lol but im a beginner.

    I was just wondering because ive created a private sub called setbutton, this is what does the validation check BUT do i need to type in setbutton in all of the radiobutton/combo boxes/ checkboxes and text boxes coding?

  9. #29
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Quote Originally Posted by kaiser soze
    I was just wondering because ive created a private sub called setbutton, this is what does the validation check BUT do i need to type in setbutton in all of the radiobutton/combo boxes/ checkboxes and text boxes coding?
    yes you will have to call the setbutton sub from the on click event of all of your radio buttons. Since this turns out to be an assignment we will let you work on this with the info we have given you to figure out some of the rest. I would suggest that you study Gerry's validation script for some understanding of what your trying to accomplish.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #30
    thanks for your help, i owe u 1

  11. #31
    ive thrown my previous coding out the window as i have just understood what gerry was warning me about, I have thoroughly checked hes coding and just this one bit puzzles me..

    it comes up with an error

    "Run time error 438"

    "Object doesnt support this property or Method"

    The line it breaks on is:

    If fragender.Controls(oCtl.Name).Value = True Then

    Im sure it supports this property or method, as when i ran gerrys it had no problem.

    I know you told me to do the rest by myself, but im puzzled at this part and could do with just a little help. Im sorry if im asking for too much

  12. #32
    this is so weird, i just copyed and pasted the coding and its started working hmmmmmm. thanx

  13. #33
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes...as this is an assignment, we will stop now.

    I have other comments though.

    Sorry, but if you think my demo is complicated - and you are wanting to really validate all the questions on YOUR form...then you are in trouble. My form is actually very very simple.
    the Layout was the last thing on my mind
    Then you will make bad forms. Period. A userform is - or should be - DESIGNED for a user to use. If it does not do that very well, then I don't care a whit if it has fabulous coding. It is a bad userform. When creating anything for someone else to use, then layout is, IMHO, the #1 issue...before any code it may contain. Again, if a user gets confused, or has difficulty using a form you make...it is NOT the user's fault..it is yours.

    As for validation...with due respect to Steve, his approach was different from mine. His approach is taking validating from a Change event to a control. In other words, when a control is changed his code is doing a form of validation, true. The result of this validation is a disabling of further action.

    My approach was a validation of the values of the form itself.

    How can I put this??? Steve's code uses a sample of a control and, using the Change event, checks if things are OK - with THAT control (and/or others using AND logic). If it is not OK, then he disables being able to click Continue. To use this effectively, yes, you need to have the validating going on for every control.

    My approach was different. My logic disables nothing. The logic is applied when you click Continue. At that point, the logic goes through the form checking ALL control values. If things are OK, then you proceed. If things are NOT good, then you get a message stating which control had an error, and puts the user back on the form. By building a string of error messages, the user will precise information that THIS information is blank (or wrong).

  14. #34
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Don't hesitate to post back with specific questions.....we just want you to learn from your project so we won't do your assignment for you but will gladly answer your questions and give you pointers. Your doing ok for someone who just dived in so hang in there.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #35
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just to give you an idea....

    Here is a version of a userform that does not take up huge real estate. Not only that, but pay attention to the fact that you can easily use the keyboard. The TabIndex are explicitly set for movement through the form. You can Tab and use the arrow keys to select items, then Tab to the next control. They are explicitly set for the flow order.

    I put no code - other than loading the comboboxes - into this. I removed any validation code. You are starting to see that it may take some work. As this is an assignment...sorry...that work is yours to do.

    When you get practiced at this you can do the control building part pretty darn fast. I built this form in about 20 minutes.

    I would also STRONGLY suggest you always use explicit names for everything. If you are doing any passing of code modules and/or forms, also look at standard notation.

    cbo - for comboboxes, not Cmb
    opt - for Option Button, not op

    Technically speaking, it does not matter one bit...shrug, there just is some standard prefixes. No one will force you to do anything, which is one of the fun things about VBA. That, and there are multiple ways to do the same thing. Like the fact that I can think of at least two other very valid ways to design this form.

    You seem to being doing quite well for just starting. Keep going!

  16. #36
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Further testing shows some problems with if/else....changed it to if/elseif/else statements.....see the Private Sub SetButton

    also look at the change events to trigger the setbutton sub. I did a few of them for you and this seems to be working as expected.....let us know how your doing.
    [VBA]
    Private Sub obfemale_Change()
    SetButton
    End Sub
    Private Sub obmale_Change()
    SetButton
    End Sub
    Private Sub CmbAge_Change()
    SetButton
    End Sub
    Private Sub txtheight_Change()
    SetButton
    End Sub
    Private Sub txtweight_Change()
    SetButton
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  17. #37
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just for my own interest...Steve, why would you do validation this way? It seems overly intrusive. Using Change means EVERY time the user does anything you have to run through code. Plus you have not included any logic to deal with the option buttons within the frames.

    In particular, using _Change on the textboxes is very intrusive. Say the user types in 5 ' 10" in the height textbox. Here is what happens.

    "5" - txtheight_Change() fires; SetButton fires
    "'" - txtheight_Change() fires; SetButton fires
    " " - txtheight_Change() fires; SetButton fires
    "1" - txtheight_Change() fires; SetButton fires
    "0" - txtheight_Change() fires; SetButton fires


    It is a Change event. Every single change will fire it (and SetButton). That one user input will execute 10 procedures!

    I am not being critical, yes, it would work, but it seems...hmmmm....not really the best way of doing validation of a form.

    My point being that using Change - especially on a textbox causes a whole bunch of processing to be done before the user has even finished entering their input. Is it not better to validate on what they have actually entered (after they have finished), rather than every single individual part of it, as they enter it?

  18. #38
    Thanx you 2, lol as you can see i am still working on this. Even though this does not have to be in till 4 weeks I am trying my best, my interface was actually all done up and running UNTIL i saw your demo interface, now that is what im talking about, its a bit small but perfect Is it ok if i use yours and add some of my colours and text?

    I can see both of the validations are useful, personally id prefer gerrys as this strikes back a message box, and im not too fond of setbutton but thanx anyway lucas.

    The most recent update is that I have decided to keep to the 1 form, on the userform2 there werre more questions but i have added these to the first userform. I think my next aim is to develop with gerrys validation, but from this validation (which at the moment works with radio buttons and check boxes only) I will try to make this work with the 2 text boxes and the combo boxes that are left. Thanks dudes

  19. #39
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I agree Gerry and it looks as though kaiser has come to the same conclusion. The method I used was originally for only one combobox and a much simpler userform.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  20. #40
    im about to loose the will to live now,lol

    i have so far the following (as theres no text in the combo box when i load the form)

    Sub validateFrame2()
    Dim oCtl As Control
    Dim BolOK_Local As Boolean
    For Each oCtl In FraAge.Controls
    If FraAge.Controls(oCtl.Name).Value = True Then
    BolOK_Local = True
    End If
    Next
    If BolOK_Local = False Then
    strErrors = strErrors & vbCrLf & _
    "Please enter your age!"
    End If
    End Sub

    Its Still throwing up the message box even if the combobox has been selected and dont get me started on the text boxes lol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •