Hey guys. I have recently built userforms that submit data entry into worksheets, but also into a webform for work. It all works perfectly for submitting data, but I've decided to make my userforms able to not only submit data, but to also remove any data that needs to be removed. So, as of right now, if I want to remove or modify any data that has already been entered into our webform, I have to go to the url, select the data to be removed or modified from a ListBox, and then click a button to remove or modify. By doing that, I wasn't changing any of the data that was also saved in the workbook, without going in manually and changing it myself. The whole point of creating the userform was to eliminate steps, and I'm not able to do that by going back and forth between excel and the webform. Here is the code I have right now for my "Remove" button in my Userform:
Right now the code works, but it's because I put in the 'Application.Wait' to allow the data to load after selecting the entered data from the listbox. I feel like using the Wait function isn't the best way of going about this, because if the server ever lags for some reason then it will throw off the rest of the code. I've tried many different DoEvent loops to try to get it to click the button after the data has loaded, but can't seem to get any to work. I've tried:
just as a guess to see if I can get it to loop while the entry field is blank, but I get an error doing that.
Anyone have any suggestions? If I'm not explaining myself or the situation very good, feel free to ask. Thanks for any help.
Code:
Private Sub CommandButton2_Click()
'Retrieve selected item value in combobox3 change event_______________
Dim TheValue As String
TheValue = efsform.ComboBox3.Text
'Find the corresponding row______________________________
Dim TheSearch As Object
Dim TheRange As Range
Set TheRange = Sheet2.Range(Sheet2.Cells(2, 3), Sheet2.Cells(Sheet2.UsedRange.Rows.Count, 1))
Set TheSearch = TheRange.Find(What:=TheValue, After:=TheRange.Cells(TheRange.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Sheet2.Cells(TheSearch.Row, 2).EntireRow.Delete
'Manipulate the Webform____________________________________
Dim IE As InternetExplorerMedium
Dim targetURL As String
targetURL = "http://miap33wsapx16/asoma/asomaentryform.aspx"
Set IE = New InternetExplorerMedium
IE.Visible = True ' Set to true to watch what's happening
IE.Navigate targetURL
Do Until IE.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
IE.Document.getElementById("ddlSelection").selectedIndex = 1
IE.Document.getElementById("ddlSelection").FireEvent ("onchange")
Do
DoEvents
Loop While IE.Document.getElementById("Sample_Arrival_Time") Is Nothing
'__Entered Sample Selection__________________________________________________
Dim selectElement As HTMLSelectElement
Dim optionIndex As Integer
Set selectElement = IE.Document.getElementById("ListBox1")
optionIndex = FindSelectOptionIndex(selectElement, ComboBox3.Value)
If optionIndex >= 0 Then
selectElement.selectedIndex = optionIndex
Else
MsgBox ComboBox3.Value & " Selected sample not found. "
End If
IE.Document.getElementById("ListBox1").FireEvent ("onchange")
Application.Wait (Now + #12:00:02 AM#)
IE.Document.getElementById("btnRemove").Click
Unload efsform
End Sub
Private Function FindSelectOptionIndex(selectElement As HTMLSelectElement, findOptionText As String) As Integer
Dim i As Integer
FindSelectOptionIndex = -1
i = 0
While i < selectElement.Options.Length And FindSelectOptionIndex = -1
Debug.Print i, selectElement.Item(i).Value & " >" & selectElement.Item(i).Text & "<"
If LCase(selectElement.Item(i).Text) = LCase(findOptionText) Then FindSelectOptionIndex = i
i = i + 1
Wend
End Function
Right now the code works, but it's because I put in the 'Application.Wait' to allow the data to load after selecting the entered data from the listbox. I feel like using the Wait function isn't the best way of going about this, because if the server ever lags for some reason then it will throw off the rest of the code. I've tried many different DoEvent loops to try to get it to click the button after the data has loaded, but can't seem to get any to work. I've tried:
Code:
Do
DoEvents
Loop While IE.Document.getElementById("Copper").innerText = ""
just as a guess to see if I can get it to loop while the entry field is blank, but I get an error doing that.
Anyone have any suggestions? If I'm not explaining myself or the situation very good, feel free to ask. Thanks for any help.