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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stuck reading the selected item in a webform droplist

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
I've got a WebBrowser object showing a webpage that contains a droplist (so-called choice box). The relevant HTML for the page is:

Code:
<FORM>
<select name="att" id="att">
<option>Page 1</option>
<option>Page 2</option>
</select>
</FORM>

In VB I need to read the value of whatever is selected by the user, but I've tried so many combinations of reading it and I'm stumped. Here's what I've currently got:

Code:
Dim objDoc as HTMLDocument
Set objDoc = WebBrowser1.Document
N$ = objDoc.getElementById("att").getAttribute("value")

However, this returns "Object variable or With variable not set". Any tips or solutions would be very useful.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Andy,
You were almost there
try

Code:
Dim objDoc as HTMLDocument
Set objDoc = WebBrowser1.Document
'N$ = objDoc.getElementById("att").getAttribute("value")
N$ = objDoc.All("att").Value

Learn from others' mistakes. You could not live long enough to make them all yourself
-- Hyman George Rickover (1900-86)
 
OK thanks, I'll give it a go.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
[1]
>However, this returns "Object variable or With variable not set".
Error of this kind at that general position usually means either [a] the webbrowser1 did not load any page or it had not found the page or [c] it was still loading the page (with readystate not yet equal to 4).

[1.1] This it is the first two causes, check the page and path fed to the navigate method.

[1.2] If it is the last cause, you've to make a wait until webbrowser1 has returned to readystate=4, something like this before proceed to reference to its document.
[tt]
do while webbrowser1.readystate<>4
doevents
loop
[/tt]
[2] If the page contains the fragment exactly as shown, then the value would always return empty. Your line is a workable option. Another equivalent can be simply like this.
[tt]
N$ = objDoc.getElementById("att").value
[/tt]
[2.1] But I guess you'd like to get page 1, page 2 etc... In that case it would be this.
[tt]
N$ = objDoc.getElementById("att").[red]text[/red]
[/tt]
[3] However, html select-one element has the flip-side of the story as well, namely submitted to a server and communicate the data to it. In that case, .value is needed and would be consumed by the server. Hence, the page fragment should better be scripted like this for that purpose.
[tt]
<option [blue]value="Page 1"[/blue]>Page 1</option>
<option [blue]value="Page 2"[/blue]>Page 2</option>
[/tt]
In that case, the .value restores its utility client-side as your original code line intended to retrieve.
 
Amendment

[2.1-a] As to the text-way, I meant this.
>[self]N$ = objDoc.getElementById("att").text
[tt]N$ = objDoc.getElementById("att")[red].options(objDoc.getElementById("att").selectedIndex)[/red].text[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top