使用MS Access VBA将数据发送到Webform

时间:2017-05-19 20:57:04

标签: html vba ms-access

我正在尝试将数据从Access数据库发送到网站http://www.lee.vote/voters/check-your-registration-status/。我可以使用类似的代码(如下)将数据发送到其他网站,但我无法弄清楚它为什么不适用于此网站。

我试图填写的HTML:

<div id="FindVoterForm">
    <div id="IntroText">
    <h1 style="text-align: center; margin-bottom: 3px;">Voter Information in&nbsp;<span id="MainCounty">Lee</span>&nbsp;County</h1>
        <h2 style="text-align: center; margin-top: 3px; margin-bottom: 3px;">Sample Ballots and Voting Locations</h2>
    <span class="style1" style="margin-bottom: 0px;">Complete the form to see:</span><ul style="margin-top: 0px;">
        <li class="style1"><b>Where to vote on election day</b></li>
        <li class="style1"><b>Sample ballots</b></li>
        <li class="style1"><b>Upcoming elections</b></li>
    </ul>
    <p class="style2" style="margin-bottom: 0px;">
        You'll also be able to:</p>
    <ul style="margin-top: 0px;">
        <li class="style2">Request a mail ballot</li>
            <li class="style2">Review/update your voter registration information</li>
            <li class="style2">Check the status of your mail ballot</li>
            <li class="style2">Review your voting activity for the past 12 months</li>
    </ul>
    <div id="NotRegistered" style="font-size: small;"><a href="https://www.voterfocus.com/ws/pfinder/printvapp4.php?county=lee" target="_blank">If you are not registered to vote please fill out our voter registration form</a></div><br>
    <i><b style="text-decoration: underline;">All items are required</b></i>.
    </div>
    <div class="voterForm">
    <div class="voterFormLine"><div>1.</div><div>Voter's Last Name</div><div><input title="Please enter your last name." id="NameID" type="text" size="10" maxlength="35" value=""></div> 
    </div><div class="voterFormLine"><div>2.</div><div>Voter's Birth Date</div><div><input title="Please enter your birth date (MM/DD/YYYY)." id="BirthDate" type="text" size="10" maxlength="10" value="">
    <br>MM/DD/YYYY</div></div><div class="voterFormLine"><div>3.</div>
    <div><a title="House Number" href="https://www.voterfocus.com/VFVoterGlossery.php?term=House Number" target="_blank">House Number</a> of Voter's Residence Address</div>
    <div><input title="Please enter your house street number." id="StNumber" type="text" size="10" maxlength="10" value=""></div> 
    </div>
    <div>&nbsp;</div>
    </div>
    <div><div style="text-align: center;"><h2 id="MoreVoter" style="display: none;"><b></b></h2>
    <button id="ButtonForm" onclick="ButtonForm_onclick()" type="button" value="Submit">Submit</button></div>
    </div> 
    </div>


VBA代码:

'creates a new internet explorer window
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

'opens Lee County registration check
With IE
    .Visible = True
    .navigate "http://www.lee.vote/voters/check-your-registration-status/"
End With

'waits until IE is loaded
Do Until IE.ReadyState = 4 And Not IE.busy
    DoEvents
Loop

'sends data to the webpage
Call IE.Document.getelementbyid("NameID").setattribute("value", Last_Name)
Call IE.Document.getelementbyid("BirthDate").setattribute("value", Date_of_Birth.Value)
Call IE.Document.getelementbyid("StNumber").setattribute("value", Street_Number.Value)

'"clicks" the button to display the results
IE.Document.getelementbyid("ButtonForm").Click

任何帮助?

2 个答案:

答案 0 :(得分:0)

您提供的HTML代码段属于iframe <iframe id="dnn_ctr1579_View_VoterLookupFrame" src="https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee" width="100%" height="2000" frameborder="0"></iframe>,因此您应该导航到网址https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee而不是http://www.lee.vote/voters/check-your-registration-status/

我在Chrome浏览器中导航https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee并在我通过开发人员工具( F12 ),网络标签提交数据后检查了XHR:

xhr

似乎这是带有JSON格式的有效负载的简单POST XML HTTP请求,如:

{'LastName':'Doe', 'BirthDate':'01/01/1980', 'StNumber':'10025', 'County':'lee', 'FirstName':'', 'challengeValue':'', 'responseValue':''}

XHR在标题和有效负载中都不使用cookie或任何其他授权数据,因此我尝试使用以下代码重现相同的请求:

Option Explicit

Sub Test_Submit_VoterInfo()

    Dim sLastName As String
    Dim sBirthDate As String
    Dim sStNumber As String
    Dim sFormData As String
    Dim bytFormData
    Dim sContent As String

    ' Put the necessary data here
    sLastName = "Doe"
    sBirthDate = "01/01/1980"
    sStNumber = "10025"
    ' Combine form payload
    sFormData = "{" & _
    "'LastName':'" & sLastName & "', " & _
    "'BirthDate':'" & sBirthDate & "', " & _
    "'StNumber':'" & sStNumber & "', " & _
    "'County':'lee', " & _
    "'FirstName':'', " & _
    "'challengeValue':'', " & _
    "'responseValue':''" & _
    "}"
    ' Convert string to UTF-8 binary
    With CreateObject("ADODB.Stream")
        .Open
        .Type = 2 ' adTypeText
        .Charset = "UTF-8"
        .WriteText sFormData
        .Position = 0
        .Type = 1 ' adTypeBinary
        .Position = 3 ' skip BOM
        bytFormData = .Read
        .Close
    End With
    ' Make POST XHR
    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", "https://www.electionsfl.org/VoterInfo/asmx/service1.asmx/FindVoter", False, "u051772", "mar4fy16"
        .SetRequestHeader "Content-Length", LenB(bytFormData)
        .SetRequestHeader "Content-Type", "application/json; charset=UTF-8"
        .Send bytFormData
        sContent = .ResponseText
    End With
    ' Show response
    Debug.Print sContent

End Sub

对我的回复是{"d":"[]"},与浏览器相同,但不幸的是我无法检查它是否在服务器上正确处理,因为我没有有效的选民记录数据。

答案 1 :(得分:0)

这是我在(非常需要的)帮助确定我没有真正导航到表格的正确网页后得出的答案:

'creates a new internet explorer window
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

'opens Lee County registration check
With IE
    .Visible = True
    .navigate "https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee"
End With

'waits until IE is loaded
Do Until IE.ReadyState = 4 And Not IE.busy
    DoEvents
Loop
x = Timer + 2
Do While Timer < x
    DoEvents
Loop

'sends data to the webpage
Call IE.Document.getelementbyid("NameID").setattribute("value", Last_Name.Value)
    'formats DOB to correct output
    Dim DOBMonth As Integer
    Dim DOBDay As Integer
    Dim DOBYear As Integer
    DOBMonth = Month(Date_of_Birth.Value)
    DOBDay = Day(Date_of_Birth.Value)
    DOBYear = Year(Date_of_Birth.Value)

    If DOBMonth < 10 Then
        Call IE.Document.getelementbyid("BirthDate").setattribute("value", "0" & DOBMonth & "/" & DOBDay & "/" & DOBYear)
    Else
        Call IE.Document.getelementbyid("BirthDate").setattribute("value", DOBMonth & "/" & DOBDay & "/" & DOBYear)
    End If
Call IE.Document.getelementbyid("StNumber").setattribute("value", Street_Number.Value)

'"clicks" the button to display the results
IE.Document.getelementbyid("ButtonForm").Click