PDA

View Full Version : conditional drop-down form field problem...



AnGeL
03-22-2007, 01:41 PM
Hi;

i would like to make a conditional drop-down list for my form which means the content of dropdown B will depend on what is selected on dropdown A .
since i dont have an experience in VBA, i do some search,and after searching i found this code:


Option Explicit

Sub FirstFieldExit()

Dim Fruits(3) As String
Dim Veggies(3) As String
Dim Meat(3) As String
Dim i As Integer
Dim var

'Create your multiple lists here (dropdown #2)

Fruits(0) = "Apples"
Fruits(1) = "Peaches"
Fruits(2) = "Pears"
Fruits(3) = "Bananas"

Veggies(0) = "Green Beans"
Veggies(1) = "Corn"
Veggies(2) = "Lettuce"
Veggies(3) = "Squash"

Meat(0) = "Beef"
Meat(1) = "Chicken"
Meat(2) = "Pork"
Meat(3) = "Veal"

'Use the value of the dropdown to case select condition

Select Case ActiveDocument.FormFields("Dropdown1").DropDown.Value


'For each one of these cases, change the " .Add Name:= part to be
'one of the options in your #1 dropdown box

Case 1
ActiveDocument.FormFields("Result").DropDown.ListEntries.Clear
For var = 1 To 4
ActiveDocument.FormFields("Result").DropDown.ListEntries.Add Name:=Fruits(i)
i = i + 1
Next
ActiveDocument.FormFields("Result").DropDown.Value = 1
Case 2
ActiveDocument.FormFields("Result").DropDown.ListEntries.Clear
For var = 1 To 4
ActiveDocument.FormFields("Result").DropDown.ListEntries.Add Name:=Veggies(i)
i = i + 1
Next
ActiveDocument.FormFields("Result").DropDown.Value = 1
Case 3
ActiveDocument.FormFields("Result").DropDown.ListEntries.Clear
For var = 1 To 4
ActiveDocument.FormFields("Result").DropDown.ListEntries.Add Name:=Meat(i)
i = i + 1
Next
ActiveDocument.FormFields("Result").DropDown.Value = 1
End Select
End Sub


i use it and it's work with me..
but there is a problem, in my form the content of the first drop down field not only one word ( e.g: in the code above, the first drop down field is "Fruits" BUT i want it for example "Fruits and veggies"), how can i modify the code to solve my problem ??

Need ur help :o:

Thanx

fumei
03-23-2007, 06:17 AM
That is my code, so I will answer this.

The first formfield HAS entries of Fruits, Veggies, Meat. The code above does nothing to the first formfield.

The code above affects the SECOND formfield. The item list for the second formfield is set by the selected choice from the first formfield.

OK. So the first formfield HAS:

Fruits
Veggies
Meat

You want to change it to:

Fruits and Veggies
Meat

So there is two choices, not three? OK. Go into the first formfield and change the items.

This will mess up your code though. The code will still try to populate the second formfield with three possible choices coming from the first formfield.

If you just change the item list in formfield 1, then:

Fruits and Veggies - will populate formfield 2 with Fruits.
Meat - will populate formfield 2 with Veggies

Unless you change the code above, you will never get Meat.

Perhaps if you asked and explained what you actually want.

AnGeL
03-23-2007, 10:21 AM
in my form i need the first form field:
-Bachelor of business adminstration
-Bachelor of arts
-Bachelor of science .... etc
the second drop down menu will change depend on the first choice
e.g:
if the user choose Bachelor of businees adminstartion from the first
drop dowm menu,the following choices will appear on the second drop down menu:
acounting, economics, managment .... etc
on my code the first drop down menu consists of more than one word
( e.g: Bachelor of arts = 3 words with spaces)
so, i can't declare it as an array...
how can i modify the code to solve my problem ??
i hope you understand my problem :)

fumei
03-23-2007, 12:19 PM
Still not following.

For the first formfield, it is totally irrelevant if it is one word, or three words.

I will repeat, the code has NOTHING to do with the items in the first dropdown. The code responds to whatever is selected from that list. But the actually list is irrelevant. In fact, the actual text is irrelevant.

The choice of what to populate the second formfield is determine by the [number[/b] in the first formfield list.

Case 1 - the first item, text is irrelevant

Case 2 - the second item, text is irrelevant

So, ummm, change the properties of the first formfield, and put in the text you want, including spaces.

Array??? No array needed.

on my code the first drop down menu consists of more than one word
( e.g: Bachelor of arts = 3 words with spaces) You may have something like that in code somewhere, but you did NOT include it in the code you posted.

And who says you can not have spaced strings in an array?????
BigWigs(0) = "Chairman of the Board"
BigWigs(1) = "Chief Financial Officer"
BigWigs(2) = "The guy with your paycheck"
BigWigs(3) = "Yogi Bear on steroids"

AnGeL
03-23-2007, 01:25 PM
what i mean, i need to declare it as Dim bachlor of business(3)
bachelor of business(0) = "economics"
bachelor of business(1) = "accounting"
bachelor of business(2) = "information system"

but this way not work (because of the spaces) unlesss i declare it as:

Dim BachelorOfBusiness(3)
OR
Dim bachelor_of_Business(3)

but when i declare it in these ways,
it will be displayed like this >>>> "bachelor_of_business" in the form field..
and i want it to be displayed as "Bachelor of Business" not "bachelor_of_business"

fumei
03-23-2007, 03:44 PM
Ummm, what is with the change to that awful colour?

Sorry, but you are confusing a few things.

The array name is not the item name! It may be, but it does not have to be. They have nothing to do with each other.

Let me go back to the original example.

You have formfieldONE. It displays "Fruits", "Veggies", "Meats". It fruit is selected, then the second formfield is populated from the array Fruits(3).

OK?

Let's go to that first formfield, right click and select Properties. Delete "Fruits" from the list. Add "Blkahsdkhk tie buttomas".

FormfieldONE will now display:

Blkahsdkhk tie buttomas
Veggies
Meats

Selecting Blkahsdkhk tie buttomas will STILL populate formfieldTWO from the array Fruits.

Let's go back to displaying:

Fruits
Veggies
Meats

Let's change the array from:

Fruits(0) = "Apples"
Fruits(1) = "Peaches"
Fruits(2) = "Pears"
Fruits(3) = "Bananas"

to:

jedgajgjtzx45asdw(0) = "Apples"
jedgajgjtzx45asdw(1) = "Peaches"
jedgajgjtzx45asdw(2) = "Pears"
jedgajgjtzx45asdw(3) = "Bananas"
Select Case ActiveDocument.FormFields("Dropdown1").DropDown.Value


'For each one of these cases, change the " .Add Name:= part to be
'one of the options in your #1 dropdown box

Case 1
ActiveDocument.FormFields("Result").DropDown.ListEntries.Clear
For var = 1 To 4
ActiveDocument.FormFields("Result").DropDown.ListEntries.Add
Name:=jedgajgjtzx45asdw(i)
i = i + 1
Next

See. The result is exactly the same. Do not confuse the array with the displayed item.

The point being - you do NOT, repeat NOT, "need to declare it as Dim Bachelor of business(3). As you can se...it does not work. But you do not need to anyway!

AND, repeat AND, the name of the array is NOT the item displayed name.

With the original code, let's say Fruits was selected.
Let's say the Fruits array was:

Fruits(0) = "Apples and hayseed pie"
Fruits(1) = "Peaches and cream"
Fruits(2) = "Pears and bears and lions and tigers"
Fruits(3) = "Bananas - yes we have no"

THOSE would be populated into the item list for formfieldTWO.

fumei
03-23-2007, 03:48 PM
You could have:Dim BachBusiness(2)
BachBusiness(0) = "economics"
BachBusiness(1) = "accounting"
BachBusiness(2) = "information systems"

and still have "Bachelor of Business" displayed in the list.

The list is populated from an array content...not the array NAME.

AnGeL
03-24-2007, 11:27 AM
ahaaaa.. i thought that the array name & the item name should be identical :o:
thank you for the explanation
thanks alo0o0o0oT fumei

fumei
03-26-2007, 05:32 AM
Nope. Two different things. They COULD be named the same, but they do not have to be.

It is arguably a toss up regarding best practices. I tend to not make different things have the same names. In fact, the compiler will complain if you have objects with the same name. However, in this case one was an object, the other an array. Still, better to keep names individual.

There is, however, good reason for having similar names. A good example is when you have textboxes on a userform that match to formfields in a document. I tend to use exactly the same name, but with the prefix "txt" ( the standard prefix for textboxes) for the textboxes on the userform.

txtFirstName (textbox - userform) matches to FirstName (formfield - document)

txtLastName (textbox - userform) matches to LastName (formfield - document)

That way it is easy to simply strip off the "txt" from the textbox name, to get the appropriate name of the formfield to use. Like this:Dim oCtl As Control
Dim StrBM As String
For Each oCtl In Me.Controls
If TypeOf oCtl Is MSforms.TextBox Then
strBM = Right(oCtl.Name, Len(Octl.Name) - 3)
ActiveDocument.Bookmarks(strBM).Result = _
oCtl.Text
End If
NextVoila! The string variable is set as the textbox control name, minus the first 3 letters - "txt". This can now be used as the formfield name.

This only works if you use standardized naming.