tracyms
asked on
Populate Text Box From Dropdown List
Hello Experts,
I have the below hta/vbscript that pulls values from a sql database and populates a dropdown list. How do I have the selection go to a text field? In other words, when the user selects from the dropdown list it should populate the text box. From searching, the closest I came was to an onchange event but I couldn't get it working. Also, can this code be used to create more than one dropdown list? Thanks in advance for your assistance!
Tracy
I have the below hta/vbscript that pulls values from a sql database and populates a dropdown list. How do I have the selection go to a text field? In other words, when the user selects from the dropdown list it should populate the text box. From searching, the closest I came was to an onchange event but I couldn't get it working. Also, can this code be used to create more than one dropdown list? Thanks in advance for your assistance!
<html>
<head>
<title>SQL DropDown Demo</title>
<hta:application
id="demo"
</hta>
<script type="text/vbscript">
Option Explicit
Sub Window_OnLoad
Dim oDD : Set oDD = document.GetElementById("Field1")
Dim oDb : Set oDb = CreateObject("ADODB.Connection")
oDb.Open "Provider=SQLOLEDB;Trusted_Connection=Yes;Server=myDatabaseServer;Database=myDatabase;"
Dim sQL : sQL = "SELECT * FROM [myDatabase].[USERS].[All_USR]"
Dim oRS : Set oRS = oDb.Execute(sQL)
Dim oOpt
Do Until oRS.EOF
Set oOpt = oDD.document.createElement("option")
oOpt.Text = oRS.Fields("USER_NAME").Value
oOpt.Value = oOpt.Text
oDD.Options.Add oOpt
oRS.MoveNext
Loop
End Sub
</script>
</head>
<body>
<form>
<select id="Field1">
</select>
// Input box to show value selected from dropdown
<input type="text" maxlength="30" size="40" id="Field1">
</form>
</body>
</html>
Tracy
You had before both select and text box as "Field1" so I called them Field1 and Text1. I added a second of each "Field2" and "Text2" with them both getting drop down values from SQL. Also showed adding extra values aswell as from SQL.
I have pointed the onchange event to a subroutine HandleChange which knows the field that changed and the value it changed to passed to it
So if you need to deal with more fields then you handle the change events for each in the same place.
I have pointed the onchange event to a subroutine HandleChange which knows the field that changed and the value it changed to passed to it
So if you need to deal with more fields then you handle the change events for each in the same place.
ASKER
Thanks Steve. The data isn't pulling in from the database, I only see your values in the dropdown - Extra Option 1 and Extra Option 2.
ASKER
Also, the database is read only and I don't get any errors when I bring up the hta...if that helps.
Sorry I left ' in this line from my test without sql:
'ReadSQLData
Remove the ' to let that subroutine run.
Steve
'ReadSQLData
Remove the ' to let that subroutine run.
Steve
ASKER
Ok thanks, did that. Now I'm getting an error Wrong number of arguments or invalid property assignment AddOption - at this line:
AddOption "Field1",oRS.Fields("USER_ NAME").Val ue
I looked up the error and this is one I came across:
You tried to assign a value to a read-only property, or you tried to assign a value to a property for which no Property Let procedure exists.
Source - Visual Basic for Applications Reference
AddOption "Field1",oRS.Fields("USER_
I looked up the error and this is one I came across:
You tried to assign a value to a read-only property, or you tried to assign a value to a property for which no Property Let procedure exists.
Source - Visual Basic for Applications Reference
Sorry didn't test that but without your SQL. Amend this to that and next line:
AddOption "Field1",oRS.Fields("USER_ NAME").Val ue,,oRS.Fi elds("USER _NAME").Va lue
I.e. it is posting two values to the sub one is used as the text i.e. what you see and the other as the value which is what is returned when selected.
In this case both are the same
Steve
AddOption "Field1",oRS.Fields("USER_
I.e. it is posting two values to the sub one is used as the text i.e. what you see and the other as the value which is what is returned when selected.
In this case both are the same
Steve
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
LOL - I hope you're not driving! Thanks - that worked. I changed to this:
AddOption "Field1",oRS.Fields("USER_ NAME").Val ue,oRS.Fie lds("USER_ NAME").Val ue
AddOption "Field2",oRS.Fields("USER_ FIRST_NAME ").Value,o RS.Fields( "USER_FIRS T_NAME").V alue
The only thing is the values (Extra Option 1,Extra Option 1) show at the bottom of the dropdowns instead of the top.
AddOption "Field1",oRS.Fields("USER_
AddOption "Field2",oRS.Fields("USER_
The only thing is the values (Extra Option 1,Extra Option 1) show at the bottom of the dropdowns instead of the top.
ASKER
In all fairness, I didn't ask for that so not a show-stopper. You answered my question, THANK YOU! :-)
ASKER
Thanks so much Steve!
No problem, was out with kids not driving :-). If you move the ReadSQLData line below where it as the extra entries they will be at the top.
At the moment SQL reads all entries and puts them in the options one at a time then the extras are added... You can add others before and/or after or none at all - I only suggested it as quite often I want a fixed list and another option even if it just N/A say.
Glad it helped!
Steve
At the moment SQL reads all entries and puts them in the options one at a time then the extras are added... You can add others before and/or after or none at all - I only suggested it as quite often I want a fixed list and another option even if it just N/A say.
Glad it helped!
Steve
ASKER
Yes, I didn't think about adding something to say "Select" for example as the first option of the dropdown until you added those extras to the code. Glad to hear about the kiddies, they are special and you get out what you put in. You're awesome - thanks again!
ASKER
Steve,
Would it be possible to make this a dependent list? Meaning, if you select the first, it filters your selection in the second:
AddOption "Field1",oRS.Fields("USER_ NAME").Val ue ---> Select USER_NAME
AddOption "Field2",oRS.Fields("FIRST _NAME").Va lue ---> Filters FIRST_NAME
Would it be possible to make this a dependent list? Meaning, if you select the first, it filters your selection in the second:
AddOption "Field1",oRS.Fields("USER_
AddOption "Field2",oRS.Fields("FIRST
Suggest make a new question, I'll have a look tomorow or Monday then.... or would suggets something along the lines of using the HandleChange subroutine for "Field1" say to re-populate "Field2" drop down using different info?
If you can explain more how and what you want filtered will explain.
Steve
If you can explain more how and what you want filtered will explain.
Steve
ASKER
Awesome - yes, I'll open a new question as I think I have something else I'd like to add. Thanks!
ASKER
https://www.experts-exchange.com/questions/29132963/Dependent-Drop-List-SQL-and-VBS.html#questionAdd
Have a good night, thanks!
Have a good night, thanks!
Steve
Open in new window