Consulting

Page 3 of 3 FirstFirst 1 2 3
Results 41 to 58 of 58

Thread: Solved: Vba Newbie Require Help Please..

  1. #41
    Hi, I dont require the validation for the combo box, as I have used your idea. So when the page loads the text is already inserted in the combo box with most of the questions. This gets me out of the validation, and I still use the same validation on the Checkboxes and the Radio Button that are left on the page. Thanx

  2. #42
    The problem has returned, god its so annoying..

    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 the other frames and validations work and it just breaks on this one.

    The whole code is as follows:

    Sub validateFrame1()
    Dim oCtl As Control
    Dim BolOK_Local As Boolean
    For Each oCtl In Fragender.Controls
    If Fragender.Controls(oCtl.Name).Value = True Then
    BolOK_Local = True
    End If
    Next
    If BolOK_Local = False Then
    strErrors = strErrors & vbCrLf & _
    "Please answer question 1!"
    End If
    End Sub

    it breaks on the bold line, any help please?

  3. #43
    this is weird because within the form i just inserted a new frame with random 2 option boxes, changed the coding abit and now its working for that. So i have just renamed the frame, thanx for your help.

  4. #44
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Pay attention please. I clearly stated that the procedure I used in the demo was for a frame with ONLY Option buttons.

    Not comboboxes.
    Not textboxes.

    Next, take a look at the_Initialize event of my demo userform. Notice that the combobox .AddItem is followed by a .ListIndex = 0? This makes the combobox display the first item in the list (the index is 0 - based). So MY comboboxes never are blank. I suggest you do the same - even if it is an item "Please select an age group"...or whatever.

    Of course...and by now you are really seeing how involved good validation is... you must always test to see if the user did not select anything, or more accurately, if the request text (ListIndex = 0) is the value of the combobox.

    I have no idea what you have done, but I have questions.

    In the code in the above post, it is "validateFrame2". Now...is this a frame? In my demo, the combobox for Age is NOT in a frame. Did you put it in one? if so...why? If the Age combobox is in the frame, and it is the only one, then there is NO need to run through all the controls in the frame. You would test directly against the combobox.

    My original code for validate frame was to determine if ANY Option button was selected. Not which one, but if ANY. Run through the code...it loops through the controls in the frame, and if the control is True (ie. an OPTION BUTTON is selected), then the local boolean is True.

    Get it? It is not the same logic for a combobox! Try doing a Debug.Print (or if you want a MsgBox) for a combobox. Hmmm, what do you see?. The logic in the procedure will ALWAYS end up with BolOK_Local = False. It is a combobox...not a optionbutton. An optionbutton value is true, or false. This is not the case for a combobox.

    Code must be written (and applied) appropriately for the task instructed, and the object that is being actioned. It is rare that code for object A can simply be copied and used for object B.

    In this case, my procedure was code applied to optionbuttons (object A)....and you are trying to use it for a combobox (object B).

    Are you having fun yet??

  5. #45
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Bad timing.

    Anyway, you are on your way.

    Steve...yeah, I understand. With just a couple of controls on a simple form _Change validation can be useful. Although not, IMHO, ever for textboxes. Ok...maybe not "ever", but I would sure have to have a good reason.

  6. #46
    Gerry thanx for your explanation, its given me an even better understanding thanx alot.

    lol the problem i was facing was, in the combobox coding you gave me the user was able to change the text in the combo box so i entered

    comboboxname.Style = fmStyleDropDownList

    And now its working fine, kool.

  7. #47
    why is it that when i work on my assignment at college, the layout and colours are totally different to when i work on it at home. Sometimes, it just gets so annoying. Like im working on it and its fine at college, but when i bring it home, its like the form has trippled the size, is it because were running 2 different office versions?

  8. #48
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Uh....have you compared what screen resolution you may be running on the different machines? Remember you are not explicitly sizing the userform. If a monitor has high resolution then its pixel sizes are smaller.

    In other words...

    A form is 600 pixels x 420 pixels.

    On a monitor set for 1900 x 1200 that form will take up a wee less than 1/3 (31.5%) of the horizontal real estate.

    That SAME form displayed on a monitor set for 800 x 600 will take up 3/4 (75%) of the horizontal real estate.

    The form is not twice as big...although it looks like that. Higher resolution means smaller pixels. It is not like pixels are a particular size. They are picture elements, and are totally dependent on the ability of the graphics system to make dots.

    Most Office (VBA) type programmers ignore this. And, if the work environment is such that everyone works at the same resolution, then it is not a problem.

    I work in an environment that ranges from 800 x 600 (around about 70% of our users) to 1900 x 1200 for the fancy design weenies.

    There are two possible solutions, if it is a problem.

    1. Make the same form say 3 - 4 times. I mean that literally. Four separate userforms with the same controls on them, the same code on them etc etc. But sized to display nicely at 800 x 600, 1024 x 768, 1280 x 960, 1900 x 1200. You need, of course to able to work with those resoultions to ensure the applicable form DOES display nicely. Then, in the procedure that opens the form have code lines that check the current screen resolution. Depending on that, display the applicable form.

    2. have one form, but after checking the screen resolution, dynamically resize the form and all the controls on it.

    Of the two, #2 is much more tedious to do. It depends on how many controls you have on the form. If there are a lot...this is a fair amount of work to do.

    In any case, no, it likely has nothing to do with the office versions.

  9. #49
    well i work on a laptop and the biggest resolution my latop has is 1200 by 800. I think i know what the problem is, the screen of the laptop is like that of a wide screen, width ways it is fine, but the height is way 2 small compared to that of college. I dont know what to do, but i shall talk with my teacher, lol just hope when he marks it its not on a widescreen screen.

    Thanx for your help

  10. #50
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Good luck.

  11. #51
    well i have nearly completed the project, and learnt so much from the people of vba express. But as usual, Im stuck in a pretty tricky situation, I have all but finished the coding to add the answers in a current word file, I have managed to do this for all of the comboboxes, option and check buttons. But i require assistance on the 2 text boxes. Its more than likely that your probably lost so let me just break it down, this is what i have done for a combobox.

    At the top the coding:

    Dim ages As String

    within the combooboxes i have

    With ActiveDocument
    If cboages = "Under 16" Then age = "Under 16"
    If cboages = "16 - 24" Then age = "16 - 24" Etc

    And with the finish button i have

    Selection.TypeText Text:=ages

    I think you have the general idea now, i Have done the above with all of the option and check boxes aswell, and they are all working, But im stuck on the text boxes.

    Any suggestions please?

  12. #52
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    What do you mean "stuck on the textboxes"? Stuck how? Do you mean some sort of validation of the contents? Do you mean what to do with the contents?

    Comments:[vba]With ActiveDocument
    If cboages = "Under 16" Then age = "Under 16"
    If cboages = "16 - 24" Then age = "16 - 24" Etc

    ' And with the finish button i have

    Selection.TypeText Text:=ages[/vba]Hmmmm, two points.

    1. Every If...Then statement in a procedure actually run. So - regardless of what cboages is, BOTH If statements will be done. So, if cboages = "Under 16" then the string ages will be set to "Under 16". BTW: you declare the string as ages, but set a variable as age. Nothing will be set as there is no variable age...it is ages.

    Are you using Option Explicit???? If not....start doing so, right now.

    When you are validating ONE thing for multiple values do not use multiple If statements. Use Select Case.[vba]Select Case oboages
    ' tells VBA to see what the value of oboages is
    case "Under 16"
    ages = "Under 16"
    Case "16 - 24"
    ages = "16 - 24"
    End Select[/vba]
    Select Case checks the cases, and ONLY processes the case that matches.

    Further, you don't need to put the text in again.[vba]Select Case oboages
    ' tells VBA to see what the value of oboages is
    case "Under 16"
    ages = oboages
    Case "16 - 24"
    ages = oboages
    End Select[/vba]

    Further, why are you even doing any validation at all here? It is not as if you are making any corrections. You are totally accepting whatever the user has put. So, REALLY, no matter what oboages is, the string ages will be that value. So if becomes that value - regardless of what the user inputs - why check?

    2. Following THAT...you can just do[vba]Selection.TypeText Text:= oboages[/vba]as in fact, you do not even need the string variable.

    3. Following THAT...be careful using Selection.Typetext. Are you watching carefully where in fact the Selection IS? It would be better to put your text information to explicitly defined locations, such as bookmarks, or formfields.

    As for your thing with the textboxes...I don't know what you are asking.

  13. #53
    Oh i understand now,

    Select Case cboage
    ' tells VBA to see what the value of oboages is
    Case "Under 16"
    age = cboage
    Case "16 - 24"
    age = cboage
    Case "25 - 34"
    age = cboage
    Case "35 - 44"
    age = cboage
    Case "45+"
    age = cboage
    End Select

    this is my whole code for the age combo box. Yes i do use option explicit, and i unserstand what you have said about the If and Select statements. I do not use validation on the comboboxes. Well atleast with your help i have cut down the code and only now i understood what you meant with the If statement repeating because when i looked at the word file all the values in the combobox appeared.

    What i mean with the textboxes is, what coding do i use to do exactly the same as i have done before with the comboboxes and the option/check buttons?

    for the option buttons ive used coding as follows:

    With ActiveDocument
    If optmale = True Then gender = "Male"
    End With

    And also decalred it as a string, and used the last selecttion.typetext line.

    1 final question how come with your select statement i dont use "With active document" and "End With"?? Do i stick with this in my option/check boxes?

  14. #54
    So far i have the following coding under the newmacro section

    Open "C:\ResultsTest.csv" For Output As #1
    Print #1, "1, GenderGoesHere"
    Close #1

    I know im in the right direction, i know the comma seperated the cell so the next one is selected. But is there anyway were i can get the "Gendergoeshere" and replace it with my initial answer because at the moment if i open up the csv file it reads "1, GenderGoesHere" but i need it to be somthing like "1, Male or Female(depending on the answer)".

    Any Suggestions would be welcome. Thanks In Advance

  15. #55
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    THINK![vba]Select Case cboage
    ' tells VBA to see what the value of oboages is
    Case "Under 16"
    age = cboage
    Case "16 - 24"
    age = cboage
    Case "25 - 34"
    age = cboage
    Case "35 - 44"
    age = cboage
    Case "45+"
    age = cboage
    End Select[/vba]I take 10 packs of cards. I take the Queen of Clubs out of all of them. I take those 10 cards and shuffle them, and then put one behind my back. I ask you to guess what card it is. Are you going to say the 10 of Diamonds? No.

    You are asking VBA to check the value of cboages...then in ALL cases you make ages = cboages. So WHY are you testing? No matter what cboages =, you still make ages equal to it. There is no difference in the cases. Sure the value of the cases are different ('Under 16", "35 - 44")...but who cares???? No matter what the value is you STILL make ages = cboages. So it makes no difference whatsoever.
    What i mean with the textboxes is, what coding do i use to do exactly the same as i have done before with the comboboxes and the option/check buttons?
    THINK!

    The comboboxes can only have the values that you placed in as items. The option buttons only have the choices you put to the user.

    The textboxes are TEXT user input. What if the user enters "Fgsdkk"? What if they put in "15 feet 9 inches" as height? (They meant 5 feet.)

    This is one reason why it is good design to limit the use of textboxes on userform. If only certain values are acceptable...give them those values to choose from (say in a combobox). A textbox - if you are truly validating - needs serious validation, as you have to to be able to cover ANYTHING the user puts in.

  16. #56
    I have completed my project, and i know i might sound a lil bit dumb but i need help on the documenting of the code. I have everything done, and documented it to the best of my knowledge, but is it possible to get a lil help like if i go wrong anywere if anybody could help me out id appreciate it.

  17. #57
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    No, certainly not from me.

    I have no idea what you even mean by "documenting of the code". Do you mean commenting?

    In any case, you have received more than enough help on your project. Be brave...do it yourself.

  18. #58
    dont worry i somehow got it all done, thanks for all your help i appreciated it bud..

Posting Permissions

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