Auto-submit HTML Form using VB in Access 2007 - html

I'm at my wit's end on this one.
Part of a project I'm working on requires interaction with an HTML form to bring up the correct page on an external web-based tool. I've managed to make this form accept entered variables and be generated on the fly using document.write, but what I haven't been able to do reliably is trigger the 'Submit' action in a way that will not throw off Error 91.
So far, I've been using a simple getelementbyid.click call... Here's a list of a few things I've tried to get it to trigger:
TestLoop:
Pause (1)
BrowserControl.document.getelementbyid("Submit").click
On Error Goto TestLoop
*(The 'Error Loop' tactic didn't really work at all.)
Pause (3)
BrowserControl.document.getelementbyid("Submit").click
(This actually works the best, but because different PCs might have different hardware specs and open applications that might slow it down, what works for me hasn't been working for another tester. If I make it TOO long, I end up adding additional delay to a form I'm already worried about being too slow.)
<script type="text/javascript">
window.onload = function(){
document.getElementById('Submit').click();;
}
</script>
(Embedding an auto-trigger in the generated HTML, frustratingly, works OUTSIDE of the embedded web browser control, but not inside it.)
Private Sub wb_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
If Me.BrowserControl.Document.URL= "about:blank" Then
BrowserControl.document.getelementbyid("Submit").click
End If
End Sub
(For some insane reason, using debug.print I found that it's saying the navigation is complete before it's actually complete, so this didn't work at all.)
I may have missed a few... I've been attacking this from a bunch of different angles. The trick is that I have to use the HTML form to interact with this system that I don't have admin priveledges on, so changing how the information is recieved isn't really an option.
My last train of thought was to see if I could mimic a FORM submission using straight VB, bypassing the need for a 'click' event, but it's been exceptionally difficult to search for things related to HTML FORM interactions with Access 2007 Forms. :P
Any assistance would be greatly appreciated!

Well, after 7 months of testing and thousands of activations in that time, splitting the 'work' seems to have done the trick.
Form_Open: Initialises the Browser Object and sets its address to about:blank
Form_Current: Calls a formula that 'draws' the web form using Document.Write, then enters a loop...
TestLoop:
BrowserControl.document.getelementbyid("Submit").click
On Error Goto TestLoop

Related

VBA webcontrol automation problem with inputfield

I'm an accountant and I develop a lot of automation to carry the repetitive task of my job. I've been fairly successful with most of them, but I got stuck with some code I can't find the solution to and probably it's just a simple thing I should already know.
So it's like this, I used to use selenium and make the code open an IE instance and navigate, login, fill multiple fields, select radio buttons etc. But I was using this with sendkeys function and Tab.
Now I reviewed all the code and implemented a webbrowser element to a userform and I'm using HTMLdoc to interact with the webpage, the objective of the app is to open the website, fill the invoice information then send and save the file, right now I'm able to login and navigate to the new invoice page.
The problem starts here, I use this code to get the text to the input fields on the webpage:
HTMLDoc.getElementsByName("dataInicio").Item(0).Value = Controlo_Arrendamento.txt_dataInicio
HTMLDoc.getElementsByName("dataFim").Item(0).Value = Controlo_Arrendamento.txt_dataFim
HTMLDoc.getElementsByName("dataRecebimento").Item(0).Value = Controlo_Arrendamento.txt_novoData.Text
HTMLDoc.getElementsByName("valor").Item(0).Value = Controlo_Arrendamento.txt_novoPago.Text & ",00"
HTMLDoc.getElementsByClassName("btn btn-sm btn-success").Item(0).Click
What happends is, when the submit button was pressed, the website gives me an error in every field, then I realized that when the data is inputed on the website normaly it automaticaly updates other elements on the webpage, it goes through some validation, so when my code inserts the values on the textbox it doesn't trigger, only by typing the values in...
So, I tried to dig everywhere for a solution, I watched the HTML elements and what changes when the value is valid and it's checked, tried implementing a .checked = true but no solution.
Anyone knows what I can do to get trough this?
I could send you the webpage but it needs a login and since it's a nacional finances website I'm afraid I can't share with you any login, but I will leave here the website in question, it may help figuring out what type of form i'm interacting with.
https://www.portaldasfinancas.gov.pt
Thanks in advance

Unable to replicate Refresh All quick access toolbar button in form

I have a form with several subforms whose content is edited in separate forms, opened by command buttons.
When the data in those forms is updated, and the extra forms closed, naturally the main form needs to be refreshed before those changes can be seen. Clicking the Refresh All quick access toolbar button achieves this perfectly.
So I made a macro for the "got focus" event on the main form to refresh it, but it does nothing. I tried repaint and requery as well, applying the latter specifically to the subforms in question, and ran them out of vba instead of an access interface built macro too, none of these seem to solve the problem.
vba coding:
Private Sub Form_GotFocus()
Screen.ActiveForm.Requery
Screen.ActiveForm.Repaint
Screen.ActiveForm.Refresh
Me.sfmContact.Requery
Me.frmCustCert.Requery
Me.frmCustReq.Requery
End Sub
Annoying thing is that if I put this (or even just the macro or Screen.ActiveForm.Refresh) in the on click event of one of the controls it works fine. Just can't seem to make it work in any automatic events that don't require thought or clicks from the end user.
This question is sort of a dupe of Refresh button on an Access form but that question is over a year old, and has no accepted answer. So I've expanded on it.
What you mentioned in your comment there is correct:
My guess is that it's not running it, but I don't know why. I got mixed up between OnActivate and OnLoad, which is why I didn't try that option. That works and solves the problem, but why wouldn't GotFocus fire? Surely the main form lost focus when another form got it for updating, and then it got it back again when the other form closed/anything in the main form was selected...
To avoid this type of error in the future, it may be prudent to specifically name the form in your code instead of using the "Me." command, as that only works for current focus. For instance:
Me.sfmContact.Requery > Forms![FormName].sfmContact.Requery
Alternatively, you could just set the focus to the form in question before that sort of coding.
Forms![FormName].SetFocus

Run VBA code whenever a tabbed form is reselected in Access

EDIT: For clarification, I'm talking in the below post about tabbed document browsing, not a Tab Control. However, if you're looking for roughly the same problem but regarding a Tab Control, Gord Thompson's answer is correct. Two answers for the price of one!
I've got an Access 2007 database that uses tabbed documents. I need to run some VBA code every time a user selects the form called "Reports", either via opening it or clicking on its tab if it's already open.
I could achieve much the same thing by closing it each time it's used and running the code on an OnLoad event, but ideally I'd like to keep it open so that users can keep the settings of the various drop down boxes, radio boxes etc that they've already set on "Reports".
I was hoping for an event that could run code on tab reselection, but neither of my guesses (OnCurrent and GotFocus) seem to work (OnCurrent works only when the form is opened, like OnLoad would).
Any ideas greatly appreciated - can't find what I'm looking for on Google, though I suspect that's because I don't know exactly what I'm looking for.
The .Value property of a TabControl returns the index (zero-based) of the current page. So, if I have a TabControl named TabCtl14 that contains two pages, FirstPage and SecondPage, then the code...
Private Sub TabCtl14_Click()
If Me.TabCtl14.Value = 1 Then
MsgBox "SecondPage was clicked."
End If
End Sub
...displays the MsgBox whenever I click the "SecondPage" tab in the TabControl.
Have found the answer I was looking for. It's the OnActivate event.

Creating "are you sure?" popup window by using html only

Assume I have a html from, and it contain some submit type. I want to create a "are you sure" popup window that will appear when user click submit button.
My question is that is there any way to create it by using "only" html, not using javascript or any other?
HTML only is possible, but not without a postback
Scenario that could work without javascript:
You have your form with submit button
User clicks (and submits) the form
You display another form with are you sure? form (that contains Yes and No buttons as well as hidden fields of the first form that will make it possible to do the action required on the original data
functionality that executes the action and goes back to whatever required.
This would be completely Javascript free, but it would require several postbacks.
This kind of thing is usually done on the client with a Javascript confirm() function (here's a simple example) or lately with a more user friendly modal dialog provided by many different client libraries or their plugins.
When to choose the script free version?
If you know your clients are going to be very basic ones (ie. vast majority of your users will access your application using clients like Opera Mini that's not able to run scripts at all). But in all other cases it's much better to do this using Javascript. It will be faster, easier to develop and much more user friendly. Not to mention that it will put less strain on your server as well since certain parts will execute on the client without the need of any server processing.
No, there isn't. Despite of the new features in HTML 5, HTML is still a markup language, not a programming language. In order to express dynamic behavior (such as an "are you sure?" box), you need to use a programming language.
Javascript would be the most obvious choice for this, but you could also do it with frameworks that can get you around writing Javascript by hand (for example ASP.NET).
Edit: Actually it appears that it would theoretically possible to do this with without Javascript or other frameworks. As I just learned, HTML 5 + CSS 3 seems to be turing complete. But this is hardly relevant to this question.
It's possible to ask for a confirmation, but it will not be in a "popup window". The creation of the "popup window" requires javascript/other language.
It will be:
Request (first form)
POST
Response (confirmation form)
POST
Response (outcome message)
You can create a form with all hidden elements containing the data from the first form and a "Yes" and "No" button below the "Are you sure?" text. You can use PHP sessions to avoid the hidden form elements. If there is a lot of data or confidential data or you do not want to re-validate the data from the second form, use sessions. Make sure you validate the data from either form before using it.
I know I'm like .. 10 years late. But for anyone still wondering I thought I could be of some help!
What I did for this exact problem was make sure I had multiple "divs" in my code. For me specifically, I had two main ones.
First, one whose id="main", and another whose id="popup" with the 'visible' property initially set to 'false' for the popup div.
Then, on whichever event you're looking for (button click for example) you'll simply set main.Visible = false and popup.Visible = true, then you could have more buttons in your popup (yes, no, cancel, confirm, etc.) which do the exact same thing, but in reverse!
The most important thing to make sure of is that you have the 'runat="server"' property in your divs so that you can access them in your CS code
Hope this was helpful! :)

What can prevent an MS Access 2000 form from closing?

My Access 2000 DB causes me problems - sometimes (haven't pinpointed the cause) the "book" form won't close. Clicking its close button does nothing, File -> Close does nothing, even closing Access results in no action. I don't have an OnClose handler for this form. The only workaround I can find involves opening the Vba editor, making a change to the code for that form (even adding a space and then immediately deleting the space), and then going back to close the "book" form, closing it, and saying "no, I don't want to save the changes". Only then will it close. Any help?
Here's a forum post describing, I think, the same problem you face. Excerpt belows states a workaround.
What I do is to put code on the close button that reassigns the sourceobject
of any subforms to a blank form, such as:
me!subParts.sourceobject = "subBlank" 'subBlank is my form that is
totally blank, free of code and controls, etc.
docmd.close acForm, "fParts", acSaveNo
The above 2 lines is the only way I've found to prevent the Access prompt
from popping up.
http://bytes.com/forum/thread681889.html
Another alternative is
(Me.Checkbox)
or my preferred syntax:
(Me!Checkbox)
It seems to me that there is much confusion in the posts in this topic. The answer that was chosen by the original poster cites an article where the user had a prompt to save design changes to the form, but the problem described here seems like it's a failure of the form to close, not a save issue (the save issue came up only in the workaround describing going to the VBE and making a code change).
I wonder if the original user might have incorrect VBE options set? If you open the VBE and go to TOOLS | OPTIONS, on the GENERAL tab, you'll see several choices about error handling. BREAK ON UNHANDLED ERRORS or BREAK IN CLASS MODULE should be chosen, but it's important to recognize that if you use the former, you may not see certain kinds of errors.
There's not really enough detail to diagnose much more, other than the fact that the reference to the checkbox control seemed to have been causing the problem, but there are a number of Access coding best practices that can help you avoid some of these oddities. The code-related recommendations in Tony Toews's Best Practices page are a good place to start.
That sure is weird. Do you have any timer controls on the form? If you do, try disabling it in the OnClose.
There is a possibility that the message box that asks if you want to save changes is being displayed behind the form. I believe that this message box is modal so you must click yes or no before you can do anything with the form which is why you can't close it.
Does your form have an unload event? That can be canceled, and if it is, the form won't close when it's in form view. It will only close in design view, which, when you edit the vba code is what the form does in the Access window when you're editing the code.
Does your form have a checkbox, toggle button or option button? There's a bug in Access 2000 where Access won't close if you test the value without explicitly using the Value property in the vba code, like this:
If Me.chkbox Then
versus:
If Me.chkbox.Value Then