PDA

View Full Version : Solved: Vba Newbie Require Help Please..



kaiser soze
10-18-2006, 07:27 AM
Hi, I have started working with vba since monday. I have tried my best and got most of the interface out the way, and the coding is nearly finished aswell. The main problem i am facing at the moment, is when i select a button, the button takes me on the next page. I dont want this to happen, as on the first page there are 9 questions, all questions must be filled in, in order for the user to be taken to the second page. Sorry if you are not understanding me, I shall try to break it down.

Question 1 - 2 radio buttons
Question 2 - 1 combo box
Question 3 - 2 text boxes
Question 4 - 4 radio buttons
Question 5 - 1 combo box
Question 6 - 4 radio buttons
Question 7 - 4 radio buttons
Question 8 - 3 radio buttons, and
Question 9 - 5 check boxes.

What i need the program to do is, when the "Next" button is selected, it shall look at all of the values and if none of the value in a question are met, a message box should appear letting the user know.

Thanks

lucas
10-18-2006, 07:35 AM
Hi kaiser,
Could you possibly post the document? Sanitized of any personal or proprietary info.
It would make it much easier to help with your problem.

fumei
10-18-2006, 07:42 AM
kaiser,
The main problem i am facing at the moment, is when i select a button, the button takes me on the next page. The problem is - WE are not facing it. We can't see over your shoulder at your screen.

So...we have no idea:

1. are these buttons on a userform (I think so, but...)?
2. what "pages" are you talking about? Are these multi-pages on a userform? Pages in the document?
3. if a button "takes" you to a "next page" then it has been told to do so. We can not figure this out unless we can see soemthing - preferably, as Steve mentions, an actual document. However, even posting relevant code will help a lot.

kaiser soze
10-18-2006, 07:53 AM
www(dot)uploading(dot)com/files/21QFFQ67/Work(dot)doc(dot)html
hope it helps

were it says dot put in .

as i cant post link as i have less than 5 posts

kaiser soze
10-18-2006, 07:59 AM
I know im pushing it but once you download the file and look at the first userform, on the third question there is a help button, i have the following code for that button

MsgBox ("1 Stone = 14 Pounds 1 Kg = 2.2 Pounds 1 Feet = 30 Centimetres 1 Inch = 2.5 Centimetres")

the message box shows the information like in 1 complete line. Ive been going crazy for the past few days by trying to find the code that list the information in the message box for example

1 Stone = 14 Pounds
1 Kg = 2.2 Pounds
1 Feet = 30 Centimetres
1 Inch = 2.5 Centimetres

instead of 1 Stone = 14 Pounds 1 Kg = 2.2 Pounds 1 Feet = 30 Centimetres 1 Inch = 2.5 Centimetres

fumei
10-18-2006, 08:01 AM
No....that does not help. I don't see anything relevant. I tried replacing your (dot) with ".", but it just gave me a opening page for Uploadoing.com.

Again...you are giving us text that does not really SAY anything. You need to be able to articulate what you are thinking. What was I supposed to see?

Try answering questions.

What "pages" are you talking about? Second page of what? The document? A multi-page userform?

lucas
10-18-2006, 08:02 AM
Hi kaiser,
Hit "Post Reply" at the bottom of the last thread....
then click on manage attachments.
You can post your actual document here on the forum...


ps here is your work.doc from the link you provided
see attached

kaiser soze
10-18-2006, 08:08 AM
im sorry but its not letting me do it as i have exceeded the forums limit.

my file size is 70kb and there only letting me upload 20

fumei
10-18-2006, 08:10 AM
Slow down! Slow down!

1. I still can not see anything yet.
2. I see no file I can download.
3. You have to learn to post explicit text.
4. Try to keep your posts to one request. Your first post was "pages". Now you are posting about messagebox displays. Keep to what you ask in your first post...especially when you have not received an answer yet!

However, as to your messagebox text - this is simple. You need to add a carriage return/line feed to your message string.MsgBox ("1 Stone = 14 Pounds" & vbCrLf & _
"1 Kg = 2.2 Pounds" & vbCrLf & _
"1 Feet = 30 Centimetres" & vbCrLf & _
"1 Inch = 2.5 Centimetres")vbCrLf is a built-in VBA instruction for Carriage return/Line feed.

You had better look up some information on using strings.

lucas
10-18-2006, 08:10 AM
You just have to zip it up.....
Gerry, I think kaiser is looking for validation of fields filled out before moving to the second userform...kaiser, please verify

kaiser soze
10-18-2006, 08:14 AM
Im sorry if i have confused you people. In word i have 2 userforms 1 is knows as userform1 and the other is knows as userform2. I want to click on "next" in userform1 and this shall open the userform2 page. All the radiobuttons/comboboxes/text boxes and check boxes are in userform1.

Hope that helps you

fumei
10-18-2006, 08:16 AM
Well, yes, I can see there is a request for validation.

Second userform? What second userform? Things are happening very quickly here. I seem to be a post behind things.

kaiser - validation is almost always a question of logic. Error trapping and validation is where (in a lot of cases) the most work goes. There are methods for good error trapping/validation, but the logic behind it - WHY this value is OK, but that value is not - has to come from you.

Steve, your post seems to indicate you have a file. Yes? You mention "attached", but there is none.

kaiser soze
10-18-2006, 08:19 AM
http://www.uploading.com/files/QG1S2372/Work.doc.html

Should work now, i have other validations on the first form but this one seems a little bit tricky.

lucas
10-18-2006, 08:24 AM
Hi Gerry,
I posted his file for him in post #7 of this thread.....I can see it and am wondering if there is a problem with the forum????

ps Gerry I edited the thread to add the attachment so you may have to refresh to see it...sorry.

fumei
10-18-2006, 08:25 AM
Darn, I still seem to be a wee bit behind. OK, confirmed - ther are two userforms. OK, so the logic for validation of the values on userform 1 needs to be done before userform 2 is displayed. That makes sense.

So do it. You will need (probably) some IF statements

If question 1 Button 1 selected then
do this
Else ' Button 2 selected
do that
End If

Or some Select Case statements, say for Question 5 (the combobox with items "yaddayadda", "blahblah", "Yogi Bear", "Santa Claus"):Select Case ComboBox5.Value
Case "yaddayadda"
' do whatever yaddayadda is supposed to mean to do
Case "blah blah"
' do whatever blahblah is supposed to mean to do
Case "Yogi Bear"
' do whatever Yogi Bear is supposed to mean to do
Case "Santa Claus"
' do whatever Santa Claus is supposed to mean to do
End SelectThese are just logic statements - which is what validation is all about.

kaiser soze
10-18-2006, 08:32 AM
I understand the Id and the Select Statements, But I just dont get how i could put everything together. For example I want it to check the first 2 Radio Buttons, AND then the next question which is a Combobox AND then the next question. Im asking this because at the moment if all fields are empty, and a radio button is selected in any random question the nex page will be shown when the button is clicked.

lucas
10-18-2006, 08:39 AM
by next page you mean userform 2

kaiser soze
10-18-2006, 08:41 AM
by next page you mean userform 2

yup, i mean userform2

kaiser soze
10-18-2006, 09:13 AM
i know if i can get the first question to work i shall be able to do the rest

so far i have this

If obmale.Value = False Then MsgBox ("Please Complete Question1")
Else
If obfemale.Value = False Then MsgBox ("Please Complete Question1")
End If

but it keeps throwin errors, any help please?

fumei
10-18-2006, 09:21 AM
Crap crap crap. Behind STILL!

OK, I have the file, but I am going to drop out of this thread. The userform is so large that it will not fit on my screen. The buttons are not visible at all, and there is no way to scroll to see them. This is poor design. Fortunately I always step into any stranger's code, so I could get out.

I could resize your form so I can see/use it, but frankly, I don't have the time to do that.

Why do you have the userform so big????? It may be OK on your system, but it is NOT reasonable to assume everyone will have the same screen resolution as you. I don't, and because of that, your userform is - even as it is - totally useless for me. I can't click the ReSet or Continue button, even if I wanted to. They are not visible on my screen. I can of course see them in the VBE, but that is hardly the point.

WHY are you using all those frames? I can use the use of most of them, but WHY are you putting textboxes in them to explain what to do? You can use the .Caption property of the frame to do this.

You are taking up a HUGE amount of real estate for this form.

Plus, a multipage would be a much better way of getting all these questions on a userform.

As for your validation - again, you have to build up the whole logic.

lucas
10-18-2006, 09:22 AM
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.

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

lucas
10-18-2006, 09:26 AM
As Gerry points out in post #20....your form could use some redesign but the code above will help you with the validation logic.

fumei
10-18-2006, 09:33 AM
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.If obmale.Value = False Then MsgBox ("Please Complete Question1")
Else
If obfemale.Value = False Then MsgBox ("Please Complete Question1")
End IfIf 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.

fumei
10-18-2006, 09:34 AM
Ahem......underscore character please.

lucas
10-18-2006, 09:39 AM
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.

fumei
10-18-2006, 10:50 AM
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.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

kaiser soze
10-18-2006, 11:38 AM
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.

kaiser soze
10-18-2006, 11:48 AM
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?

lucas
10-18-2006, 12:33 PM
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.

kaiser soze
10-18-2006, 12:37 PM
thanks for your help, i owe u 1

kaiser soze
10-18-2006, 01:10 PM
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

kaiser soze
10-18-2006, 01:25 PM
this is so weird, i just copyed and pasted the coding and its started working hmmmmmm. thanx

fumei
10-18-2006, 01:26 PM
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 mindThen 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).

lucas
10-18-2006, 01:27 PM
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.

fumei
10-18-2006, 02:10 PM
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!

lucas
10-18-2006, 03:16 PM
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.

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

fumei
10-18-2006, 03:58 PM
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?

kaiser soze
10-18-2006, 04:54 PM
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

lucas
10-18-2006, 05:27 PM
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.

kaiser soze
10-18-2006, 10:26 PM
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.

kaiser soze
10-19-2006, 05:14 AM
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

kaiser soze
10-19-2006, 09:04 AM
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?

kaiser soze
10-19-2006, 11:29 AM
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.

fumei
10-19-2006, 12:03 PM
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??

fumei
10-19-2006, 12:11 PM
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.

kaiser soze
10-19-2006, 02:34 PM
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.

kaiser soze
10-19-2006, 02:50 PM
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?

fumei
10-19-2006, 10:21 PM
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.

kaiser soze
10-20-2006, 03:50 AM
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

fumei
10-20-2006, 07:18 AM
Good luck.

kaiser soze
10-20-2006, 09:32 AM
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?

fumei
10-20-2006, 10:17 AM
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: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:=agesHmmmm, 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.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
Select Case checks the cases, and ONLY processes the case that matches.

Further, you don't need to put the text in again.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

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 doSelection.TypeText Text:= oboagesas 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.

kaiser soze
10-20-2006, 11:43 AM
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?

kaiser soze
10-21-2006, 04:31 AM
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

fumei
10-22-2006, 04:47 PM
THINK!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 SelectI 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.

kaiser soze
10-26-2006, 01:57 PM
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.

fumei
10-26-2006, 01:59 PM
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.

kaiser soze
10-27-2006, 04:02 AM
dont worry i somehow got it all done, thanks for all your help i appreciated it bud.. :thumb