Skip to main content

Multiple Drop Down Combo Boxes

I have a dialog that has multiple drop down combo boxes. These combo boxes share a similar attached list and I am trying to figure out a way to prevent the user from selecting the same value on multiple combo boxes. Basically a value can only be used once and I am trying to write the code to check if the user has duplicated a selection. Any advice/help anyone can offer is really appreciated.

Brian Element Mon, 08/29/2016 - 07:36

Hi Chris,

You might want to check out my Same-Same-Same test as it has the user select multiple fields but not allow them to select the same field twice - http://ideascripting.com/Same-Same-Same-Test

I basically put the selections in an array and then loop through the array checking to see if an item has been used twice, if so I give an error and ask the user to correct it.

Here is the looping code.

'****************************************************************************************************
' Name:  checkForDuplicateRows
' Description: Routine to validate the each field in the dropdown is only picked once.
' Accepts:  nothing
' returns:  returns true with the field is selected twice
'****************************************************************************************************
Function checkForDuplicateRows() As Boolean
 Dim i As Integer
 Dim j As Integer
 
 checkForDuplicateRows = false
 For i = 1 To 8
  If keyField(i) <> "" Then
   For j = 1 To 8
    If keyField(j) <> "" Then
     If i <> j Then
      If keyField(i) = keyField(j) Then
       checkForDuplicateRows = true
      End If
     End If
    End If
   Next j
  End If
 Next i
 
End Function

It check to make sure that a user has made a selection as only the first selection box needs to be selected.  It then compares that it hasn't been selected twice but it skips if it is the same item (if i <> j).

Hopefully this will help you out a bit.

Brian

Brian Element Fri, 09/02/2016 - 12:05

In reply to by chdarc

Hi Chris,

Glad that this helped.

It is strange that the email I received contained more information than what was posted.

You had a question on the problem with:

Dim Preserve files(UBound(files)+1)

The problem is that it should actually be:

ReDim Preserve files(UBound(files)+1)

What happens in the editor is if you don't properly define it somewhere in the code it changes the ReDim to Dim, stupid, but that is what the editor does.  So what you are missing somewhere in your global variables is:

Dim files() as string

That should fix the problem, you will have to change the Dim to Redim.  Another time this happens is if you have something like this:

Dim files() as string 'this is for function xyz

When the editor sees the word function in the comment it gets confused, so if you are defining an array never use the word function as a comment, I ended up needing the help of IDEA to figure out what the problem was. So this is a known bug that will probably not get fixed as it is not a high priority.

Brian

chdarc Wed, 09/07/2016 - 14:27

Thank you for the reply. 
I have a quick follow up question for you. 
I have been able to use the suggestion to ensure the user does not duplicate selections; however, when the user makes selections I want to set that to a variable. The script will later then print this variable value to a txt file. 
The problem I am having is that no matter what it returns a script value of nothing. Here is the function: 
***************************************************************************
Dim Co As String
Dim Jrnl As String
Dim Ref As String
Dim Cntrl As String
Dim ANUM As String
Dim Amt As String
Dim ADate As String
Dim PDate As String
Dim Cntrl2 As String
Dim DDesc As String
Dim Acc As String
Dim ADesc As String
Dim PPF As String
Dim Unadj As String
Dim SubG As String
Dim SubD As String
Dim FIS As String
 
Dim tablegl As Object 
Dim tablewtb As Object 
Dim fieldsgl As Integer 
Dim fieldswtb As Integer 
Dim fieldx As Object
Dim fieldw As Object
Dim keyField(10) As String
Dim wtbField(6) As String
Function DatabaseMap()
  Dim DMM As DMMenu
Dim button As Integer
Dim exitDialog As Boolean
Dim Found As Boolean
Dim i As Integer
Dim OneSelected As Boolean
 
ReDim ListBoxGL$(0)
ReDim ListBoxGLN$(0)
ReDim ListBoxGLD$(0)
j=0 
w=0
d=0
For i =1 To fieldsgl 
Set fieldx=tablegl.getfieldat(i)
If fieldx.ischaracter Then 
ReDim preserve ListBoxGL$(UBound(ListBoxGL$)+2)
ListBoxGL$(UBound(ListBoxGL$))=fieldx.name
j=j+1 
End If 
If fieldx.isnumeric Then 
ReDim preserve ListBoxGLN$(UBound(ListBoxGLN$)+2)
ListBoxGLN$(UBound(ListBoxGLN$))=fieldx.name
w=w+1
End If 
If fieldx.isdate Then 
ReDim preserve ListBoxGLD$(UBound(ListBoxGLD$)+2)
ListBoxGLD$(UBound(ListBoxGLD$))=fieldx.name
d=d+1
End If 
Next i 
 
ReDim ListBoxWTB(0)
ReDim ListBoxWTBN$(0)
j=0 
w=0
For i =1 To fieldswtb 
Set fieldw=tablewtb.getfieldat(i)
If fieldw.ischaracter Then 
ReDim preserve ListBoxWTB(UBound(ListBoxWTB)+2)
ListBoxWTB(UBound(ListBoxWTB))=fieldw.name
j=j+1 
End If 
If fieldw.isnumeric Then 
ReDim preserve ListBoxWTBN$(UBound(ListBoxWTBN$)+2)
ListBoxWTBN$(UBound(ListBoxWTBN$))=fieldw.name
w=w+1
End If 
Next i 
 
Call sortArray(ListBoxGL$())
Call sortArray(ListBoxGLN$())
Call sortArray(ListBoxGLD$())
Call sortArray(ListBoxWTB$())
Call sortArray(ListBoxWTBN$())
 
 
 
 
 
Do 
button=Dialog(DMM)
 
Select Case button 
 
Case 1 'User done with mapping
 
FIS=DMM.TextBox1
If FIS="" Then 
MsgBox "Please enter first Income Statement subgroup number"
End If 
 
Co=ListBoxGL$(DMM.DropListBox7)
Jrnl=ListBoxGL$(DMM.DropListBox8)
Ref=ListBoxGL$(DMM.DropListBox9)
Cntrl=ListBoxGL$(DMM.DropListBox10)
ANUM=ListBoxGL$(DMM.DropListBox11) 
 
keyField(1)=ListBoxGL$(DMM.DropListBox7)
keyField(2)=ListBoxGL$(DMM.DropListBox8)
keyField(3)=ListBoxGL$(DMM.DropListBox9)
keyField(4)=ListBoxGL$(DMM.DropListBox10)
keyField(5)=ListBoxGL$(DMM.DropListBox11)
keyField(6)=ListBoxGL$(DMM.DropListBox14)
keyField(7)=ListBoxGL$(DMM.DropListBox15)
keyField(8)=ListBoxGLN$(DMM.DropListBox16)
keyField(9)=ListBoxGLD$(DMM.DropListBox12)
keyField(10)=ListBoxGLD$(DMM.DropListBox13)
wtbField(1)=ListBoxWTB(DMM.DropListBox1)
wtbField(2)=ListBoxWTB(DMM.DropListBox2)
wtbField(3)=ListBoxWTBN$(DMM.DropListBox3)
wtbField(4)=ListBoxWTBN$(DMM.DropListBox4)
wtbField(5)=ListBoxWTB(DMM.DropListBox5)
wtbField(6)=ListBoxWTB(DMM.DropListBox6)
OneSelected=TRUE
 
For i = 1 To 10 
If keyField(i)="Select one" Then 
MsgBox "Please select a field for each reference", MB_ICONEXCLAMATION, "Error"
OneSelected=FALSE
GoTo CheckDup
End If 
Next i 
CheckDup:
If checkForDuplicateRows() Then
MsgBox "You cannot use the same field twice for the General Ledger File", MB_ICONEXCLAMATION, "Error"
Else 
If OneSelected=TRUE Then 
exitDialog=True
End If 
End If 
 
For i = 1 To 6 
If wtbField(i)="Select one" Then 
MsgBox "Please select a field for each reference", MB_ICONEXCLAMATION, "Error"
OneSelected=FALSE
GoTo CheckDup2
End If 
Next i 
CheckDup2:
If checkForDuplicateRowsWTB() Then
MsgBox "You cannot use the same field twice for the Working Trail Balance File", MB_ICONEXCLAMATION, "Error"
OneSelected=FALSE
exitDialog=False
Else 
If OneSelected=TRUE Then 
exitDialog=True
End If 
End If 
 
Case 0
exitDialog=TRUE
        Exit Function
 
End Select
Loop While exitDialog=FALSE
 
Call CreateProjectFile()
End Function 
Function CreateProjectFile()
 
    Dim fso As Object 
Dim filepath As String
    Set fso = CreateObject("Scripting.FileSystemObject")
 
    Dim Fileout As Object
    Set Fileout = fso.CreateTextFile("C:\FTG\CRTB\Project Files\"& ProjectName &" Project File.txt",True, True)
    FileOut.Close
filepath="C:\FTG\CRTB\Project Files\"& ProjectName &" Project File.txt"
 
Open filepath For Output As #2
Print #2, ProjectName
Print #2, typeField
Print #2, filegl
Print #2, filewtb
Print #2, ""
Print #2, Co
Print #2, Jrnl
Print #2, Ref
Print #2, Cntrl
Print #2, ANUM
Print #2, Amt
Print #2, ADate
Print #2, PDate
Print #2, Cntrl2
Print #2, DDesc
Print #2, ""
Print #2, Acc
Print #2, ADesc
Print #2, PPF
Print #2, Unadj
Print #2, SubG
Print #2, SubD
Print #2, FIS
Close #2
 
MsgBox "Project File succesfully created!"
 
End Function
 
 
 
 
 

Brian Element Wed, 09/07/2016 - 18:56

In reply to by chdarc

Hi Chris,

Why don't you use the Scripting.FileSystemObject to print to the file?

So remove the filepath="C:\FTG\CRTB\Project Files\"& ProjectName &" Project File.txt"
 and

Open filepath For Output As #2

And change the print #2 to Fileout.WriteLine("ProjectName")

Then change the Close #2 to FileOut.Close and that should work.

Brian

chdarc Wed, 09/07/2016 - 21:58

In reply to by Brian Element

Yes that would work to print to the file. 
However, the problem I am having is getting the value to actual print. Everytime I select an item from the Drop List Box, it prints nothing for the value even though I have something selected. 

chdarc Wed, 09/07/2016 - 14:29

Sorry, quick additional note. 
I am able to print to the txt file the array (i.e. if item 2 is selected it will print "2"), but I need the actual value at number 2. 

Brian Element Thu, 09/08/2016 - 09:23

In reply to by chdarc

Hi Chris,

Ok, I looked at your script a bit closer and realized what you were trying to do and why it is not working. 

In IDEAScript there are two types of variables, local and global. Global can be seen and used by the entire script and local can only be used within the function they are defined.  In your case all the variables you are using in the CreateProjectFile function have been defined at a local level but within a different function, so the CreatProjectFile function does not have access to them.  Now there are several ways for you to get around this, probably the easiest is to take all those variables and change them to global variable but placing them before the sub main function.  Another way is to move your CreatProjectFile code into the function that is used to optain all those variables.  There are other ways to get around this but these two suggestions are probably the easiest and quickest to implement.

Hope this helps.

Brian