Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems passing array using ShowModalDialog 1

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hello again,

I'm have some difficulty passing the contents of an array from the parent to child window.

I get an Object required '[string: "John Smith"]' error message when opening the child form.

main.hta code snippet:
Code:
ElseIf arg = "openDGPage" Then
		'oDictDialogArgs.Add "DisplayName", strDisplayName
		'oDictDialogArgs.Add "cbCitrix",document.getElementById("cbCitrix").value
		cbCtx = document.getElementById("cbCitrix").value
		wArg = Array(cbCtx,strDisplayName)
		Set retDG = ShowModalDialog("pages/DistributionGroupPage.hta",wArg,_
"toolbar=0,location=0,status=0,menubar=0,_
scrollbars=1,resizable=1,dialogHeight:1250px;dialogWidth:550px,top=200,left=200")
		If IsEmpty(retDG)Then
			MsgBox "You haven't selected any Distribution Groups!"
		ElseIf Not IsEmpty(retDG) Then
			For Each x In retDG
				'MsgBox "Item: " & x & " " & retDG(x)
				oSGDict2.Add "dg" & x, retDG(x)
			Next
		End If

And the child (modal) page:
Code:
Sub LoadPage()
        Set wArgs = window.dialogArguments
	DataAreaUser.innerHTML=wArgs(1)
	MsgBox wArgs(0)
End Sub

The error is in the Set wArgs = window.dialogArguments line
If I remove 'Set' then the error is Type mismatch: 'wArgs'.

I'm a little stuck and being VBScript, there aren't alot of examples. In fact I only found one and I can't see why mine doesn't work. Example here.

Help!!!

Many thanks as always.
 
[1] Dialog returns an array only.
>Set retDG = ShowModalDialog("pages/DistributionGroupPage.hta",wArg,_
"toolbar=0,location=0,status=0,menubar=0,_
scrollbars=1,resizable=1,dialogHeight:1250px;dialogWidth:550px,top=200,left=200")

[tt][highlight]r[/highlight]etDG = ShowModalDialog("pages/DistributionGroupPage.hta",wArg,_
"toolbar=0,location=0,status=0,menubar=0,[red]" &[/red]_
[red]"[/red]scrollbars=1,resizable=1,dialogHeight:1250px;dialogWidth:550px,top=200,left=200")[/tt]

[2] You pass to the dialog an array only.
>Set wArgs = window.dialogArguments
[tt][highlight]w[/highlight]Args = window.dialogArguments[/tt]
 
Thanks Tsuji,

I am obviously in one of my 'thick' moods again!!

I've removed 'Set' from both retDG= and wArgs= but get the same error: Type mismatch: 'wArgs' on the line: MsgBox wArgs(1).

When you say "You pass to the dialog an array only", do you mean that I have to 'break down' the array object. For Example:

Code:
For i=0 To UBound(wArgs)
   MsgBox i
Next

The error I get with the above is another:
Type mismatch: 'UBound'

Or do you mean I have to use the same array for passing arguments to the child and back to the parent (via redim())?

Sorry to be a pain, but I'm just not understanding.

Thanks again.

 
[2.1] Here I have no doubt it is the structure to appear in LoadPage sub of the dialog. You have shown clearly you pass an array to the dialog. Hence, I have zero doubt.

[1.1] Here, I am not as sure as you have subsequently use a structure like "for x in retDG" and then using the pair like (x, retDG(x)). Could you show you window.returnValue line in the dialog expliciting the nature of the right-hand-side of that line?

[1.2] The line continuation correct is a must, if you overlook the colored red part.
 
[1.1.1] If you are passing a dictionary object back, then you have to use keyword set. Hence, it would be cloaser to your original apart from the line-continuation correction.
[tt]
'retDG being a dictionary object
set retDG = ShowModalDialog("pages/DistributionGroupPage.hta",wArg,_
"toolbar=0,location=0,status=0,menubar=0," &_
"scrollbars=1,resizable=1,dialogHeight:1250px;dialogWidth:550px,top=200,left=200")
[/tt]
 
[1.1.2] It looks more likely you're passing a dictionary back. Hence, I will stand behind my [1.1.1] more. In that case, "for each x in retDG" is fine. Nevertheless, "for each x in retDG.keys" would be clearer in the intention than the short form.

[1.1.3] If that's the case, testing isempty() is not a priori a problem, but it is inadequate because if retDG is nothing, it is not empty but calling "for each x in retDG" when retDG is nothing is a runtime error. You need to replace the block by this.
[tt]
If [red]retDG is nothing[/red] Then
MsgBox "You haven't selected any Distribution Groups!"
ElseIf [red] Not (retDG is nothing)[/red] Then 'or spare it using else simply
For Each x In retDG
'MsgBox "Item: " & x & " " & retDG(x)
oSGDict2.Add "dg" & x, retDG(x)
Next
End If
[/tt]
 
Thank you so much for your help, however I have a slight confession. I did say I had my 'thick' head on. I was modifying the code on the wrong modal page. Hence the problems persistence.

Once I sussed that it worked fine. D'oh. I've now cleaned up the code a little, creating some sub routines to make sure everything is a little more readable.

You have hit on one of the problems I am facing, and that is how to find out if a dictionary object is empty. I have used your suggestion of 'Is Nothing', but even if I don't tick a checkbox on the modal dialog, retSG Is Nothing always returns false. i.e there is something in it. If I pass it to a message box, nothing is present. I've tried Is Null and Is Empty, but these return "Object Required".

Basically, the modal dialog dynamically populates with a list of all the AD security groups; checkbox, name and description. I need the code to make sure atleast one checkbox is checked. If true then allow modal commitPage.hta (a summary page with a commit button) or if false open SecurityGroupPage.hta.

I have tried making the doSG() function return the contents of retSG, but of course that opens the SG page.

I created the following function that is called from the pressing of the "Create User..." button on the parent dialog:

Code:
Function doSG()
	oSGDict.RemoveAll
	strDisplayName = document.getElementById("txtFirstName").value & " " & document.getElementById("txtLastName").value
	cbCtx = document.getElementById("cbCitrix").value
	sgArg = Array(cbCtx,strDisplayName)
	Set retSG = ShowModalDialog("pages/SecurityGroupsPage.hta",sgArg,_
							"toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1," &_
							"dialogHeight:1000px;dialogWidth:1000px,top=200,left=200,unadorned:1")
	If Not retSG Is Nothing Then
		For Each v In retSG
			oSGDict.Add v, retSG(v)
		Next
	Else 'doesn't do anything even if = nothing??
		MsgBox "You haven't selected any Security Groups!"
	End If
	'Set doSG = retSG
End Function

And it is called with this code:

Code:
If doSG Is Nothing Then
  MsgBox "Empty"
Else
  MsgBox "Something there"
End If

And it always comes up with "Something there". Well actually this code above just opens the SG page.

Is there a correct way of determining if a dictionary object is totally empty? object.exists() (object.exists(null)??)doesn't work as the key name could be one of 200 Security group names dynamically generated into an HTML table.

I was thinking of something like a do while retSG is nothing, show the SG page. But I couldn't work out the stucture.

I am rambling. I probably need to take a step back and re-evaluate the code structure.

Any tips would be greatly appreciated as always.

Once again, thank you Tsuji and sorry for being such an idiot.




 
[3] You still did not show the window.returnvalue line in the dialog ([1.1]). But, it is up to you to show or not, I'm not insisting.

[4] An empty dictionary in the sense of no entry in it should be tested by the count property.
[tt]
if doSG.count=0 then 'doSG dictionary object
MsgBox "Empty"
Else
MsgBox "Something there"
End If
[/tt]
 
Sorry, an omission on my part. The window.returnValue code for the child dialog: (This is fired from an 'OK' button.)

Code:
Function CloseWindow()    
    
    Set chkbox = document.getElementById("sgForm")
    Set oSgDict = CreateObject("Scripting.Dictionary")
    i = 0
    For Each box In chkbox.chkctrl
    		If box.checked Then
    			i = i + 1
    			oSgDict.Add i,box.value
    		End If
    	'Put above into dict or array
    Next
    Window.returnValue = oSgDict
    window.Close 
End Function

Thanks
 
[5] This is where I suspect can cause problem too, that's why I asked to show. And it confirms my suspicion.
>Window.returnValue = oSgDict
[tt][red]set[/red] Window.returnValue = oSgDict[/tt]
 
Ok, I've had some time to get back to my little script.

I've cleaned it up a little and put the calls to the modal dialogs in their own functions. The function is called once when the user clicks a 'dedicated' button to complete the form, then again, when the Commit button is pressed. The call from commit is to check the user hasn't forgotten to complete the form - a check.

Initially the 'object' will be empty / not exist, but assuming the user has actually completed the form clicking commit, then the 'object' should exist. But, I must be checking for the wrong thing as it is always seen as empty.

Below is the function's code, in the parent window. The call to this function is in the same window.
Code:
Function doSG()
	strDisplayName = document.getElementById("txtFirstName").value & " " & document.getElementById("txtLastName").value
	cbCtx = document.getElementById("cbCitrix").value
	sgArg = Array(cbCtx,strDisplayName)
	'Set retSG = Nothing
	
	If Not IsObject(retSG)Then
	'If retSG.count=0 Then
	'If retSG Is Nothing Then
	'If IsNull(retSG) Then
	 
		MsgBox "retSG is empty"
		Set retSG = ShowModalDialog("pages/SecurityGroupsPage.hta",sgArg,_ 
							"toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1," &_
							"dialogHeight:1000px;dialogWidth:1000px,top=200,left=200,unadorned:1")
		For Each x In retSG
			oSGDict.Add "sg" & x, retSG(x)
		Next
	Else
		For Each x In retSG
			oSGDict.Add "sg" & x, retSG(x)
		Next
	End If
		
End Function

The problem is in the line "If Not IsObject(retSG)Then". If the user has completed the form on the modal dialog, then a dictionary object should be returned thus making it an object? Now this must happen becuase the oSGDict is populated correctly, however, If not IsObject(retSG) always evaluated to true (empty).

Tsju very kindly suggested I used the (commented) line:
"If retSG.count=0 Then" to determin if the dictionary is empty, however, before then I have to determin if the object exists? As this retur an error "object required".


The modal (child) dialog returnValue code is:
Code:
Function CloseWindow()    
    
    Set chkbox = document.getElementById("sgForm")
    Set oSgDict = CreateObject("Scripting.Dictionary")
    i = 0
    For Each box In chkbox.chkctrl
    		If box.checked Then
    			i = i + 1
    			oSgDict.Add i,box.value
    		End If
    	'Put above into dict or array
    Next
    Set Window.returnValue = oSgDict
    window.Close 
End Function

I'm stuck and not sure how to get this to work.

Any help would be greatly apreciated (as always).

Kind Regards.

 
>If Not IsObject(retSG)Then
> 'etc.[/i

>End If
What for? If there is option explicit, just dim retSG; if not, dim retSG or take it as it comes (returning from the dialog.
 
Also you check retSG.count only after it is returned. Make sense?
 
Yes, retSG.count bit makes sense.

No option explicit I am afraid :-(

How do I call doSG and if retSG is not empty then then don't open the SG dialog again?

For example:

Step 1
Click "Security Groups..." Button --> Open Dialog --> Tick some boxes --> Close SG dialog --> ReturnValue to retSG

Step 2
Click "Commit..." Button --> Check retSG is not null / empty / count (doSG()) --> if not empty then show commit dialog OR if empty then show SG dialog.

Is this possible from one function or am I barking up the wrong tree?

Thanks again.
 
Just take out the test not isobject() if ... end if. oSGDict does not coincide with the function name, if you want to return that dictionary for other sub/function to use. Clean up the logic and simplify, simplify. Nothing deep there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top