VBA - Filling input boxes in Internet Explorer not working - html

I'm having the following problem which is once I populate some inputbox on IE with information from Excel using VBA, these are populated correctly but when i change onto the second line with input boxes (they all are the same in format) the one I filled before does not get saved (even if I press save).. the only way I found for the information to remain is if I get into any of these boxes I'm filling and type something manually.
Anyone has an idea of why this might be?
Thanks!
For Each cell In wsbd.range(range("A6"), range("A6").End(xlDown))
additemsbtn.Click
Set aNodeList = ieDoc.querySelectorAll("[dojoinsertionindex]")
aNodeList.Item(0).Click
For i = 0 To 15
If ieDoc.getElementById("meetingResultsPlanningTable").getElementsByTagName("select")(0).Item(i).innerText = wsbd.range("A6").Value Then
ieDoc.getElementById("meetingResultsPlanningTable").getElementsByTagName("select")(0).Item(i).Selected = True
Exit For
End If
Next i
Set dropOptions = ieDoc.getElementById("meetingResultsPlanningTable").getElementsByTagName("select")(5)
dropOptions.Value = "Value"
Set itemName = ieDoc.getElementById("dynamicLineItems").getElementsByClassName("InputBox")(0)
itemName.Value = wsbd.range("F6").Value
Set itemName = ieDoc.getElementById("dynamicLineItems").getElementsByClassName("NumInputBox2")(0)
itemName.Value = wsbd.range("J6").Value
Set itemName = ieDoc.getElementById("dynamicLineItems").getElementsByClassName("NumInputBox")(0)
itemName.Value = wsbd.range("Q6").Value
Set itemName = ieDoc.getElementById("dynamicLineItems").getElementsByClassName("NumInputBox")(1)
itemName.Value = wsbd.range("T6").Value * 100
Set itemName = ieDoc.getElementById("dynamicLineItems").getElementsByClassName("NumInputBox")(1)
itemName.Value = itemName.Value + 0
'Set savebtn = ieDoc.getElementById("/images/buttons/save.gif")
' savebtn.Click
Next cell
The code is working and is reading properly all the inofrmation in Excel, finding the corresponding Input boxes and populating them but then nothing gets saved or recorded.. as you can see I tried saving after completing the boxes but it still doesn't work...

I came up with a solution! And pretty simple by the way.. I just added a fireevent ("onchange") for each input box and that records all changes!
Set itemName = ieDoc.getElementById("dynamicLineItems").getElementsByClassName("InputBox")(0)
itemName.Focus
itemName.FireEvent ("onchange")
itemName.Value = somevalue.Value

Related

How do you update a selected dropdown value using VBScript

I am trying to problematically change the dropdown selection on an InternetExplorer.Application then save that selection. The code I have so far is
dim myValue
myValue="3"
for j = 0 to obj.Options.length - 1
if(obj.Options(j).Value = myValue) then
obj.Options(j).selected = true
exit for
end if
next
This works on the current pages dropdown list, however when I click save, the value "3" isn't saved and it reverts back to its original value when I reload the page.
Another thing to mention is that when I manually click the dropdown and select a value then save, it does update to the new value when I reload the page. I have tried the obj.click function on it but I do not believe a programmatic mouse click works like a actual mouse click with the action listener.
My guess would be something to do with the databinding between the new value selection and the action listener for the page. I am fairly new to vbscript and have tried all sorts of different things.
Any help would be very much appreciated. Thank You!
Supposing you have the obj object set properly, e.g. something like
set obj = ie.document.getElementById("my_dropdown") then you should ensure that only one option is selected:
for j = 0 to obj.Options.length - 1
if (obj.Options(j).Value = myValue) then
obj.Options(j).selected = true ''' do not exit for
else
obj.Options(j).selected = false
end if
next
or
For Each opt In obj.Options
If opt.Value = myValue Then
opt.Selected = True
Else
opt.Selected = False
End If
Next
Caution: above code snippet could result to (undesired?) case that no option remains selected!

Dynamically updating a table row in HTA (VBS)

After researching I can see that to dynamically update a table within a HTA, I need to add the tbody element. I can also see that then I need to use the appendchild function to add the necessary data / rows to the table.
I've done this and am trying to loop through an array ArrLogs using the code below
Dim i
i = 1
Set table = document.getElementById("maintable")
Set tbody = document.createElement("tbody")
table.appendChild(tbody)
Set trow = document.createElement("tr")
Set tcol = document.createElement("td")
ArrLogs = ReadLogs(computerasset.value)
Do Until i = UBound(ArrLogs)
tcol.innerHTML = ArrLogs(i)
trow.appendChild(tcol)
tbody.appendChild(trow)
table.appendChild(tbody)
i = i+1
Loop
The problem I'm having is that I'm only seeing the last value of my array appended to the table, almost as if I'm missing a command to save the append and it's overwriting the row as it runs through?
I'm very concious that this isn't tidy, or the correct way to loop through an array (should use for i = 1 to UBound(ArrLogs) etc) - I was testing different ways of doing things in case I was making an obvious mistake.
trow.appendChild(tcol) does not copy tcol to the row; it inserts a reference to it, meaning that you only ever have one tcol that you constantly overwrite, E.g. the code below would show B not A
Set p = document.createElement("p")
p.innerHTML = "A"
document.body.appendChild(p)
p.innerHTML = "B"
To fix this create new elements inside your loop:
Dim i: i = 0
Set tbody = document.createElement("tbody")
ArrLogs = ReadLogs(computerasset.value)
for i = lbound(ArrLogs) to ubound(ArrLogs)
Set trow = document.createElement("tr")
Set tcol = document.createElement("td")
tcol.innerHTML = ArrLogs(i)
trow.appendChild(tcol)
tbody.appendChild(trow)
Next
document.getElementById("maintable").appendChild(tbody)

Creating a value in a control on a form

I need to create a value in a text box control upon triggering a certain event to allow me to then relink my forms to a different master/child link scheme. This value is to be used subsequently to create an if statement. For some strange reason, the value is generated and formatted correctly but regardless of what is in the text box, the If statement does not recognise this value and knows it only as blank. I tried numbers, letters but everything is the same.
In my example below, after updating the control (text box) 'txtDeviation' to the value of '1', for some strange reason is not recognised in as the value 1.
Private Sub cmdSkillsTracking_Click()
Form_frmValueChain01!frmValueChain02.SetFocus
Form_frmValueChain01.Pagina370.Visible = False
Form_frmValueChain01.Pagina371.Visible = True
If txtDeviation01 < 1 Then
Form_frmValueChain01.Form.frmValueChain07.LinkMasterFields = "txtMicroProcess01e"
Form_frmValueChain01.Form.frmValueChain07.LinkChildFields = "ID"
Form_frmValueChain01.Form.frmValueChain17.LinkMasterFields = "txtSubProcessID"
Form_frmValueChain01.Form.frmValueChain17.LinkChildFields = "IDskillsmatrix"
Form_frmValueChain01.Form.frmValueChain16.LinkMasterFields = "txtSubProcessID"
Form_frmValueChain01.Form.frmValueChain16.LinkChildFields = "ID"
Else
Form_frmValueChain01.Form.frmValueChain07.LinkMasterFields = "txtMicroProcess01f"
Form_frmValueChain01.Form.frmValueChain07.LinkChildFields = "ID"
Form_frmValueChain01.Form.frmValueChain14.LinkMasterFields = "txtMicroProcess01f"
Form_frmValueChain01.Form.frmValueChain14.LinkChildFields = "subprocessID"
Form_frmValueChain01.Form.frmValueChain10c.LinkMasterFields = "txtMicroProcess01f"
Form_frmValueChain01.Form.frmValueChain10c.LinkChildFields = "ID"
Form_frmValueChain01.Form.frmValueChain101.LinkMasterFields = "txtMicroProcess01f"
Form_frmValueChain01.Form.frmValueChain101.LinkChildFields = "ID"
Form_frmValueChain01.Form.frmValueChain07.LinkMasterFields = "txtMicroProcess01e"
Form_frmValueChain01.Form.frmValueChain07.LinkChildFields = "ID"
Form_frmValueChain01.Form.frmValueChain17.LinkMasterFields = "txtSubProcessID"
Form_frmValueChain01.Form.frmValueChain17.LinkChildFields = "IDskillsmatrix"
Form_frmValueChain01.Form.frmValueChain16.LinkMasterFields = "txtSubProcessID"
Form_frmValueChain01.Form.frmValueChain16.LinkChildFields = "ID"
End If
Two things I see here;
Since you are using a less than operator, you seem to want to treat
this text box value as numeric. If so, you will need to convert the
text value of the text box to numeric.
Next,you need to prefix the reference to the text box with "me."
Your IF statement should look like this;
If val(me.txtDeviation01) < 1 Then
...

Send message to user if they leave all fields blank when adding data to table; and don't add the data

So, I have a table, 2 forms, a sub form, 2 buttons, and a bunch of text boxes. With these I made a button that pops up an "add field" where you type information into text boxes then click add, and it adds it to the table which back on the other subform shows that data; I have that working. I am not sure how to make it not add the data if you leave ALL fields blank though (note; I only want it to be ALL fields, it's okay if they fill out one field and the rest are null). If all fields are null, it sends out a msgbox to the user saying please fill in data into the text boxes.
Another thing I am looking for for a future part of this database is required fields. Where lets say there are 10 text boxes, and before you can add the data into the table through the form you MUST fill out 6 out of 10 of the text boxes (marked with a * which ones are needed) and if you don't, give them an error saying please fill out [text boxes that weren't filled out] and try again. If all of the 6 required fields were filled out, then it can save it to the table even if the other 4 are null or not.
This is the code I have inside of the add button:
Private Sub CustomerAddBtn_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from CustomersT")
rec.AddNew
rec("CustomerName") = Me.CustomerAddSupplierNameTxt
rec("Address") = Me.CustomerAddAddressTxt
rec("City") = Me.CustomerAddCityTxt
rec("ProvinceState") = Me.CustomerAddProvinceStateTxt
rec("PostalZip") = Me.CustomerAddPostalZipTxt
rec("Phone") = Me.CustomerAddPhoneTxt
rec("Fax") = Me.CustomerAddFaxTxt
rec("CustomerSince") = Me.CustomerAddCustomerSinceTxt
rec("Email") = Me.CustomerAddEmailTxt
rec("Notes") = Me.CustomerAddNotesTxT
rec.Update
Set rec = Nothing
Set db = Nothing
'Send message to user saying it was saved, so they know
Dim intReply As Integer
intReply = MsgBox("Customer has been successfully saved to the database!", vbOKOnly, "Success!")
End Sub
Thanks in advance.
You can check if a Text Box has a value in it by using IsNull(Me.TextBoxName). So, if you want to test if all of the text boxes are empty then you can do something like this
If IsNull(Me.CustomerAddSupplierNameTxt) _
And IsNull(Me.CustomerAddAddressTxt) Then
MsgBox "Please don't try to enter an empty record."
Else
MsgBox "(user filled in at least one field)", vbInformation, "Debug Message"
' your existing database code here
End If
...and just expand the initial If statement to include all of the controls you want to check.
Similarly, for required fields, you can check the value of the controls corresponding to those required fields and display a similar message if any of them IsNull().

invalid property value in vb6

i have a code right here this is for saving records to databse:
If mstrMaintMode = "ADD" Then
lngIDField = GetNextCustID()
strSPName = "InsertCustomer"
Set objNewListItem = mylistview.ListItems.Add(, , txtname.Text)
PopulateListItem objNewListItem
With objNewListItem
**.SubItems(mlngCUST_ID_IDX) = CStr(lngIDField)**
.EnsureVisible
End With
Set mylistview.SelectedItem = objNewListItem
Set objNewListItem = Nothing
Else
lngIDField = CLng(mylistview.SelectedItem.SubItems(mlngCUST_ID_IDX))
strSPName = "UpdateCustomer"
mylistview.SelectedItem.Text = txtname.Text
PopulateListItem mylistview.SelectedItem
End If
the error is: invalid property value in the line with asterisks. ive tried using this code to another database and it works, but for the other it's not.ive checked the stored procedure, it's right, the table fields, also right but im still getting this error.ive spent 3 hrs to find the answer but i culdn't figure it out.
The line you have highlighted will fail with "Invalid property value" when you specify a sub item index that is out of bounds given the number of columns in the listview.
As the index is 1 based but starting from the second column, with your index of 7, you need at least 8 columns added.