get value from selection in dropdown menu with variable name - vbscript

I have created a HTA page to get the status of workstations filtered from Active Directory.
Currently I added buttons to perform specific actions for the workstation that has the button next to is.
Since I'm adding more and more functions, I would like to use a dropdown box.
The size of the list with workstations is variable depending on the input made by the user, so I've given the name of the worstation to the dropdown box.
The problem I have now is that I cannot retreive the selected value in my VBScript code.
If I put listbox1.value I get the correct value in return.
If I use place the name of the listbox in a variable like this, it doesn't work.
strlistbox = "listbox1
listbox1.value
I get an error.
This is part of the code I'm using. In total I have 760 lines, so I won't paste everything here.
VBScript code:
Sub RunOption(strCPU)
dim lstname
dim intNumber
lstname = "list"&strCPU 'Value of lstname = listWPBED3702885
msgbox listWPBED3702885.value 'If I try this messagebox, I get the value of the selected option form the dropdown list names "listWPBED3702885"
intNumber = lstname.value 'This doesn't seem to work
Select Case list 'intNumber
Case 1 Do something
Case 2 Do something
Case 3 Do something
Case 4 Do something
End Select
End Sub
HTML code:
strHTML = strHTML & "<td width='1'><select class='select' size='1' title='list" & _
objRecordSet.Fields("Name") & "' name='list' onChange=""RunOption('" & _
objRecordSet.Fields("Name") & "')"">"& _
"<option value='0' selected>Options...</option>" & _
"<option value='1'>C:\ Drive</option>" & _
"<option value='2'>Computer Management</option>" & _
"<option value='3'>Event Viewer</option>" & _
"<option value='4'>MAC Address</option>" & _
"</select></td>"

You can't set a variable to a string and expect that string to behave like an element of your HTML code. You need to fetch the element referenced by the name or ID. The usual way is to define the element you want to use with a unique ID like this:
<select id='uniquevalue' ...>
...
</select>
and then get that element via getElementById():
Set listbox = document.getElementById("uniquevalue")
MsgBox listbox.value

Related

Expected Function or variable vb6.0

Hello i am coding in Visual Basic 6.0 and i have this error, i am trying to make a button inserting data in database.
Expected Function or variable
This is my code
Private Sub Command1_Click()
With ConString.Recordset.AddNew
!ID = txtID
!Emri = txtEmri
!Mbiemri = txtMbiemri
!Datelindja = txtData
!Telefon = !txtTelefon
!Gjinia = gender
!Punesuar = job
!Martese = cmbMartese
!Vendlindja = txtVendlindje
End With
End Sub
txtID is textbox
txtEmri is textbox
txtMbiemri is textbox
txtData is date picker
txtTelefon is textbox
gender is a string that takes value if a radio button is clicked
job is an integer if a checkbox is clicked
cmbMartese is combo box
txtVendlindje is textbox
Thank you in advance.
#JohnEason gave you the right answer. But there's another error in the line !Telefon = !txtTelefon, if I'm not mistaken. It should read !Telefon = txtTelefon, i.e. without the exclamation mark in front of txtTelefon.
I'm also not a big fan of that coding style. I prefer to not rely on default properties, but instead "spell out" the whole term, e.g.
With ConString.Recordset
.AddNew
!ID = txtID.Text
!Emri = txtEmri.Text
!Mbiemri = txtMbiemri.Text
!Datelindja = txtData.Text
!Telefon = txtTelefon.Text
' Since VB6 doesn't provide Intellisense for variables, I prefer to use some
' kind of hungarian notation for variable names, in this case sGender or strGender for a string variable
!Gjinia = gender
' Same here: iJob or intJob for an integer variable
!Punesuar = job
' You need to specify that you want the selected item of a combobox
!Martese = cmbMartese.List(cmbMartese.ListIndex)
!Vendlindja = txtVendlindje.Text
' If you're done setting column values and don't do so somewhere else,
' you also need to add a .Update statement here in order to finish the
' .AddNew action and actually persist the data to the database.
' .Update
End With
End Sub
As pointed out below by #JohnEason, there's also potentially an .Update statement missing within the With/End With block

ASP.Classic Getting value from input to POST parameter

Please excuse me if this question is dumb.
I need to get an input value and pass it in a POST parameter like follow:
SQL = "[proc_Happy]" & Request.Cookies("UserID")& "," & Request.Form("MYINPUTFIELD")
I have tried hardcoding MYINPUTFIELD with (it worked!):
SQL = "[proc_Happy]" & Request.Cookies("UserID")& "," & 54555152
My input in the asp page looks as follow:
<input type="number" name="MYINPUTFIELD " id="MYINPUTFIELD" value="<%=MYINPUTFIELD%>">
Things I have tried:
Getting the value with JS - failed.
Notes:
MYINPUTFIELD is an int
Is your input field in a form, i.e. is it between <form...> and </form> tags? If no, that's your problem right there. If yes, what does the <form...> tag have in it? Does it say method='get'? If yes, then your inputs are being put in the querystring, not the form object. For Request.Form(...) to work, your form needs to say method='post'.
If you need this code to work with both form methods, you can do something like
dim MyInputField
MyInputField = Request.Querystring("MyInputField")
If MyInputField = "" Then MyInputField = Request.Form("MyInputField")
'make the "OMGSQLINJECTION!!1!" people just go away already
'(note to such people: he's using a frigging stored procedure.)
If Not Isnumeric(MyInputField) Then
MyInputField = 0
End If
SQL = "[proc_Happy]" & Request.Cookies("UserID")& "," & MyInputField

For Loop gives Object Required Error

I am comparatively new to UFT as well as VB script.
I am trying to check innertext of divs inside For loop.
set getData = Browser("Browser").Page("Page").Object.getElementsByClassName("ClassName")
'Below line outputs 5'
msgbox getData.length-1
'output innertext for all these divs'
For i=0 to getData.length-1
msgbox getData(i).innertext
Next
This gives me Object Required Error on this line
msgbox getData(i).innertext
My first and 2nd element is blank while 3,4,5 are non-empty values.
When I write
msgbox getData(0).innertext
msgbox getData(1).innertext
msgbox getData(2).innertext
It gives me proper results
I further need to check this data against "Data" spreadsheet in UFT
Any pointers would be very much helpful.
Thanks,
Clarification needed:
As you would like to query div text, Is that any reason to use ClassName? If that so, you could use getElementsbyTagName instead getElementsByClassName.
However, I enhanced the code and adapting the query by any tag name option in the function. Here you go.
Dim objResultsDictionary
Set objResultsDictionary = GetTextContentFromHtmlTag(Browser("title:=Welcome: Mercury Tours").Page("title:=.*"),"div","")
Msgbox objResultsDictionary.Count
Result:8
Set objResultsDictionary = GetTextContentFromHtmlTag(Browser("title:=Welcome: Mercury Tours").Page("title:=.*"),"ClassName","mouseOut")
Msgbox objResultsDictionary.Count
Result = 11
Public Function GetTextContentFromHtmlTag(ByVal BrowserObject,ByVal TagName,ByVal TagValue)
Dim objDictionary
Dim objCollection
Set objDictionary = CreateObject("Scripting.Dictionary")
Select Case UCase(TagName)
Case "DIV"
Set objCollection = BrowserObject.Object.getElementsByTagName(TagName)
Case "CLASSNAME"
Set objCollection = BrowserObject.Object.getElementsByClassName(TagValue)
End Select
intDivCount = objCollection.Length
If intDivCount > 0 Then
For intCounter = 0 To intDivCount
If IsObject(objCollection(intCounter)) Then
strTagInnerText = objCollection(intCounter).innerText
If strTagInnerText <> "" Then
objDictionary.Add intCounter,strTagInnerText
End If
End If
Next
End If
Set GetTextContentFromHtmlTag = objDictionary
End Function
What you have to do:
You have to iterate the dictionary and get the innertext of the each tag.
Its giving you that error most probably because, there is various elements with the same class name other than the div or divs.
Try using some other properties.

Assign Attachment Field To Variable in Access 2010

I'm trying to understand how to work with the new Attachment field that is available in Access 2010. I would like to assign the value from the table directly into a variable. I know that I can do this if I use an intermediary form, but this seems like sloppy coding to rely on a form in order to grab a value from a table. Is there some way to grab what is in an attachment field and assign it directly to a variable? I have multiple instances where this would be handy for me. The first instance is I want to grab a photo stored in an attachment field to assign to the ribbon. A second instance is to load a company logo from a table into a variable and keep it in memory to use throughout the program as needed.
The code I have so far is this, but it gives me a type mismatch error:
Dim ParentRS As Recordset, ChildRS As Recordset, Img As Attachment
Set ParentRS = CurrentDb.OpenRecordset("SELECT * FROM LtblImg;", dbOpenSnapshot)
If ParentRS.RecordCount > 0 Then
Set ChildRS = ParentRS("Img").Value
If ChildRS.RecordCount > 0 Then
Set Img = ChildRS("FileData")
End If
ChildRS.Close
End If
ParentRS.Close
Yes, Dim Img As Attachment looks tempting, but Attachment (which is actually Access.Attachment) refers to an Attachment control that could be used on a form (just like Access.TextBox) and does not appear to be suitable for your intended purpose.
The only native VBA type for storing this sort of binary data is an array of Byte values, but often when dealing with byte arrays we wind up looping through and processing them byte-by-byte, which is tedious and inefficient.
You might consider using a binary ADODB.Stream object as your "variable". You could create a function to retrieve the attachment bytes and return them in a Stream like so
Option Compare Database
Option Explicit
Public Function GetLogoAsStream() As ADODB.Stream
Dim cdb As DAO.Database, rstMain As DAO.Recordset, rstAttach As DAO.Recordset2, fldAttach As DAO.Field2
' Project references required for early binding:
' Windows Script Host Object Model
' Microsoft ActiveX Data Objects 2.8 Library
Dim fso As FileSystemObject, tempFileSpec As String
Static strm As ADODB.Stream
If strm Is Nothing Then
Set fso = New FileSystemObject
tempFileSpec = fso.GetSpecialFolder(TemporaryFolder) & "\" & fso.GetTempName
Set fso = Nothing
Set cdb = CurrentDb
Set rstMain = cdb.OpenRecordset( _
"SELECT [AttachmentFiles] " & _
"FROM [AttachmentsTable] " & _
"WHERE [Description]='SO logo'", _
dbOpenSnapshot)
Set rstAttach = rstMain("AttachmentFiles").Value
' make sure we use the correct file extension
tempFileSpec = tempFileSpec & "." & rstAttach.Fields("FileType").Value
Set fldAttach = rstAttach.Fields("FileData")
fldAttach.SaveToFile tempFileSpec
Set fldAttach = Nothing
rstAttach.Close
Set rstAttach = Nothing
rstMain.Close
Set rstMain = Nothing
Set cdb = Nothing
Set strm = New ADODB.Stream
strm.Type = adTypeBinary
strm.Open
strm.LoadFromFile tempFileSpec
Kill tempFileSpec
End If
strm.Position = 0
Set GetLogoAsStream = strm
End Function
and then if you had, say, a Report like this with an empty Image control
and an On Load event procedure like this to load the Image control's .PictureData from your "variable" (actually a Function returning an ADODB.Stream)
Private Sub Report_Load()
Me.LogoImage.PictureData = GetLogoAsStream.Read
End Sub
it could produce something like this

VBA 6 + how to export variable to click button

I created the following VB6 code and I created two controls on my form - Combo1 (listbox) and Command3 (a button).
When I select an item from the Combo1 list I assign a string to the form scoped variable param and display it in a message box and then dismiss it.
But when I then click on the Command3 button and try to display the same param variable in a message box then there is no value stored.
Here's my code:
Dim param As String
Sub Form_load()
Combo1.AddItem "linux ver"
Combo1.AddItem "linux ver"
End Sub
Sub Combo1_Click()
If Combo1.ListIndex = 0 Then
param = "linux 5.1"
MsgBox param
End If
If Combo1.ListIndex = 1 Then
param = "linux 5.5"
MsgBox param
End If
End Sub
Sub Command3_Click()
MsgBox "param" & param
End Sub
What am I doing wrong?
It looks like this is due to Variable Scope. You need to define "param" outside of the Combo1_Click() subroutine, because as it stands, param only exists and is accessible within that routine.
What if Combo1.ListIndex is -1? Or 2?
Your param variable would never be assigned, and the behavior you see is exactly what is expected.

Resources